Repository navigation
Fix FindClosestBlock/FindBlockAtPosition with definition groups (#677) - #962
Conversation
|
This is turning log(N) searches into very expensive N allocations + N scans, I don't think it's an acceptable approach for a fix in this case - have you looked at any alternatives? |
…x#677) LinkReferenceDefinitionGroup and FootnoteGroup are added to the document by the parsers with Line 0 and a span starting at 0, at a position that does not follow the source order (the footnote group is even moved to the end of the document). The binary searches in FindClosestBlock and FindBlockAtPosition expect children ordered by line/position, so they could return the group, a definition, or the document itself instead of the block at the requested line/position. When a binary search on a document probes one of these groups, compare the nearest other block within the search range instead, so the search stays logarithmic and does not allocate. The definitions collected by the groups are only considered when nothing else matches; the groups are then taken from the document data (link reference definitions) and from the last child of the document (footnotes) instead of scanning the document.
f65bf9c to
98ed892
Compare
|
You're right, that approach cost far too much. I've reworked it and pushed it as a single commit again:
Numbers come from a quick local loop using
On the same document without any definitions, The ~1 µs blank-line case is the linear walk over the 210 definitions. If you'd rather not pay that on a miss, I also compared this version against the previous one on 3000 generated documents, about 520k
|
|
Thanks @ybai08 for the thorough write-up and fix! 🙏 Besides the new tests, I ran a randomized check: 5000 generated documents mixing paragraphs, link reference definitions, footnotes, lists, quotes and headings. For every line, Review done via CodeAlta / Opus 5.5. |
Fixes #677
Problem
LinkReferenceDefinitionGroupandFootnoteGroupare added to theMarkdownDocumentby the parsers withLine == 0and a span starting at 0:LinkReferenceDefinitionGroupis inserted where the first[label]: urldefinition is parsed, i.e. in the middle of the document;FootnoteGroupis moved to the end of the document.FindClosestBlockandFindBlockAtPositionbinary-search the children of a container and assume they are ordered by line / position, so these groups break the search.The exact repro in the issue (auto-identifiers only) no longer triggers on
main, becauseAutoIdentifierExtensionnow registers its definitions withaddGroup: false. The root cause is still there, though, whenever the document has a real link reference definition or a footnote. Output onmain(ccb9d85):Fix
In
BlockExtensions, the binary searches ofFindClosestBlockandFindBlockAtPositionkeep their existing shape. When the container is aMarkdownDocumentand the probed child is aLinkReferenceDefinitionGrouporFootnoteGroup, the search compares the nearest child in the current[lower, upper]range that is not a group. If the range contains only groups, the search stops there. A document has at most one group of each kind, so the searches stay O(log N) and don't allocate. Nested containers never contain these groups, and their search is unchanged.The definitions inside the groups are only used as a fallback:
FindClosestBlockreturns a definition only on an exact line match. Otherwise it returns the closest block from the other children.FindBlockAtPositionchecks the group children only when no other child contains the position, for example when the position is inside[foo]: /urlor inside a footnote's text.The fallback finds the groups without scanning the document. The
LinkReferenceDefinitionGroupcomes from the document data through a new internalGetLinkReferenceDefinitionGroupOrNull()inLinkReferenceDefinitionExtensions, and it is used only when it is attached to the document (it is not attached when tracking trivia). TheFootnoteGroupisdocument.LastChild, becauseFootnoteParsermoves it to the end. The fallback walks the groups' children linearly, because they are not in line order: auto-identifier heading definitions are appended with line 0, and footnotes are sorted by reference order. If a document contains only groups,FindClosestBlocksearches them as regular children, as before.This follows the direction suggested in the issue (special-casing
LinkReferenceDefinitionGroupinFindClosestBlock). Handling only the last block isn't enough: the link reference group can sit in the middle of the document, andFootnoteGroupandFindBlockAtPositionhave the same problem.No public API is added, and the parsers, the groups'
Line/Spanand the renderers are unchanged.Tests
New tests in
src/Markdig.Tests/TestPragmaLines.cs, next to the existingFindClosestLinetests:TestFindClosestWithLinkReferenceDefinitionTestFindClosestWithFootnotesTestFindBlockAtPositionWithLinkReferenceDefinitionAndFootnotesAll three fail on
main:All three pass with the fix.
What was run locally (macOS arm64, .NET SDK 10.0.401, .NET 8.0.31 runtime):
cd src && dotnet build markdig.slnx -c Release: 0 warnings, 0 errors (net462, netstandard2.0, netstandard2.1, net8.0, net10.0)dotnet test Markdig.Tests -c Release -f net10.0: 7345 passed, 1 skipped (main: 7342 passed, 1 skipped)dotnet test Markdig.Tests -f net8.0(withRunConfiguration.DotNetHostPathpointing at a host that has the .NET 8 runtime): 7345 passed, 1 skippedTest netstandardworkflow steps, run locally for netstandard2.0 and netstandard2.1 in Debug and Release with-p:MarkdigNoRuneTests=true: 7282 passed, 1 skipped on net8.0 and net10.0Not run: the dotnet-releaser CI action (pack/publish) and the net462 test path (the tests do not target net462).