Skip to content

[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
mainfrom
repo-assist/perf-pipe-table-split-20260911-9c64efc07655fd6d
Draft

[repo-assist] perf: avoid O(n^2) re-scan when splitting Markdown pipe-table rows#1319
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/perf-pipe-table-split-20260911-9c64efc07655fd6d

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

pipeTableFindSplits in src/FSharp.Formatting.Markdown/MarkdownTableParser.fs parses a Markdown pipe-table row into cells by finding delimiter positions recursively. For each cell found, it computed the chunk size as List.length line - List.length x - 1, recomputing List.length over 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 Release369/369 pass (368 existing + 1 new test covering an escaped pipe character inside a table cell, exercising the '\\' :: _ :: rest branch 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.

Generated by 🌈 Repo Assist, see workflow run. Learn more.
Comment /repo-assist to run again

Add this agentic workflow to your repo

To install this agentic workflow, run

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

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>
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.

0 participants