Add Criterion Benchmarks to Track Performance #19

Closed
opened 2026-03-11 18:14:10 +03:00 by NiXTheDev · 0 comments
NiXTheDev commented 2026-03-11 18:14:10 +03:00 (Migrated from github.com)

Goal: Establish performance baselines and catch regressions early.

Create a benches/ directory with benchmarks using the criterion crate. Measure key operations:

  • Compilation time for various patterns.
  • Matching time for simple literals, alternations, character classes, backreferences.
  • Replacement speed.
  • Transpilation speed.

Implementation Steps:

  1. Add criterion as a dev-dependency.
  2. Create benches/compile.rs, benches/match.rs, etc.
  3. Write benchmarks that use black_box to prevent over-optimization.
  4. Run cargo bench to generate reports.

Example:

use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn bench_compile(c: &mut Criterion) {
    c.bench_function("compile simple literal", |b| {
        b.iter(|| Regex::new(black_box("abc")).unwrap())
    });
}

criterion_group!(benches, bench_compile);
criterion_main!(benches);

Benefits:

  • Quantify the impact of optimizations.
  • Prevent accidental slowdowns.
**Goal**: Establish performance baselines and catch regressions early. Create a `benches/` directory with benchmarks using the `criterion` crate. Measure key operations: - Compilation time for various patterns. - Matching time for simple literals, alternations, character classes, backreferences. - Replacement speed. - Transpilation speed. **Implementation Steps**: 1. Add `criterion` as a dev-dependency. 2. Create `benches/compile.rs`, `benches/match.rs`, etc. 3. Write benchmarks that use `black_box` to prevent over-optimization. 4. Run `cargo bench` to generate reports. **Example**: ```rust use criterion::{black_box, criterion_group, criterion_main, Criterion}; fn bench_compile(c: &mut Criterion) { c.bench_function("compile simple literal", |b| { b.iter(|| Regex::new(black_box("abc")).unwrap()) }); } criterion_group!(benches, bench_compile); criterion_main!(benches); ``` **Benefits**: - Quantify the impact of optimizations. - Prevent accidental slowdowns.
Sign in to join this conversation.
No description provided.