Code: Advanced programming with Jennifer
Advanced programming with Jennifer: the code
The programs behind the slides. They use only system libraries and bundled modules, need no server and no decks, and run with Jennifer 0.27.0 from this folder. Timings and profile figures depend on the machine (the slides were measured on an 8-core laptop); the shapes, counts and positions do not.
spawn.j: four CPU-bound jobs, one after the other and then each in its ownspawn, collected withtask.waitAll("spawn: run work in parallel"). Run:jennifer run spawn.j. The two times differ by machine.copy.j: a spawn sees the list as it was when it started, prints6 4("A spawn gets its own copy"). Run:jennifer run copy.jchannel.j: a producer streams squares through a channel of capacity 4, the consumer sums them to 55 ("Channels stream values between tasks"). Run:jennifer run channel.jcancel.j: a worker stopped bytask.cancelafter three rounds, and a slow task given up on withtask.waitTimeout("Cancellation and deadlines"). Run:jennifer run cancel.j. The round count relies on 100 ms sleeps against a 350 ms wait, so a very loaded machine may print another count.trycatch.j: a malformed JSON document and 5000 opening brackets, told apart by kind ("try / catch: decide by kind"). Run:jennifer run trycatch.jkinds.j: one catch per generic kind:limit,timeout,cancelled,closed, andnetwith the fields ofnet.NetErrorfrom a refused connection to127.0.0.1:1("Error kinds you can rely on"). Run:jennifer run kinds.j. Needs nothing listening on port 1.defer.j:setup(false)andsetup(true)withdeferanderrdefer("defer and errdefer: clean up on the way out"). Run:jennifer run defer.jconcat.j: the two loops of 20,000 rounds for the profiler ("Measure first: jennifer profile"). Run:jennifer profile concat.j. The profiler prints full paths and every position; the slide shows the four loop rows with the path shortened. Times differ by machine.concatbench.j:+in a loop against append-then-join for 20,000 and 40,000 lines ("Why+in a loop is slow", "Append to a list, join once"). Run:jennifer run concatbench.j. Times differ by machine.borrow.j: three helpers called 300 times each; onlycellOf, which takes alist of list of int, copies ("profile --allocs: what was copied", "Copied or borrowed?"). Run:jennifer profile --allocs borrow.j. The positionborrow.j:32:22and the 3.5 GiB estimate match the slide; the path is printed in full.
| File | Size | |
|---|---|---|
| LICENSE | 899 B | raw |
| README.md | 2.5 KiB | raw |
| borrow.j | 1011 B | raw |
| cancel.j | 909 B | raw |
| channel.j | 586 B | raw |
| concat.j | 552 B | raw |
| concatbench.j | 1.0 KiB | raw |
| copy.j | 272 B | raw |
| defer.j | 699 B | raw |
| kinds.j | 968 B | raw |
| spawn.j | 815 B | raw |
| trycatch.j | 516 B | raw |