From f57897b1d8eca7080ba2482a6ed20e1c9fa7d453 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 15:57:57 -0300 Subject: regexp: one step budget in every build mode, 32M: what a search finds is the core's state, and with 4M in Debug a Debug replica of a ReleaseSafe session answered TooSlow where its session found no match (`\s*(\w+)\s*=` over 200 letters, the variant hunt's test, here) Co-Authored-By: Claude Opus 5.5 --- src/regexp.zig | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/regexp.zig b/src/regexp.zig index 6d281859..6c858dea 100644 --- a/src/regexp.zig +++ b/src/regexp.zig @@ -2,7 +2,6 @@ //! sam searches (editors/acme/regx.c). `addr` (src/ninep/addr.zig) and //! normal mode's `s` and `S` (src/normal.zig) both call it. const std = @import("std"); -const builtin = @import("builtin"); const mvzr = @import("mvzr"); /// A compiled pattern and how to run it. @@ -57,8 +56,11 @@ pub const Regex = struct { end_hint: struct { ptr: usize = 0, len: usize = 0, start: usize = 0, hi: usize = 0, end: usize = 0 } = .{}, /// Timed on this machine's ReleaseSafe build: 10M steps of the worst - /// patterns take 45-80 ms, and a Debug build is ten times slower. - pub const budget: u64 = if (builtin.mode == .debug) 4_000_000 else 32_000_000; + /// patterns take 45-80 ms. One budget in every build mode: what a + /// search finds is the core's state, and a Debug replica of a + /// ReleaseSafe session must find what the session does (a Debug build + /// is ten times slower at it, about two seconds). + pub const budget: u64 = 32_000_000; pub const e_long = std.fmt.comptimePrint("bad regular expression: longer than mvzr's {d} operations (about {d} pattern characters)", .{ max_ops, max_ops }); pub const e_anchor = "bad regular expression: in a pattern with \\n, ^ can only come first and $ only just before a \\n"; @@ -555,6 +557,16 @@ test "a search that would backtrack without end runs out of budget, promptly" { try std.testing.expectError(error.TooSlow, chain.find(&line, 0, line.len, line.len)); } +test "a search's answer is every build mode's" { + // `\s*(\w+)\s*=` over a line of letters costs more than linear: over + // 200 of them a Debug build's old budget (4M steps) ran out, TooSlow, + // where a release build's (32M) answered no match, so a Debug replica + // of a ReleaseSafe session diverged from it on one search. + var line: [200]u8 = @splat('a'); + var rx = try Regex.compile("\\s*(\\w+)\\s*="); + try std.testing.expectEqual(@as(?Regex.Match, null), try rx.find(&line, 0, line.len, line.len)); +} + test "the match is the leftmost, however long the line" { // A window cut into the identifier would find `x... =` from its middle. const text = "let " ++ &@as([100]u8, @splat('x')) ++ " = 1"; -- cgit v1.3