From 3aa8ecd9ac930792ffbf683b3450cca9b49bd3e1 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Sat, 18 Oct 2025 02:28:48 +0200 Subject: make fix mtimes multi-threaded MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Robert Günzler --- src/gen.zig | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/gen.zig b/src/gen.zig index 2a01590..485ff01 100644 --- a/src/gen.zig +++ b/src/gen.zig @@ -6,30 +6,39 @@ const fs = @import("fs.zig"); const exe = @import("exe.zig"); pub fn fixMtimes(ctx: *Context) !void { - var buf: [2 * std.os.linux.PATH_MAX]u8 = undefined; - var fba = std.heap.FixedBufferAllocator.init(&buf); - ctx.progress_start("Fix mtimes"); defer ctx.progress_end(); - var counter: usize = 0; - for (ctx.pages.items) |p| { - ctx.progress_start(p); - defer ctx.progress_end(); + const Counter = std.atomic.Value(usize); + var counter = Counter.init(0); - const path = try fs.joinPathAndFilename(fba.allocator(), &.{Context.params.PAGE_PATH}, &.{p}); - const mdpath = try fs.replaceExtension(fba.allocator(), path, "md"); - defer fba.reset(); + const worker = struct { + fn func(wctx: *Context, wp: []const u8, wcounter: *Counter) !void { + var buf: [2 * std.os.linux.PATH_MAX]u8 = undefined; + var fba = std.heap.FixedBufferAllocator.init(&buf); - std.fs.cwd().access(mdpath, .{}) catch { - // only set mtime from git if no markdown source is used - const ts = fs.getGitTime(path) catch continue; - try fs.setMtime(path, ts); - counter += 1; - }; - } + wctx.progress_start(wp); + defer wctx.progress_end(); + + const path = fs.joinPathAndFilename(fba.allocator(), &.{Context.params.PAGE_PATH}, &.{wp}) catch unreachable; + const mdpath = fs.replaceExtension(fba.allocator(), path, "md") catch unreachable; + + std.fs.cwd().access(mdpath, .{}) catch { + // only set mtime from git if no markdown source is used + const ts = fs.getGitTime(path) catch return; + fs.setMtime(path, ts) catch unreachable; + + _ = wcounter.fetchAdd(1, .monotonic); + }; + } + }; + + try exe.WaitPool( + []const u8, + worker.func, + ).work(ctx, ctx.pages.items, .{&counter}); - log.info("Retimed {d} pages.", .{counter}); + log.info("Retimed {d} pages.", .{counter.raw}); } pub fn generate(ctx: *Context) !void { -- cgit 1.4.1