From db87b47e6f0727f158e6395e685d946506236c4c Mon Sep 17 00:00:00 2001 From: peterino2 Date: Mon, 2 Nov 2026 06:37:44 -0800 Subject: [PATCH] dxcapi: keep each thread's compile arena between calls Freeing the arena after every Compile gave its blocks back to the OS (smp_allocator maps 64 KiB and up directly), so each call faulted about 4 MB of fresh pages in again. On the Glide corpus's 8,770 compiles that was 9.06M page faults and up to 28 s of kernel time; with the arena kept (reset to at most 64 MiB) it is 0.4-1.0M, and the replay is 25% faster on 1 thread, 30% on 8 and 16% on 24. Output is byte-identical. --- src/dxcapi.zig | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/dxcapi.zig b/src/dxcapi.zig index 0572e41..ab2618f 100644 --- a/src/dxcapi.zig +++ b/src/dxcapi.zig @@ -665,7 +665,7 @@ fn makeResult(c: compile.Result) !*Result { try addOutput(r, .remarks, try Blob.utf8(""), null); std.mem.sortUnstable(Output, r.outputs.items, {}, struct { fn lt(_: void, x: Output, y: Output) bool { - return @intFromEnum(x.kind) < @intFromEnum(y.kind); + return @backingInt(x.kind) < @backingInt(y.kind); } }.lt); return r; @@ -826,9 +826,17 @@ const Compiler = struct { } }; +/// Each thread's compile arena, kept between calls. Freeing it per call +/// gives its blocks back to the OS (smp_allocator maps 64 KiB and up +/// directly), and faulting fresh pages in again costs more than the compile +/// on many threads. What a thread keeps is capped at `retain_limit`. +threadlocal var thread_arena: ?std.heap.ArenaAllocator = null; +const retain_limit = 64 << 20; + fn run(source: *const Buffer, wargs: []const ?[*:0]const WCHAR, include: ?*anyopaque) error{ OutOfMemory, InvalidArgument }!*Result { - var arena: std.heap.ArenaAllocator = .init(gpa); - defer arena.deinit(); + if (thread_arena == null) thread_arena = .init(gpa); + const arena = &thread_arena.?; + defer _ = arena.reset(.{ .retain_with_limit = retain_limit }); const a = arena.allocator(); const args = try a.alloc([]const u8, wargs.len); for (args, wargs) |*d, w| d.* = try fromWide(a, w orelse return error.InvalidArgument);