perf: eliminate hot-path allocations in five rules#433
Open
jeduden wants to merge 1 commit into
Open
Conversation
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 Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files
☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Audited the codebase against
docs/development/high-performance-go.mdusing 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[]byteon every line of every directive body; now converted once before the outer AST walk and passed tobytes.Containsinstead ofstrings.Contains(string(...)).firstlineheading/rule.go: Remove the unconditionalfmt.Sprintfthat built the "missing heading" message on every file check, even for passing files (1 alloc per workspace file). Replaced withstrconv.Itoa+ concatenation, deferred to the error sites only.githooksync/rule.go: Replacestrings.Contains(string(data), marker)withbytes.Contains(data, preMergeMarkerBytes).datais already[]bytefromos.ReadFile; the previous code allocated a copy of the entire hook file as a string.preMergeMarkerBytesis a package-level var to avoid re-converting the constant each call.linkvalidity/rule.go: Add abytes.Contains('(')guard beforereversedRe.FindAllSubmatchIndex. Reversed links always start with(; this guard skips the regex NFA walk on lines without that byte — the common case.headingincrement/rule.go: Replacefmt.Sprintfwithstrconv.Itoa+ string concatenation in the two diagnostic message sites.fmt.Sprintfuses reflection;strconv.Itoais ~3× faster.Test plan
go build ./...— cleango test ./...— all packages passgo test ./internal/integration/...— integration suite passesgo run ./cmd/mdsmith check .— 388 files checked, 0 failureshttps://claude.ai/code/session_01QLP2AGDLaojzi8TFpPDLr8
Generated by Claude Code