ReviewOS

also looking at this

zig-utils/zig-regex

fix(vm): make findAll linear by stopping matchAt when no threads remain

#8
Merged DaliVana wants to merge fix/findall-quadratic into main
3 files +102 -0
9 days ago

Fixes: https://github.com/zig-utils/zig-regex/issues/6

matchAt seeds only the start-state thread, yet after a match completed and all threads died it kept iterating to input.len doing nothing. findAll restarts matchAt per match, so that dead-walk was paid Σ(n − posᵢ) ≈ O(n²): a 64 KiB scan with a match every 64 bytes took ~6.5 s. This also made findIter/replaceAll/split unusable past a few KB and contradicted the documented O(n×m) guarantee.

Break out of the matchAt loop as soon as the live thread set is empty; no further match can begin from this start_pos, and any accept on the consumed input was already recorded. Restores O(n·states) end to end.

Reproduction (issue), ReleaseFast: 16→7.7ms 32→15.4 64→30.8 128→56.3 256→119ms — ~2× per doubling (was ~4×). Match counts unchanged.

Adds a build-mode-independent regression test (time ratio of input vs 2×input must stay sub-3×); links libc for tests/regression.zig to get a monotonic clock (Zig 0.16 std.time has none).

3 changed files on the files tab, with 0 review threads.

Sign in to comment on this pull request.