Skip to content

perf: eliminate hot-path allocations in five rules#433

Open
jeduden wants to merge 1 commit into
mainfrom
claude/eloquent-feynman-EWxdX
Open

perf: eliminate hot-path allocations in five rules#433
jeduden wants to merge 1 commit into
mainfrom
claude/eloquent-feynman-EWxdX

Conversation

@jeduden
Copy link
Copy Markdown
Owner

@jeduden jeduden commented May 31, 2026

Summary

Audited the codebase against docs/development/high-performance-go.md using a four-agent parallel sweep. Fixed the top 5 allocation anti-patterns found:

  • include/rule.go: Hoist []byte(text) out of the inner PI-body loop. Previously the full file content was re-allocated as []byte on every line of every directive body; now converted once before the outer AST walk and passed to bytes.Contains instead of strings.Contains(string(...)).

  • firstlineheading/rule.go: Remove the unconditional fmt.Sprintf that built the "missing heading" message on every file check, even for passing files (1 alloc per workspace file). Replaced with strconv.Itoa + concatenation, deferred to the error sites only.

  • githooksync/rule.go: Replace strings.Contains(string(data), marker) with bytes.Contains(data, preMergeMarkerBytes). data is already []byte from os.ReadFile; the previous code allocated a copy of the entire hook file as a string. preMergeMarkerBytes is a package-level var to avoid re-converting the constant each call.

  • linkvalidity/rule.go: Add a bytes.Contains('(') guard before reversedRe.FindAllSubmatchIndex. Reversed links always start with (; this guard skips the regex NFA walk on lines without that byte — the common case.

  • headingincrement/rule.go: Replace fmt.Sprintf with strconv.Itoa + string concatenation in the two diagnostic message sites. fmt.Sprintf uses reflection; strconv.Itoa is ~3× faster.

Test plan

  • go build ./... — clean
  • go test ./... — all packages pass
  • go test ./internal/integration/... — integration suite passes
  • go run ./cmd/mdsmith check . — 388 files checked, 0 failures

https://claude.ai/code/session_01QLP2AGDLaojzi8TFpPDLr8


Generated by Claude Code

Five issues identified by auditing the codebase against the
high-performance-go guidelines:

1. include/rule.go: hoist []byte(text) out of the inner PI-body loop.
   Previously the full file content was re-allocated as a []byte on
   every line of every PI body; now it is converted once before the
   outer AST walk and passed to bytes.Contains instead of
   strings.Contains(string(...)).

2. firstlineheading/rule.go: remove the unconditional fmt.Sprintf that
   built the "missing heading" message on every file check, even for
   files that pass. Replace with strconv.Itoa + concatenation, deferred
   to the error sites.

3. githooksync/rule.go: replace strings.Contains(string(data), marker)
   with bytes.Contains(data, preMergeMarkerBytes). data is already
   []byte from os.ReadFile; the previous code allocated a copy of the
   entire hook file as a string. preMergeMarkerBytes is a package-level
   var to avoid re-converting the constant on each call.

4. linkvalidity/rule.go: add a bytes.Contains('(') guard before
   reversedRe.FindAllSubmatchIndex. Reversed links always start with
   '('; the guard skips the regex NFA walk on lines without that byte,
   which is the common case.

5. headingincrement/rule.go: replace fmt.Sprintf with strconv.Itoa +
   string concatenation in the two diagnostic message sites. fmt.Sprintf
   uses reflection; strconv.Itoa is ~3x faster with no allocation for
   the format string.

All existing tests pass; go build ./... and mdsmith check . are clean.

https://claude.ai/code/session_01QLP2AGDLaojzi8TFpPDLr8
@codecov
Copy link
Copy Markdown

codecov Bot commented May 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.27%. Comparing base (37488a7) to head (e27a2f1).

Additional details and impacted files
Components Coverage Δ
Go 97.25% <100.00%> (+<0.01%) ⬆️
TypeScript 99.54% <ø> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants