From 113e89b389505ade3687c63b1de31a7647cead70 Mon Sep 17 00:00:00 2001 From: Jacob Sandlund Date: Sat, 6 Sep 2025 15:06:00 -0400 Subject: [PATCH] [benchmarks] Use std.mem.doNotOptimizeAway to avoid data collisions --- src/benchmark/CodepointWidth.zig | 20 ++++---------------- src/benchmark/GraphemeBreak.zig | 3 +-- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/src/benchmark/CodepointWidth.zig b/src/benchmark/CodepointWidth.zig index e9207aed5..406603ceb 100644 --- a/src/benchmark/CodepointWidth.zig +++ b/src/benchmark/CodepointWidth.zig @@ -121,11 +121,7 @@ fn stepWcwidth(ptr: *anyopaque) Benchmark.Error!void { const cp_, const consumed = d.next(c); assert(consumed); if (cp_) |cp| { - const width = wcwidth(cp); - - // Write the width to the buffer to avoid it being compiled - // away - buf[0] = @intCast(width); + std.mem.doNotOptimizeAway(wcwidth(cp)); } } } @@ -151,14 +147,10 @@ fn stepTable(ptr: *anyopaque) Benchmark.Error!void { if (cp_) |cp| { // This is the same trick we do in terminal.zig so we // keep it here. - const width = if (cp <= 0xFF) + std.mem.doNotOptimizeAway(if (cp <= 0xFF) 1 else - table.get(@intCast(cp)).width; - - // Write the width to the buffer to avoid it being compiled - // away - buf[0] = @intCast(width); + table.get(@intCast(cp)).width); } } } @@ -182,11 +174,7 @@ fn stepSimd(ptr: *anyopaque) Benchmark.Error!void { const cp_, const consumed = d.next(c); assert(consumed); if (cp_) |cp| { - const width = simd.codepointWidth(cp); - - // Write the width to the buffer to avoid it being compiled - // away - buf[0] = @intCast(width); + std.mem.doNotOptimizeAway(simd.codepointWidth(cp)); } } } diff --git a/src/benchmark/GraphemeBreak.zig b/src/benchmark/GraphemeBreak.zig index 57effebe4..86661f985 100644 --- a/src/benchmark/GraphemeBreak.zig +++ b/src/benchmark/GraphemeBreak.zig @@ -126,8 +126,7 @@ fn stepTable(ptr: *anyopaque) Benchmark.Error!void { const cp_, const consumed = d.next(c); assert(consumed); if (cp_) |cp2| { - const v = unicode.graphemeBreak(cp1, @intCast(cp2), &state); - buf[0] = @intCast(@intFromBool(v)); + std.mem.doNotOptimizeAway(unicode.graphemeBreak(cp1, @intCast(cp2), &state)); cp1 = cp2; } }