Skip to content

perf: hoist regex compilation, pre-allocate slices#64

Open
marcus wants to merge 1 commit intomainfrom
perf/hoist-regex-preallocate
Open

perf: hoist regex compilation, pre-allocate slices#64
marcus wants to merge 1 commit intomainfrom
perf/hoist-regex-preallocate

Conversation

@marcus
Copy link
Owner

@marcus marcus commented Mar 20, 2026

Summary

  • Hoist 12 regexp.MustCompile calls from function bodies to package-level vars across tmux/scraper.go (8), claudemd.go (2), and agentsmd.go (2) — eliminates repeated compilation on every scrape cycle
  • Pre-allocate slices with capacity hints in 3 hot paths (trends/analyzer.go, reporting/run_report.go, stats/stats.go)
  • Add benchmark tests to guard against future regressions

Benchmark results (Apple M4 Pro, arm64)

Scraper parse functions (after hoisting)

BenchmarkParseClaudeWeeklyPct   ~600k     ~2–6.5µs/op    689 B/op    4 allocs/op
BenchmarkParseCodexWeeklyPct    ~350k     ~5.6–5.9µs/op  641 B/op    5 allocs/op
BenchmarkParseClaudeResetTimes  ~77–100k  ~12.7–15.4µs/op 721 B/op   5 allocs/op
BenchmarkParseCodexResetTimes   ~140–159k ~7.8–8.2µs/op  593 B/op    5 allocs/op

Integration MD parsers (after hoisting)

BenchmarkParseClaudeMD   ~58–122k   ~8.6–20.4µs/op  5471 B/op  32 allocs/op
BenchmarkParseAgentsMD   ~136–175k  ~7.0–7.8µs/op   6937 B/op  53 allocs/op

Test plan

  • go vet passes on all modified packages
  • go build ./... succeeds
  • Existing tests pass for all modified packages
  • New benchmarks run successfully

🤖 Generated with Claude Code


Automated by nightshift

…ices

Move 12 regexp.MustCompile calls from function bodies to package-level
vars in tmux/scraper.go (8), claudemd.go (2), and agentsmd.go (2).
This eliminates repeated regex compilation on every scrape cycle.

Pre-allocate slices with capacity hints in 3 hot paths:
- trends/analyzer.go: hourlyAverage slice with cap 24
- reporting/run_report.go: task result slices with cap len(tasks)
- stats/stats.go: report results slice with cap len(entries)

Add benchmark tests for parse functions and MD parsers.

Nightshift-Task: perf-regression
Nightshift-Ref: https://github.com/marcus/nightshift

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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