[Repo Assist] perf: avoid Seq allocations in hot markdown-parsing paths#1171
Closed
github-actions[bot] wants to merge 2 commits intomainfrom
Closed
[Repo Assist] perf: avoid Seq allocations in hot markdown-parsing paths#1171github-actions[bot] wants to merge 2 commits intomainfrom
github-actions[bot] wants to merge 2 commits intomainfrom
Conversation
- removeSpaces: replace Seq.takeWhile+Seq.length per line with String.TrimStart().Length — avoids boxing each char and allocating two enumerators per non-empty line. - StartsWithNTimesTrimIgnoreStartWhitespace: replace Seq.windowed + Seq.map String + Seq.takeWhile + Seq.length with a direct index loop — avoids O(n) sliding-window allocations just to count consecutive fence characters (e.g. backticks or tildes) at the start of a line. Called for every line during markdown block parsing. - XmlDocReader.readXmlElementAsSingleSummary: same Seq.takeWhile fix when checking indentation columns of XML doc comment lines. All 520 tests pass (317 Markdown, 143 Literate, 30 CodeFormat, 30 ApiDocs). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
6 tasks
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.
🤖 This is an automated pull request from Repo Assist.
Summary
Three small but impactful performance improvements in parsing hot paths, eliminating unnecessary
Seqenumerator allocations and object boxing.1.
removeSpaces— count leading whitespace withoutSeqFile:
src/Common/StringParsing.fsremoveSpacesis called for every XML doc comment and every literate code block. The old implementation counted leading whitespace with:This allocates two
IEnumeratorobjects per non-empty line and boxes eachchar. The replacement usesline.Length - line.TrimStart().Length, which is a single O(n) pass with no allocations.2.
StartsWithNTimesTrimIgnoreStartWhitespace— count fence chars withoutSeq.windowedFile:
src/Common/StringParsing.fsThis active pattern is called for every line during markdown block parsing to detect fenced code blocks (
```or~~~). The old implementation:...built a sliding-window sequence over the entire line, allocated a new
System.Stringfor each window, and counted withSeq.length. For a 100-character line with a 3-char fence, that's ~98 string allocations before yielding 3.Replaced with a direct index loop:
3.
readXmlElementAsSingleSummary— count leading spaces withoutSeqFile:
src/FSharp.Formatting.ApiDocs/XmlDocReader.fsSame
Seq.takeWhile + Seq.lengthpattern, called once per line per XML doc comment to detect indentation uniformity. Replaced withline.Length - line.TrimStart([|' '|]).Length.Test Status
dotnet build FSharp.Formatting.sln --configuration Release— 0 warnings, 0 errors