Skip to content

mr starts 18x faster: regexps compile on first use (#8) - #66

Merged
Cd1s merged 1 commit into
mainfrom
perf/lazy-regexp
Sep 25, 2026
Merged

Cd1s merged 1 commit into
mainfrom
perf/lazy-regexp

Conversation

@Cd1s

@Cd1s Cd1s commented Sep 25, 2026

Copy link
Copy Markdown
Owner

What

  • mr/lazyre.go: lazyRegexp(pattern) compiles on first use (sync.Once) and provides the methods the code uses (MatchString, FindStringSubmatch, FindSubmatch, …). All 47 package-level regexp.MustCompile become lazyRegexp.
  • A test compiles every registered pattern (a broken pattern would otherwise only show when its code path runs).
  • CI measures GODEBUG=inittrace=1 mr version and fails if package main's init allocates 256 KiB or more — a new eager package-level MustCompile is caught.

Why

mr starts for every web UI request (CGI) and every hook. Its init compiled every pattern each time, although a call uses few of them — including ones with large counted repetitions that expand into long programs.

init main (x86, build host) before after
time 3.2 ms 0.18 ms
allocated 2,477,384 bytes 39,696 bytes
allocations 33,442 289

The router's Cortex-A53 is several times slower than the build host, so the saving per request is larger there.

Verification

Measured before / after on the build host (numbers above); tools/ci.sh passes (private and public trees) with the new init-cost check.

Closes #8

https://claude.ai/code/session_01HWeUjo1dmS7gBkCYHrVjCr

init of package main compiled all 47 package-level patterns on every start (every CGI request and
hook): 3.2 ms, 2.48 MB, 33,442 allocations on x86. lazyRegexp compiles on first use; now 0.18 ms,
39,696 bytes, 289 allocations. A test compiles every pattern; CI fails if init allocates >= 256 KiB.

Closes #8
@Cd1s
Cd1s merged commit 823c01d into main Sep 25, 2026
2 checks passed
@Cd1s
Cd1s deleted the perf/lazy-regexp branch September 25, 2026 18:27
@Cd1s Cd1s mentioned this pull request Sep 25, 2026
52 tasks done
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.

性能:mr 启动时编译全部正则,每次调用约 2.45 MB / 3 ms

1 participant