Repository navigation
perf(markdown): make list parsing O(n) (was O(n²)) - #1025
Conversation
ChatMarkdownAstBuilder (md4c port) parsed list-heavy markdown in O(n^2): GetCurrentBlockLineStart() scanned all prior blocks on every block close, called once per list item. A 40k-item flat list took ~5.5s; since gateway chat messages render through ChatMarkdownRenderer, a crafted message could stall the chat UI (bounded by MaxInputBytes=256KB to ~5-6s/message). Replace both call sites with the O(1) tail computation blockLines.Count - NLines (the same value ConsumeLinkReferenceDefinitions already uses for the reference-def removal) and drop the redundant scan. 40k items now parse in ~47ms (~116x). All markdown-parser tests unchanged; adds a regression test asserting a 40k-item list parses in <500ms.
|
Codex review: needs maintainer review before merge. Reviewed July 18, 2026, 10:20 PM ET / July 19, 2026, 02:20 UTC. Summary Reproducibility: yes. The linked report supplies a focused list-construction benchmark, and the PR identifies the repeated block-close scan and its two call sites as the current-main reproduction path; this review did not execute it locally. Review metrics: 2 noteworthy metrics.
Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Next step before merge
Security Review detailsBest possible solution: Merge the narrow O(1) parser repair after normal maintainer review and completion of the existing Windows CI, retaining the bounded list-scaling regression coverage. Do we have a high-confidence way to reproduce the issue? Yes. The linked report supplies a focused list-construction benchmark, and the PR identifies the repeated block-close scan and its two call sites as the current-main reproduction path; this review did not execute it locally. Is this the best way to solve the issue? Yes. Reusing the existing tail-offset representation removes the repeated scan without adding a new surface, and the focused scaling test protects the intended complexity boundary. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 277936553135. Label changesLabel changes:
Label justifications:
Evidence reviewedWhat I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
Review history (3 earlier review cycles) |
|
@clawsweeper re-review — added real-behavior proof (executed list-scaling on the PR head + full shared-test suite) to the PR body. |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
Co-authored-by: Cameron Beeley <[email protected]>
Co-authored-by: Cameron Beeley <[email protected]>
Fixes #1024.
ChatMarkdownAstBuilder(md4c port) parsed list-heavy markdown in O(n²):GetCurrentBlockLineStart()scanned all prior blocks on every block close (once per list item). A 40k-item flat list took ~5.5s; gateway chat renders throughChatMarkdownRenderer, so a crafted message could stall the chat UI (bounded byMaxInputBytes=256KBto ~5–6s/message).Fix
Replace both call sites with the O(1) tail computation
blockLines.Count - NLines— the same valueConsumeLinkReferenceDefinitionsalready uses for its reference-def removal — and drop the redundant scan.Diffstat
src/OpenClaw.Shared/Markdown/Md4c/Md4cParser.Block.cstests/OpenClaw.Shared.Tests/MarkdownParserFuzzTests.csGates / verification
OpenClaw.Shared.Testssuite (2957 tests) passes with zero new failures vs the pre-existing baseline; every markdown-parser test is unchanged — the change is behaviour-preserving (the same tail computation was already relied on at the reference-def removal site).MarkdownParserFuzzTestsasserts a 40k-item list parses in <500 ms, plus a pathological-input suite (nested brackets, emphasis runs, deep blockquotes, tables, backticks) — all <20 ms.OpenClaw.Shared(net10.0); the WinUI project isn't built locally (Windows App SDK unavailable) and is covered by CI.Real-behavior proof
Per
@clawsweeper(proof unranked → needs execution evidence). Executed on the PR head (29d0d77, clean — only this change), macOS + .NET SDK 10; paths redacted.Scaling — the list-heavy case actually runs, now linear (pre-fix was quadratic: 350 / 1346 / 5468 ms):
40k-item flat list: 5468 ms (pre-fix) → 40 ms (~137×).
Build_ManyListItems_ScalingCurve(asserts 40k < 500 ms) and theBuild_PathologicalInput_IsBoundedAndDoesNotCrashsuite pass.Shared-test suite (covers the changed
OpenClaw.Sharedcode):The 29 failures are the pre-existing macOS-environmental baseline (HttpListener / ExecApprovalsCoordinator, etc.), identical on a clean checkout — zero new failures; every markdown-parser test is unchanged.
./build.ps1+ tray-test: require Windows (WinUI / Windows App SDK) and aren't runnable on this macOS host; the change is confined toOpenClaw.Shared(net10.0), fully exercised by the shared-test proof above, and the Windows build + e2e run in CI on this PR.