diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-11 13:22:07 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-11 13:26:44 -0300 |
| commit | 2f27fbe0aa2da0f85d599c1e5f3ac14d025f590c (patch) | |
| tree | 140a291011e453888ed87b7a43d75f538abb9fae /anim/scenes | |
| parent | 576a78b4de02c4c6566c327d85c465554d293975 (diff) | |
| download | computeranim-2f27fbe0aa2da0f85d599c1e5f3ac14d025f590c.tar.gz computeranim-2f27fbe0aa2da0f85d599c1e5f3ac14d025f590c.zip | |
canim: the graph of a function's blocks
From the user. A blocks lens draws a function's basic blocks as boxes
with their offsets and visit counts, loops in their lanes with the turn
count, the path's last edges gold, the pc's block gold, and the
disassembly as its view; leave and turn carry the mark along an edge,
with the compare's flags framed for the edge chosen. On the map the
graph takes the disassembly's place with its rows. The layout is a pure
function, tested on sort, reverse and libc's open (twelve blocks the run
never reached fold into six boxes). The sort scene walks one trip of its
inner loop block by block and sprints the middle on the map. Every
scene's linear rows carry a rule and a count at each block's head. The
stack's panel no longer reads registers from the file on every draw: a
frame is 20 ms where it was 57.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'anim/scenes')
| -rw-r--r-- | anim/scenes/gallery.zig | 107 | ||||
| -rw-r--r-- | anim/scenes/sort.zig | 100 |
2 files changed, 162 insertions, 45 deletions
diff --git a/anim/scenes/gallery.zig b/anim/scenes/gallery.zig index 693553e..48fba99 100644 --- a/anim/scenes/gallery.zig +++ b/anim/scenes/gallery.zig @@ -10,6 +10,11 @@ //! 3.6 s a step, mid-way: struck, read, projection, echo (mock 10) //! 6 s `subject` (mock 11); 8 s `none`, with the backtrace //! 10.8 s two loupes in depth, from rax and its node's `next` (mock 21) +//! 21 s the `blocks` lens: at rest (blocks reached and not yet, the trail, +//! an edge never taken), `leave` mid-way, `turn` landing, folds +//! 24 s the graph with its rows, as the map draws it (here at half size): +//! sort's (panned to its loop, the pc's block annotated) and libc's +//! `open` at its syscall (the blocks never reached folded) const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); @@ -306,6 +311,101 @@ fn rowsPage(u: ui.Ui, p: *const Pages) void { _ = canim.hexdump.reading(panel, 3 * row, 0, m, &values, &known, &.{ .{ .at = 0, .len = 1, .type = char }, .{ .at = 1, .len = 1, .type = boolean }, .{ .at = 4, .len = 4, .type = int }, .{ .at = 8, .len = 8, .reads = .pointer } }); } +/// The graph pages' names, over their drawings. +const GraphPages = struct { + box: canim.Rect = .{ .x = 0, .y = 0, .w = 1920, .h = 1080 }, + + pub fn draw(_: *const GraphPages, f: *sk.Frame, at: anim.Placed) void { + const u: ui.Ui = .{ .f = f, .at = at }; + if (at.t >= 21 and at.t < 24) { + title(u, 64, 40, "graph: the blocks lens"); + for ([_][2]f32{ .{ 64, 72 }, .{ 976, 72 }, .{ 64, 488 }, .{ 976, 488 } }, [_][]const u8{ "at rest: reached, not yet, the trail, never taken", "leave: the edge drawn gold, the mark on it", "turn: landed, the lane's count and the header's struck", "folds: libc's open, the blocks never reached" }) |p, words| note(u, p[0], p[1], words); + } else if (at.t >= 24 and at.t < 27) { + title(u, 64, 40, "graph: with its rows, as on the map (half size)"); + } + } +}; + +/// A component drawn at half its size (the map's rows as the map shows them filling a 1080p frame). +fn Half(comptime T: type) type { + return struct { + inner: T, + box: canim.Rect = .{ .x = 0, .y = 0, .w = 1920, .h = 1080 }, + + pub fn draw(h: *const @This(), f: *sk.Frame, at: anim.Placed) void { + var p = at; + p.xf = p.xf.mul(.scale(0.5, 0.5)); + p.scale *= 0.5; + h.inner.draw(f, p); + } + }; +} + +/// The graph pages: sort's blocks in the lens, at rest and in `leave` and +/// `turn`, and libc's `open` folded; then the map's form of both. +fn graphPages(s: *anim.Frame, at_time: f32) [3]anim.Obj { + const graph = canim.graph; + const sort = canim.open(recordings.gallery_sort); + const hello = canim.open(recordings.gallery_hello); + const sort0 = sort.register(sort.call("sort", 0).enter, "rip"); + // Where the opening of the sort scene is: the inner loop's test, about to compare 31 with 12. + var start = sort.call("sort", 0).enter; + while (sort.next(.over, sort.next(.over, start)) < sort.call("swap", 0).enter) start = sort.next(.over, start); + // The first `jne` to the swap, and the first jump back to the test. + var jne = start; + while (!(sort.insn_branches[jne] != null and sort.insn_branches[jne].?.taken and sort.insn_branches[jne].?.to == sort0 + 0xc5)) jne += 1; + var back = jne; + while (!(sort.insn_branches[back] != null and sort.insn_branches[back].?.to == sort0 + 0x8a)) back += 1; + var syscall_at: u64 = 0; + var buf: [256]u8 = undefined; + for (hello.registers, 0..) |r, k| if (hello.strings.get(r.rip)) |name| if (std.mem.startsWith(u8, name, "libc.so.6!open+") and std.mem.eql(u8, (hello.instruction(k, r.rip, &buf) orelse continue).text, "syscall")) { + syscall_at = k; + break; + }; + const place = struct { + fn stage(s_: *anim.Frame, band: canim.Rect, disasm: canim.Rect) *canim.Stage { + var split = canim.layout.split.blocks; + split.band = band; + split.disasm = disasm; + return canim.Stage.init(s_, split); + } + }.stage; + const nowhere: canim.Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 }; + const rest = canim.cursor(s, sort, start); + const leaving = canim.cursor(s, sort, jne); + const turning = canim.cursor(s, sort, back); + const open_at = canim.cursor(s, hello, syscall_at); + const lenses = .{ + canim.add(s, graph.Blocks{ .cursor = rest, .stage = place(s, .{ .x = 64, .y = 84, .w = 880, .h = 296 }, nowhere) }), + canim.add(s, graph.Blocks{ .cursor = leaving, .stage = place(s, .{ .x = 976, .y = 84, .w = 880, .h = 296 }, nowhere) }), + canim.add(s, graph.Blocks{ .cursor = turning, .stage = place(s, .{ .x = 64, .y = 500, .w = 880, .h = 296 }, nowhere) }), + canim.add(s, graph.Blocks{ .cursor = open_at, .stage = place(s, .{ .x = 976, .y = 500, .w = 880, .h = 296 }, nowhere) }), + }; + // Each step played so that the page shows it at `at_time`: the leave with its mark mid-way, the turn just landed. + const l = canim.theme.time.leave; + s.cursor = at_time - l.sets_off - l.mark / 2.0 - 0.05; + graph.leave(leaving, s); + s.cursor = at_time - l.sets_off - 0.30 - l.count; + graph.leave(turning, s); + // The map's form, at half size: the map's units, two columns wide each. + const maps = .{ + canim.add(s, Half(graph.Map){ .inner = .{ .cursor = canim.cursor(s, sort, jne), .stage = place(s, nowhere, .{ .x = 128, .y = 176, .w = 1792, .h = 1688 }) } }), + canim.add(s, Half(graph.Map){ .inner = .{ .cursor = canim.cursor(s, hello, syscall_at), .stage = place(s, nowhere, .{ .x = 1952, .y = 176, .w = 1792, .h = 1688 }) } }), + }; + const pages = canim.add(s, GraphPages{}); + var all: [lenses.len]anim.Animation = undefined; + inline for (lenses, 0..) |o, i| all[i] = o.fadeIn(); + s.cursor = 21; + s.play(.{pages.fadeIn()}, .{ .seconds = 0 }); + s.play(all, .{ .seconds = 0 }); + s.cursor = 24; + inline for (lenses, 0..) |o, i| all[i] = o.fadeOut(); + s.play(all, .{ .seconds = 0 }); + s.play(.{ maps[0].fadeIn(), maps[1].fadeIn() }, .{ .seconds = 0 }); + s.cursor = 27; + return .{ pages, maps[0], maps[1] }; +} + fn frame(s: *anim.Frame) void { const rec = canim.open(recordings.gallery); const reverse = rec.call("reverse", 0); @@ -341,5 +441,12 @@ fn frame(s: *anim.Frame) void { near.open(s); s.wait(18.8 - s.cursor); deeper.open(s); + s.wait(20.6 - s.cursor); + deeper.close(s); + near.close(s); s.wait(21 - s.cursor); + // The graph's pages: the context stands aside. + inline for (panels, 0..) |o, i| all[i] = o.fadeOut(); + s.play(all, .{ .seconds = 0 }); + _ = graphPages(s, 21.8); } diff --git a/anim/scenes/sort.zig b/anim/scenes/sort.zig index 82a99e1..81d55dd 100644 --- a/anim/scenes/sort.zig +++ b/anim/scenes/sort.zig @@ -1,11 +1,17 @@ -//! Insertion sort: neighbours compared, and swapped while the left is larger. +//! Insertion sort as its blocks: the graph of `sort` in the band (the +//! `blocks` lens), the disassembly its view over the state column, the +//! source alone in the program column; on the map the same graph with its +//! rows. //! -//! How it moves (design/motion.md): it opens where `i` has reached 12, the -//! first value out of order; the compare is walked (its two cells framed: -//! the swap's anticipation), the caption about the swap stands two -//! seconds before it, and the swap is two flights on arcs, the second a -//! little behind the first, each cell struck as its value lands. The next -//! trips are a run, the long middle a sprint, and the last swap is walked. +//! How it moves (design/motion.md, "A block left"): it opens where `i` has +//! reached 12, the first value out of order. One trip round the inner +//! loop is walked a block at a time: each block's instructions a cascade +//! onto its last row, then `leave` along the edge that row takes (the +//! compare's chips framed, the edge drawn gold, the mark along it, the +//! target's count struck), and `turn` up the inner loop's lane. The next +//! trip is a run; the long middle is a sprint on the map (`surface`, the +//! gold going block to block, `dive`); the last value's trip is walked +//! again, and the way out run. const anim = @import("anim"); const canim = @import("canim"); const recordings = @import("recordings"); @@ -15,52 +21,56 @@ pub const scene = anim.Live(frame, struct {}); fn frame(s: *anim.Frame) void { const rec = canim.open(recordings.sort); + const graph = canim.graph; const t = canim.theme.time; - const sort = rec.call("sort", 0); // The `while`, about to compare 31 with 12: the stop before the one that calls swap. - var start = sort.enter; + var start = rec.call("sort", 0).enter; while (rec.next(.over, rec.next(.over, start)) < rec.call("swap", 0).enter) start = rec.next(.over, start); + // The inner loop's test (`j > 0`): where each of its trips begins. + const test_pc = rec.register(start, "rip"); var at = canim.cursor(s, rec, start); - // The array is the idea; `i` and `j` are memory, and strike a beat before their tags slide. - at.levels = .{ .idea = .data, .follow = t.quick, .source_rows = 10 }; - // Its last compare: nothing larger is left of 20. + at.levels = .{ .fire = .disasm, .source_rows = 20 }; const last = rec.call("swap", 9); - const end = rec.next(.over, rec.next(.over, last.exit.?)); - at.journey(.over, end); - // The context is the instrumented program's: its registers are unmarked. - const split = canim.layout.split.subject; - const ctx = canim.context(s, at, .{ .split = split }); - // The lens: the array as cells, and the two indices that walk it. - const cells = canim.array(s, .{ .cursor = at, .stage = ctx.stage, .variable = "a", .indices = &.{ "i", "j" }, .focus = true }); - ctx.map.add(s, canim.Array{ .cursor = at, .stage = ctx.map.stage, .variable = "a", .indices = &.{ "i", "j" } }); + // The last value's trip: from its test, before the compare that leads to the last swap. + var last_trip = last.enter; + while (rec.register(last_trip, "rip") != test_pc) last_trip -= 1; + at.journey(.over, rec.call("sort", 0).exit.? - 1); + const ctx = canim.context(s, at, .{ .split = canim.layout.split.blocks, .graph = true }); + const blocks = canim.add(s, graph.Blocks{ .cursor = at, .stage = ctx.stage, .focus = true }); var says: canim.Narrator = .{}; - // A swap, start to finish, as one step of sort(): over the call. - const swap = struct { - fn step(c: canim.Cursor, frame_: *anim.Frame, r: *canim.Recording) void { - c.step(frame_, r.next(.over, c.moment(frame_)), canim.Array.swap, canim.Array.swap.total); + const joins = ctx.ties(s); + // A block at a time: its rows a cascade onto its last, then the edge it takes. + const block = struct { + fn walk(c: canim.Cursor, f: *anim.Frame) void { + graph.toEnd(c, f); + graph.leave(c, f); } - }.step; + }.walk; - const joins = ctx.ties(s); - // The cells come in beside their bytes, one by one. - ctx.establish(s, .{ ctx.clock, ctx.registers, ctx.stack, ctx.source, ctx.disasm, cells, joins }, 8); - says.line(s, "`i` walks right. Everything left of it is in order,\nand 12 is not yet in its place."); - // The compare: both cells framed. It is the swap's anticipation. - at.walk(s, .over, rec.next(.over, start)); - says.line(s, "While the one on the left is larger, they trade places,\nand `j` follows the smaller value down."); - // Arcs: the two values fly past each other, one over, one under. The - // whole context stays: the swap is bytes changing, then cards moving. - swap(at, s, rec); - at.walk(s, .over, rec.next(.over, at.moment(s))); + canim.establish(s, .{ ctx.clock, ctx.registers, ctx.stack, ctx.source, ctx.disasm, blocks, joins }); + says.line(s, "`sort` as its blocks: two loops. `j > 0`, so its `jle`\nis not taken, and the inner loop goes on to its compare."); + block(at, s); + says.line(s, "31 > 12: `setg` leaves 1, and `jne` takes the swap."); + block(at, s); + block(at, s); + says.line(s, "The swap and `j--`; then the inner loop turns,\nback up to `j > 0`."); + block(at, s); + + // Pacing: the next trip at a run; then the long middle on the map, at a sprint. + says.line(s, "Once more at a run: 17 > 12, a second swap."); + at.hurry(s, .over, rec.lineMoment(12, rec.call("swap", 1).exit.?), .{ .settle = false }); + says.line(s, "The rest on the map: the gold goes block to block,\nand the counts grow with every trip."); + ctx.map.surface(s); + at.hurry(s, .over, last_trip, .{ .pace = t.sprint, .from = t.run }); + ctx.map.dive(s); - // Pacing: the same again, so at a run; then the long way 5 has to go, - // at a sprint; slowing for the last swap. - says.line(s, "And so on: each value is carried left\nuntil the one before it is not larger."); - at.hurry(s, .over, rec.call("swap", 3).enter - 1, .{ .to = t.run, .settle = false }); - at.hurry(s, .over, last.enter - 1, .{ .pace = t.sprint, .from = t.run }); - says.line(s, "The last swap: 20 goes one place left, past 26."); - swap(at, s, rec); - at.walk(s, .over, end); - says.line(s, "Nothing larger is left of 20. The array is sorted."); + says.line(s, "The last value, 20: 26 > 20, so `jne` takes the swap."); + block(at, s); + block(at, s); + block(at, s); + says.line(s, "The swap, and the inner loop turns once more."); + block(at, s); + says.line(s, "17 > 20 is false: the inner loop is done, and with `i` at 8\nso is the outer. The array is sorted."); + at.hurry(s, .over, rec.call("sort", 0).exit.? - 1, .{}); says.close(s); } |
