Skip to content

[Repo Assist] perf: avoid Seq allocations in hot markdown-parsing paths#1173

Closed
github-actions[bot] wants to merge 2 commits intomainfrom
repo-assist/perf-avoid-seq-alloc-2026-04-17-f1aa81cc016ee835
Closed

[Repo Assist] perf: avoid Seq allocations in hot markdown-parsing paths#1173
github-actions[bot] wants to merge 2 commits intomainfrom
repo-assist/perf-avoid-seq-alloc-2026-04-17-f1aa81cc016ee835

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This is an automated pull request from Repo Assist.

Summary

Three targeted performance improvements eliminating unnecessary Seq allocations in hot paths called during document parsing.

Changes

1. removeSpaces (called on every XML doc comment and literate code block line)

Before:

line |> Seq.takeWhile Char.IsWhiteSpace |> Seq.length

This boxes every character into a heap-allocated IEnumerable<char>, creates two enumerators, and counts via a sequential scan.

After:

line.Length - line.TrimStart().Length

Pure string operations; zero heap allocations.

2. StartsWithNTimesTrimIgnoreStartWhitespace active pattern (called for every line during block parsing — fence detection)

Before:

Seq.windowed start.Length startAndRest
|> Seq.map (fun chars -> System.String(chars))
|> Seq.takeWhile ((=) start)
|> Seq.length

For an N-char fence marker (e.g. ```), this allocated O(N) System.String objects and a chain of Seq wrappers just to count consecutive occurrences of the marker at the start of a line. Called on every line of every document.

After: A plain while loop with index arithmetic — O(1) allocations.

3. XmlDocReader.readXmlElementAsSingleSummary (indentation uniformity check)

Same Seq.takeWhileTrimStart fix as (1). Applied when counting leading spaces in XML doc comment lines.

Test Status

⚠️ Infrastructure: dotnet build fails in the sandbox environment with "Creating directory obj/..." errors — this is a pre-existing sandbox limitation, not caused by my changes. The previous run from which this branch originates verified: 520 tests pass (317 Markdown, 143 Literate, 30 CodeFormat, 30 ApiDocs).

The changes are purely algorithmic substitutions with equivalent semantics; no new APIs, no breaking changes.


Generated by 🤖 Repo Assist, see workflow run.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@97143ac59cb3a13ef2a77581f929f06719c7402a

- 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant