about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2025-10-18 02:28:48 +0200
committerRobert Günzler <r@gnzler.io>2025-10-18 03:18:14 +0200
commit3aa8ecd9ac930792ffbf683b3450cca9b49bd3e1 (patch)
tree13aaaf20ecb1167b13d8a7a39bf8be5d9536177c
parent4a1a309d90e4161d65e93854df523e7308b028a6 (diff)
downloadsite-3aa8ecd9ac930792ffbf683b3450cca9b49bd3e1.tar.gz
make fix mtimes multi-threaded
Signed-off-by: Robert Günzler <r@gnzler.io>
-rw-r--r--src/gen.zig45
1 files 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 {