[repo-assist] perf: avoid O(n^2) re-scan when splitting Markdown pipe-table rows - #1319
Draft
github-actions[bot] wants to merge 1 commit into
Draft
Conversation
pipeTableFindSplits recomputed List.length over the full remaining line and the post-delimiter remainder on every recursive call, to compute the chunk size for the current cell. For a row with many delimiters (cells), this makes parsing quadratic in the row length. Track the number of consumed characters incrementally while scanning instead, so each character is counted exactly once. No behavior change: same delimiter/escape/code/math handling, same output for all existing table tests. Added a new test exercising an escaped pipe inside a cell to cover the escape-handling branch of the scan. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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 PR was created by Repo Assist, an automated AI assistant.
Summary
pipeTableFindSplitsinsrc/FSharp.Formatting.Markdown/MarkdownTableParser.fsparses a Markdown pipe-table row into cells by finding delimiter positions recursively. For each cell found, it computed the chunk size asList.length line - List.length x - 1, recomputingList.lengthover the (shrinking) remaining line and the post-delimiter remainder on every recursive call. For a row with many cells/delimiters, this makes the split step quadratic (O(n2)) in the row length instead of linear.Fix
The inner scan (
ptfs) now returns the number of characters consumed up to and including the found delimiter, tracked incrementally as it walks the list, instead of leaving the caller to diff two list lengths. Each character in the row is now counted exactly once, making the split linear in the row length. Behavior (delimiter handling, LaTeX math escaping, inline-code escaping, backslash-escaped delimiters) is unchanged.Testing
dotnet build FSharp.Formatting.sln -c Release— 0 errors.dotnet test tests/FSharp.Markdown.Tests/FSharp.Markdown.Tests.fsproj -c Release— 369/369 pass (368 existing + 1 new test covering an escaped pipe character inside a table cell, exercising the'\\' :: _ :: restbranch of the rewritten scan).dotnet fantomas src tests --check— clean, no formatting changes needed beyond what was already applied.Test Status
✅ Build succeeded (0 errors/warnings). ✅ All 369 FSharp.Markdown.Tests pass, including the newly added test.
Trade-offs
None identified — this is a pure algorithmic improvement with identical output for all inputs; the existing "delimiters in code or math" table test and the new escaped-pipe test both pass unchanged.
Add this agentic workflow to your repo
To install this agentic workflow, run