Skip to content

perf: prefix trimming for case-fold literals — Teddy instead of AC#141

Merged
kolkov merged 2 commits into
mainfrom
feature/prefix-trimming
Mar 17, 2026
Merged

perf: prefix trimming for case-fold literals — Teddy instead of AC#141
kolkov merged 2 commits into
mainfrom
feature/prefix-trimming

Conversation

@kolkov

@kolkov kolkov commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Prefix trimming for case-fold expanded literals — when (?i) expansion produces >32 incomplete prefix literals, trims to 4-byte prefixes and deduplicates. Reduces to ~17 literals that fit Teddy SIMD instead of slower Aho-Corasick.

LangArena suspicious ((?i)etc/passwd|wp-admin|\.\./): 117ms → 10.9ms (10.7x faster, 1.8x from Rust, was 54x).

Verified: Rust uses identical optimization (optimize_for_prefix_by_preference) producing same 17 four-byte prefixes → Teddy.

Test plan

  • Full test suite passes
  • Lint clean
  • Match counts correct (10473 matches)
  • Rust strategy comparison via RUST_LOG=debug
  • CI passes
  • regex-bench validation

kolkov added 2 commits March 17, 2026 23:42
When literal extraction produces >32 incomplete prefix literals (e.g.,
225 case-fold expanded variants), trim to 4-byte prefixes and deduplicate.
This reduces literal count to fit Teddy (≤32 patterns, SIMD fast) instead
of falling through to Aho-Corasick (slower sequential scan).

Skip for AllComplete literals (exact matches) — those go to Teddy/AC directly.

LangArena suspicious pattern:
- Before: 225 literals → AC prefilter → 117ms
- After: 17 literals → Teddy SIMD → 10.9ms (10.7x faster, 1.8x from Rust)
@github-actions

Copy link
Copy Markdown

Benchmark Comparison

Comparing main → PR #141

Summary: geomean 117.7n 117.9n +0.14%

⚠️ Potential regressions detected:

Accelerate/memchr1-4       108.3n ± ∞ ¹   109.2n ± ∞ ¹  +0.83% (p=0.008 n=5)
Accelerate/memchr2-4       209.6n ± ∞ ¹   213.2n ± ∞ ¹  +1.72% (p=0.040 n=5)
geomean                    117.7n         117.9n        +0.14%
geomean                               ³                +0.00%               ³
geomean                               ³                +0.00%               ³
geomean                         ³                +0.00%               ³
geomean                         ³                +0.00%               ³
AhoCorasickManyPatterns/coregex_10_patterns-4           57.40n ± ∞ ¹    59.85n ± ∞ ¹     +4.27% (p=0.032 n=5)
MatchAnchoredLiteral/short_match-4                      6.554n ± ∞ ¹    7.225n ± ∞ ¹    +10.24% (p=0.008 n=5)
MatchAnchoredLiteral/no_match_suffix-4                  4.059n ± ∞ ¹    4.370n ± ∞ ¹     +7.66% (p=0.008 n=5)

Full results available in workflow artifacts. CI runners have ~10-20% variance.
For accurate benchmarks, run locally: ./scripts/bench.sh --compare

@kolkov kolkov merged commit ee2d823 into main Mar 17, 2026
8 checks passed
@codecov

codecov Bot commented Mar 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@kolkov kolkov deleted the feature/prefix-trimming branch March 18, 2026 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant