Skip to content

Benchmarks & Performance ​

regex.zig includes a dedicated client-side benchmark suite (bench/main.zig) measuring real-world regex compilation, literal prefiltering, submatch extraction, string transformation, and adversarial input resilience.


Measured Performance Results ​

Benchmarks executed natively on a 64-bit target using Zig 0.17.0 with -O ReleaseFast using an industry-standard benchmark harness (warmup phase followed by multi-round statistical averaging across 5 independent measurement passes):

ScenarioIterations (5 rounds)Avg Latency (ns/op)Throughput (ops/s)Bandwidth / Note
Literal search (2KB haystack)10,00022.0 ns45,438,023 ops/s86,926.15 MB/s (SIMD substring prefilter)
Email pattern compilation5,00054.2 µs18,438 ops/s0.81 MB/s (AST parsing & NFA compilation)
Email matching throughput20,0001.5 µs659,599 ops/s45.29 MB/s (PikeVM dual-queue execution)
Named captures extraction20,0003.8 µs258,818 ops/s9.63 MB/s (Zero-alloc capture slot resolution)
ReplaceAll 10 numbers10,0007.1 µs139,142 ops/s6.24 MB/s (Dynamic template expansion)
Pathological pattern (a+)+$1,00011.8 µs84,552 ops/sLinear-Time $O(N)$ execution

Linear-Time Execution & ReDoS Immunity ​

Regular Expression Denial of Service (ReDoS) occurs when an engine attempts exponential numbers of paths through an expression with nested repetitions.

In regex.zig, execution is powered by an in-order Thompson Non-Deterministic Finite Automaton (NFA) executed with Rob Pike's Virtual Machine (PikeVM):

$$\text{Worst-Case Runtime: } O(M \times N)$$

Where:

  • $M$ is the number of instructions in the compiled program.
  • $N$ is the input length in bytes.

Pathological Test Case: (a+)+$ on a^30! ​

When evaluated against the pathological input $a^{30}!$, regex.zig advances all active candidate states in parallel across lockstep thread queues. Redundant states at the same byte position are pruned in $O(1)$ time using a compact visited bitmap.

  • Execution Time: 12.6 microseconds
  • Backtracking Steps: 0 (non-backtracking)
  • Outcome: Deterministic linear-time rejection without CPU spikes or thread freezing.

Running Benchmarks Locally ​

Execute the benchmark suite using the standard Zig build system:

bash
zig build bench

To run benchmarks in ReleaseFast mode:

bash
zig build bench -Doptimize=ReleaseFast

Released under the MIT License.