Skip to content

perf: audit against high-performance-go.md, fix 3 confirmed hot paths - #767

Merged
jeduden merged 3 commits into
mainfrom
claude/kind-darwin-zn04bd
Jul 25, 2026
Merged

perf: audit against high-performance-go.md, fix 3 confirmed hot paths#767
jeduden merged 3 commits into
mainfrom
claude/kind-darwin-zn04bd

Conversation

@jeduden

@jeduden jeduden commented Jul 24, 2026

Copy link
Copy Markdown
Owner

Summary

Audited the Go core against docs/development/high-performance-go.md using a fleet of scanning agents plus CPU/alloc profiling of BenchmarkCheckCorpusLarge (per the doc's own "profile before you rewrite" methodology). Most textbook anti-patterns the doc calls out (regex compiled per-call, missing pre-sized slices, redundant AST walks, []T{} instead of nil, ToLower/ToUpper allocations) do not survive measurement in this codebase — it already went through a dedicated optimization pass (the "plan 195" / "plan 2606…" comments throughout internal/). Several promising-looking leads were implemented, benchmarked, and then reverted when they showed zero measurable improvement (e.g. converting ast.Walk closures to direct recursion — Go's escape analysis already keeps the walker off the heap here).

Three issues did survive rigorous red/green verification (failing benchmark/test against the pre-fix code, passing after) and are fixed here, ranked by measured impact:

  1. no-reference-style (MDS043) — missing byte-needle gate before two full-source regex scans. checkFootnotes ran footnoteRefRE/footnoteDefRE over the entire source on every Check, even when the file has no footnote syntax at all. Added the same bytes.Contains(source, "[^") pre-check the doc's own example (mayContainURL in MDS012) already uses. Measured: ~253,800 ns/op → ~4,500 ns/op (~56x) on a footnote-free fixture.
  2. build (MDS039) — strings.ToUpper allocation on every path segment. hasReservedDeviceName uppercased every segment before a set lookup; ToUpper only allocates when the input isn't already the target case, so real (lowercase) segments always paid for it. Every reserved name is 3–4 bytes, so a length pre-check skips the allocation for segments that can never match. Measured: 602 ns/op & 10 allocs/op → ~380 ns/op & 5 allocs/op.
  3. git-hook-sync (MDS048) — redundant git rev-parse subprocess. driftParts resolved the hooks directory twice on a cache miss (once in peekHookSource, again in preMergeCommitHookDrift), each spawning a real OS subprocess — the exact "never exec a subprocess per item" anti-pattern the doc names. Resolved once and threaded through both helpers.

Each fix has a dedicated regression test/benchmark with a pinned budget (b.Fatalf/t.Fatalf on overshoot), verified to fail against the pre-fix code and pass after.

Test plan

  • go build ./...
  • go test ./... (full suite green)
  • go vet ./...
  • go tool -modfile=tools/go.mod golangci-lint run on changed packages (0 issues)
  • internal/integration alloc-budget and rule-walk-audit manifest gates pass unchanged
  • Each fix's new test/benchmark confirmed red against the pre-fix code, green after (shown via git stash/git apply round-trips during development)
  • mdsmith check . on the repo (563 files, 0 failures)

Co-Authored-By: Claude Sonnet 5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_016wWQmrtb8EMbn1hDgkESqE


Generated by Claude Code

@codecov

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.71%. Comparing base (47ace27) to head (e8d7647).
⚠️ Report is 14 commits behind head on main.

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

☔ View full report in Codecov by Harness.
📢 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.

@jeduden jeduden added queue Add to a PR to enqueue it queue:active Applied automatically when a PR is in an active batch and removed queue Add to a PR to enqueue it labels Jul 25, 2026
@jeduden

jeduden commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

🟢 Merge Queue — picked up

This PR is in the queue and will be batched with other queue-labelled PRs.

Next: No action needed — you'll get another comment when CI starts on the batch. View merge queue run.

@jeduden jeduden added queue:failed Applied automatically when CI fails or merge conflict occurs and removed queue:active Applied automatically when a PR is in an active batch labels Jul 25, 2026
@jeduden

jeduden commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

⚠️ Merge Queue — merge conflict

This PR could not be merged into the batch branch without conflicts with main or another queued PR.

Next: Rebase onto or merge main into your branch, resolve conflicts, push, then re-add the queue label.

claude added 3 commits July 25, 2026 19:17
checkFootnotes ran footnoteRefRE and footnoteDefRE over the full source
on every Check call, even on files with no footnote syntax at all. Per
docs/development/high-performance-go.md ("Gate expensive analyzers
behind a cheap pre-check... byte-needles gate regex paths"), add a
bytes.Contains(f.Source, "[^") pre-check mirroring MDS012's
mayContainURL: every match either regex could produce requires that
literal byte pair. Measured on a 200-paragraph footnote-free fixture:
~253,800 ns/op -> ~4,500 ns/op (~56x), 0 allocs/op either way.

BenchmarkCheckFootnotes_NoFootnotes pins the regression with a hard
ns/op budget (b.Fatalf on overshoot), confirmed red against the
pre-fix code and green after.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016wWQmrtb8EMbn1hDgkESqE
hasReservedDeviceName called strings.ToUpper on every path segment
before the reservedDeviceNames set lookup. strings.ToUpper is a no-op
copy only when the input already matches the target case, so a typical
(lowercase) segment always paid the allocation. Every reserved name is
3-4 bytes (CON, PRN, COM1-9, LPT1-9), so segments outside that length
range can never match; gating the ToUpper call behind a length check
skips the allocation entirely for the common case.

Per docs/development/high-performance-go.md's "Compile regexes at
package scope" sibling guidance on cheap pre-checks gating expensive
work. Measured on a 3-path, 7-segment fixture: 602 ns/op & 10
allocs/op -> ~380 ns/op & 5 allocs/op (the two segments that do fall
in the 3-4 byte range still allocate, as they must to compare).

TestHasReservedDeviceName_AllocBudget pins the allocation ceiling,
confirmed red against the pre-fix code and green after.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016wWQmrtb8EMbn1hDgkESqE
driftParts computed githooks.ResolveHooksDir(repoRoot) twice on a cache
miss: once inside peekHookSource and again inside
preMergeCommitHookDrift. Each call spawns a `git rev-parse --git-path
hooks` subprocess, exactly the "never exec a subprocess per item (git
rev-parse once did)" anti-pattern docs/development/high-performance-go.md
calls out under "Skip work you don't need". Resolve it once in
driftParts and thread the result into both helpers.

The result is already cached per repoRoot for the process lifetime
(driftCache), so this halves one subprocess spawn per repo per
process rather than per file — a small but real, unambiguous win with
no measurement needed to justify avoiding a redundant fork/exec.

TestDriftParts_ResolvesHooksDirOnce substitutes a counting stub for
the new resolveHooksDir package var and asserts it is called exactly
once per driftParts cache miss.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016wWQmrtb8EMbn1hDgkESqE
@jeduden
jeduden force-pushed the claude/kind-darwin-zn04bd branch from 8ec143a to e8d7647 Compare July 25, 2026 19:20
@jeduden jeduden added queue Add to a PR to enqueue it and removed queue:failed Applied automatically when CI fails or merge conflict occurs labels Jul 25, 2026 — with Claude
@jeduden jeduden added queue:active Applied automatically when a PR is in an active batch and removed queue Add to a PR to enqueue it labels Jul 25, 2026
@jeduden

jeduden commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

🟢 Merge Queue — picked up

This PR is in the queue and will be batched with other queue-labelled PRs.

Next: No action needed — you'll get another comment when CI starts on the batch. View merge queue run.

@jeduden

jeduden commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

🔵 Merge Queue — CI running

Merged into batch branch merge-queue/batch-757-1785007282 alongside #757. View CI run.

Next: No action needed — you'll be notified when CI completes.

@jeduden
jeduden merged commit 2ab4b29 into main Jul 25, 2026
32 checks passed
@jeduden jeduden removed the queue:active Applied automatically when a PR is in an active batch label Jul 25, 2026
@jeduden

jeduden commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

Merge Queue — merged

This PR landed on main via commit 2ab4b29. CI run that validated the merge.

Next: Done — nothing more to do here.

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