Skip to content

Reduce allocations on file-discovery and markdown hot paths - #783

Merged
SimonCropp merged 1 commit into
mainfrom
Reduce-allocations-on-file-discovery-and-markdown-hot-paths
Jul 7, 2026
Merged

Reduce allocations on file-discovery and markdown hot paths#783
SimonCropp merged 1 commit into
mainfrom
Reduce-allocations-on-file-discovery-and-markdown-hot-paths

Conversation

@SimonCropp

Copy link
Copy Markdown
Owner

Three behavior-preserving allocation reductions on always-on paths. Each was measured with GC.GetAllocatedBytesForCurrentThread and produces byte-identical output; the full snapshot suite stays green.

  • GetLanguageFromPath: span-based Path.GetExtension + a single lowercasing string.Create, replacing GetExtension + TrimStart('.') + ToLowerInvariant (3 string allocations -> 1). 71.2 -> 28.8 B/call. Called for every file during discovery and again per snippet source file.

  • MarkdownProcessor toc checks: line.Current.TrimStart() == "toc" ->
    span SequenceEqual, dropping a trimmed-string allocation on every indented line.

  • Line.LeadingWhitespace: computed lazily. Only snippet / web-snippet lines ever read it, but the ctor allocated a leading-whitespace substring for every indented line in the document.

Wins 2 and 3 together cut ~13.5% of the allocations when applying an indentation-heavy document. Adds OptimizationBenchmarks3 (#11 GetLanguageFromPath, #12 indented Apply) to the existing benchmark suite.

Three behavior-preserving allocation reductions on always-on paths. Each was
measured with GC.GetAllocatedBytesForCurrentThread and produces byte-identical
output; the full snapshot suite stays green.

- GetLanguageFromPath: span-based Path.GetExtension + a single lowercasing
  string.Create, replacing GetExtension + TrimStart('.') + ToLowerInvariant
  (3 string allocations -> 1). 71.2 -> 28.8 B/call. Called for every file
  during discovery and again per snippet source file.

- MarkdownProcessor toc checks: line.Current.TrimStart() == "toc" ->
  span SequenceEqual, dropping a trimmed-string allocation on every indented
  line.

- Line.LeadingWhitespace: computed lazily. Only snippet / web-snippet lines
  ever read it, but the ctor allocated a leading-whitespace substring for every
  indented line in the document.

Wins 2 and 3 together cut ~13.5% of the allocations when applying an
indentation-heavy document. Adds OptimizationBenchmarks3 (#11
GetLanguageFromPath, #12 indented Apply) to the existing benchmark suite.
@SimonCropp SimonCropp added this to the 28.4.0 milestone Jul 7, 2026
@SimonCropp
SimonCropp merged commit a39476b into main Jul 7, 2026
3 of 5 checks passed
@SimonCropp
SimonCropp deleted the Reduce-allocations-on-file-discovery-and-markdown-hot-paths branch July 7, 2026 00:06
This was referenced Jul 7, 2026
This was referenced Jul 7, 2026
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