Skip to content

Commit 8a05e38

Browse files
github-actions[bot]Copilotdsymenojaf
authored
[repo-assist] perf: avoid O(n^2) re-scan when splitting Markdown pipe-table rows (#1319)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Don Syme <dsyme@users.noreply.github.com> Co-authored-by: Florian Verdonck <florian.verdonck@outlook.com>
1 parent ff223bd commit 8a05e38

3 files changed

Lines changed: 51 additions & 13 deletions

File tree

‎RELEASE_NOTES.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44

55
### Changed
6+
* Avoid recomputing `List.length` on the shrinking remainder for every scanned character when splitting a Markdown pipe-table row into cells (`pipeTableFindSplits` in `MarkdownTableParser.fs`). The scan now tracks the number of consumed characters incrementally, turning an O(n²) split into an O(n) one for rows with many delimiters, with no behavior change.
67
* Avoided a per-character `Seq.windowed`/`String()` allocation in `StartsWithNTimesTrimIgnoreStartWhitespace`, the active pattern used to count repeated fence characters (`` ` `` / `~`) when parsing code fences and headers. The count is now computed with `String.CompareOrdinal` over string offsets, with no intermediate substring allocations. Behavior is unchanged.
78

89
## [23.0.0-alpha.8] - 2026-09-18

‎src/FSharp.Formatting.Markdown/MarkdownTableParser.fs‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,34 @@ let rec pipeTableFindSplits (delim: char array) (line: char list) =
2020
let cLstToStr (x: char list) =
2121
x |> Array.ofList |> System.String.Concat
2222

23+
// Scans the line and, on success, returns the number of characters consumed up to and
24+
// including the found delimiter, together with the remaining list. Tracking the consumed
25+
// count incrementally (rather than recomputing `List.length` over the shrinking remainder
26+
// on every character) keeps this scan linear instead of quadratic in the row length.
2327
let rec ptfs delim line =
2428
match line with
25-
| DelimitedLatexDisplayMath [ '$'; '$' ] (_body, rest) -> ptfs delim rest
26-
| DelimitedLatexInlineMath [ '$' ] (_body, rest) -> ptfs delim rest
27-
| List.DelimitedWith [ '`'; ' ' ] [ ' '; '`' ] (_body, rest, _s, _e) -> ptfs delim rest
28-
| List.DelimitedNTimes '`' (_body, rest, _s, _e) -> ptfs delim rest
29-
| x :: rest when Array.exists ((=) x) delim -> Some rest
30-
| '\\' :: _ :: rest
31-
| _ :: rest -> ptfs delim rest
29+
| DelimitedLatexDisplayMath [ '$'; '$' ] (_body, rest)
30+
| DelimitedLatexInlineMath [ '$' ] (_body, rest)
31+
| List.DelimitedWith [ '`'; ' ' ] [ ' '; '`' ] (_body, rest, _, _)
32+
| List.DelimitedNTimes '`' (_body, rest, _, _) ->
33+
let consumedHere = List.length line - List.length rest
34+
35+
ptfs delim rest
36+
|> Option.map (fun (count, remainder) -> (count + consumedHere, remainder))
37+
| x :: rest when Array.exists ((=) x) delim -> Some(1, rest)
38+
| '\\' :: _ :: rest -> ptfs delim rest |> Option.map (fun (count, remainder) -> (count + 2, remainder))
39+
| _ :: rest -> ptfs delim rest |> Option.map (fun (count, remainder) -> (count + 1, remainder))
3240
| [] -> None
3341

34-
let rest = ptfs delim line
42+
let result = ptfs delim line
3543

36-
match rest with
44+
match result with
3745
| None -> [ cLstToStr line ]
38-
| Some _x when List.isEmpty line -> [ "" ]
39-
| Some x ->
40-
let chunkSize = List.length line - List.length x - 1
46+
| Some _ when List.isEmpty line -> [ "" ]
47+
| Some(count, x) ->
48+
let chunkSize = count - 1
4149

42-
cLstToStr (Seq.take chunkSize line |> Seq.toList) :: pipeTableFindSplits delim x
50+
cLstToStr (List.truncate chunkSize line) :: pipeTableFindSplits delim x
4351

4452

4553

‎tests/FSharp.Markdown.Tests/Markdown.fs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,35 @@ let ``Transform tables with delimiters in code or math correctly`` () =
818818

819819
Markdown.ToHtml doc |> shouldEqualNoWhiteSpace expected
820820

821+
[<Test>]
822+
let ``Transform tables with escaped pipe characters correctly`` () =
823+
let doc =
824+
"""| a | b |
825+
|---|---|
826+
| 1\|2 | 3 |
827+
"""
828+
829+
let expected =
830+
"""<table>
831+
<thead>
832+
<tr class="header">
833+
<th><p>a</p></th>
834+
<th><p>b</p></th>
835+
</tr>
836+
</thead>
837+
<tbody>
838+
<tr class="odd">
839+
<td><p>1\|2</p></td>
840+
<td><p>3</p></td>
841+
</tr>
842+
</tbody>
843+
</table>
844+
845+
"""
846+
|> properNewLines
847+
848+
Markdown.ToHtml doc |> shouldEqualNoWhiteSpace expected
849+
821850
[<Test>]
822851
let ``Parse empty blockquote followed by content`` () =
823852
let doc =

0 commit comments

Comments
 (0)