[benchmarks] Use std.mem.doNotOptimizeAway to avoid data collisions

pull/8548/head
Jacob Sandlund 2025-09-06 15:06:00 -04:00
parent 26e9b0a0f3
commit 113e89b389
2 changed files with 5 additions and 18 deletions

View File

@ -121,11 +121,7 @@ fn stepWcwidth(ptr: *anyopaque) Benchmark.Error!void {
const cp_, const consumed = d.next(c); const cp_, const consumed = d.next(c);
assert(consumed); assert(consumed);
if (cp_) |cp| { if (cp_) |cp| {
const width = wcwidth(cp); std.mem.doNotOptimizeAway(wcwidth(cp));
// Write the width to the buffer to avoid it being compiled
// away
buf[0] = @intCast(width);
} }
} }
} }
@ -151,14 +147,10 @@ fn stepTable(ptr: *anyopaque) Benchmark.Error!void {
if (cp_) |cp| { if (cp_) |cp| {
// This is the same trick we do in terminal.zig so we // This is the same trick we do in terminal.zig so we
// keep it here. // keep it here.
const width = if (cp <= 0xFF) std.mem.doNotOptimizeAway(if (cp <= 0xFF)
1 1
else else
table.get(@intCast(cp)).width; table.get(@intCast(cp)).width);
// Write the width to the buffer to avoid it being compiled
// away
buf[0] = @intCast(width);
} }
} }
} }
@ -182,11 +174,7 @@ fn stepSimd(ptr: *anyopaque) Benchmark.Error!void {
const cp_, const consumed = d.next(c); const cp_, const consumed = d.next(c);
assert(consumed); assert(consumed);
if (cp_) |cp| { if (cp_) |cp| {
const width = simd.codepointWidth(cp); std.mem.doNotOptimizeAway(simd.codepointWidth(cp));
// Write the width to the buffer to avoid it being compiled
// away
buf[0] = @intCast(width);
} }
} }
} }

View File

@ -126,8 +126,7 @@ fn stepTable(ptr: *anyopaque) Benchmark.Error!void {
const cp_, const consumed = d.next(c); const cp_, const consumed = d.next(c);
assert(consumed); assert(consumed);
if (cp_) |cp2| { if (cp_) |cp2| {
const v = unicode.graphemeBreak(cp1, @intCast(cp2), &state); std.mem.doNotOptimizeAway(unicode.graphemeBreak(cp1, @intCast(cp2), &state));
buf[0] = @intCast(@intFromBool(v));
cp1 = cp2; cp1 = cp2;
} }
} }