diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 06:08:57 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | f923d0342f7fbbc965867c19e03cb6c94fcb7fd3 (patch) | |
| tree | 5893aafa1253d4a49c61aab97fdacb9020cd8dbb /test/lib | |
| parent | 56274a68fb7a418625f01d479ced9fbfc19383f2 (diff) | |
| download | pardes-f923d0342f7fbbc965867c19e03cb6c94fcb7fd3.tar.gz pardes-f923d0342f7fbbc965867c19e03cb6c94fcb7fd3.zip | |
hxlib: hxdiff's cases on the library, against helix's goldens and against the file pane (docs/next-editor.md, R6): test/hxlib.zig types each case into a `pardes-editor` value and prints its result as hxdiff prints a pane's, doing what an app does with the s, S, K and Alt-K it is handed back (the keys to <ret> its pattern, <esc> no answer) and printing a case that reaches any other action (a view, n and N, a macro, gw) as the caller's; hxdiff takes another driver's results (`--actual=`), and `zig build hxlib` (`-- --parity`) holds them to the goldens or the file pane; the key notation is test/hxkeys.zig, which both harnesses read; what the cases found, fixed: paneCursorLines gave the library's body no lines (mm, mi, ma, md, mr, insert-mode arrows), and the library's selectRegex keeps its pattern in `/` as pardes's prompt does (look.submitSearch); a page key in insert mode moves a view and is the caller's; test/lib's `zig build bench -- <KiB> <keys>`: a key's cost, the pages it changes, a parse; hxlib 537 of 658 compared with 0 mismatches (5 waived as for pardes, 121 the caller's), --parity 617 of 738 with 0 mismatches and 0 waived; hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, core-test 811 of 950, shell-test 990 of 993, replicas pass, lib-test pass
Diffstat (limited to 'test/lib')
| -rw-r--r-- | test/lib/bench.zig | 80 | ||||
| -rw-r--r-- | test/lib/build.zig | 10 |
2 files changed, 90 insertions, 0 deletions
diff --git a/test/lib/bench.zig b/test/lib/bench.zig new file mode 100644 index 00000000..f2127dc4 --- /dev/null +++ b/test/lib/bench.zig @@ -0,0 +1,80 @@ +//! What a key costs on the library (`zig build bench -- <KiB> <keys>`, Debug +//! or -Doptimize): a text of that size in the C grammar's shape, a key +//! typed into its middle `keys` times, each with an arena of its own; the +//! 4 KiB pages of the value a typed character changes; a full parse of the +//! text. `keys` 0 makes the text alone, to count what the keys add; a +//! third argument, `pages`, counts the pages (a copy and a compare a key, +//! which an instruction count of the keys leaves out). +const std = @import("std"); +const pe = @import("pardes-editor"); + +const Ed = pe.Editor(.{ .text = 128 * 1024, .undo = 512 * 1024 }); + +extern fn tree_sitter_c() *const pe.syntax.ts.Language; + +fn now() u64 { + var ts: std.c.timespec = undefined; + _ = std.c.clock_gettime(.MONOTONIC, &ts); + return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec)); +} + +pub fn main(init: std.process.Init) !void { + const gpa = std.heap.page_allocator; + const args = try init.minimal.args.toSlice(init.arena.allocator()); + const kib = try std.fmt.parseInt(usize, args[1], 10); + const keys = try std.fmt.parseInt(usize, args[2], 10); + const count_pages = args.len > 3; + var text: std.ArrayList(u8) = .empty; + var i: usize = 0; + while (text.items.len < kib * 1024) : (i += 1) try text.print(gpa, "int f{d}(int x) {{ return x + {d}; }}\n", .{ i, i }); + text.items.len = kib * 1024; + const ed = try gpa.create(Ed); + ed.* = .{}; + try ed.load(text.items); + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + // to the middle, into insert mode + for ([_]pe.Key{ .{ .cp = '%', .text = "%" }, .{ .cp = ';', .text = ";" } }) |k| _ = ed.key(arena.allocator(), k); + var to_middle: [8]u8 = undefined; + for (std.fmt.bufPrint(&to_middle, "{d}G", .{text.items.len / 2 / 32}) catch unreachable) |c| _ = ed.key(arena.allocator(), .{ .cp = c, .text = &.{c} }); + _ = ed.key(arena.allocator(), .{ .cp = 'i', .text = "i" }); + _ = arena.reset(.free_all); + if (keys == 0) return; + + const before = try gpa.create(Ed); + var pages_max: usize = 0; + var pages_sum: usize = 0; + var key_ns: u64 = 0; + for (0..keys) |n| { + if (count_pages) before.* = ed.*; + const t0 = now(); + const out = ed.key(arena.allocator(), .{ .cp = 'a' + @as(u21, @intCast(n % 26)), .text = &.{'a' + @as(u8, @intCast(n % 26))} }); + key_ns += now() - t0; + if (out != .done) return error.NotTyped; + _ = arena.reset(.retain_capacity); + if (!count_pages) continue; + const a = std.mem.asBytes(before); + const b = std.mem.asBytes(ed); + var pages: usize = 0; + var at: usize = 0; + while (at < a.len) : (at += 4096) { + const end = @min(at + 4096, a.len); + if (!std.mem.eql(u8, a[at..end], b[at..end])) pages += 1; + } + pages_max = @max(pages_max, pages); + pages_sum += pages; + } + const per_key = key_ns / keys; + + pe.syntax.start(std.heap.smp_allocator); // tree-sitter mallocs: not a page each + const lang: pe.Language = .{ .name = "c-as-dsl", .ext = ".cdsl", .ts = tree_sitter_c() }; + var syn: pe.Syntax = .{}; + // the parser and its queries made once, then the parse alone timed + _ = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; + syn.tree.?.destroy(); + syn.tree = null; + const p0 = now(); + _ = syn.parse(arena.allocator(), ed, lang) orelse return error.NoTree; + const parse = now() - p0; + std.debug.print("{d} KiB: {d} keys, {d} ns a key, pages changed a key {d} max {d:.2} mean, value {d} bytes, full parse {d} ns\n", .{ kib, keys, per_key, pages_max, @as(f64, @floatFromInt(pages_sum)) / @as(f64, @floatFromInt(keys)), @sizeOf(Ed), parse }); +} diff --git a/test/lib/build.zig b/test/lib/build.zig index 6f590eda..436517d4 100644 --- a/test/lib/build.zig +++ b/test/lib/build.zig @@ -21,4 +21,14 @@ pub fn build(b: *std.Build) void { }) }); step.dependOn(&b.addRunArtifact(tests).step); } + // What a key costs, a page count and a parse: `zig build bench -- <KiB> <keys>`. + const bench = b.addExecutable(.{ .name = "bench", .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = b.path("bench.zig"), + .imports = &.{.{ .name = "pardes-editor", .module = pardes.module("pardes-editor") }}, + }) }); + const run_bench = b.addRunArtifact(bench); + run_bench.addPassthruArgs(); + b.step("bench", "a key typed into the library's editor at a size, its pages and a parse").dependOn(&run_bench.step); } |
