Repository navigation
Conversation
a ~ b was translated to a/following-sibling::b. When many siblings match a, their following-sibling node-sets overlap, and libxml2 merges each one into the result with a linear duplicate scan, so matching was cubic in the number of siblings: li ~ li took 154 s on a list of 8000 items. The siblings after the first match of a in a sibling list include those after every later one, so only walk from that one, found with preceding-sibling::a[1], which stops at the nearest match. position() = 1 keeps the first match from each context node, which matters when the context node itself matches and has matching siblings before it, and in ~ chains. Only a plain name test is handled: a condition would have to be copied into the predicate, doubling the XPath at each nested :has() level.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #227 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 3 3
Lines 1036 1038 +2
Branches 174 175 +1
=========================================
+ Hits 1036 1038 +2 ☔ View full report in Codecov by Harness. |
This branch has not been deployed
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.
a ~ bwas translated toa/following-sibling::b, so when many siblings matchatheir following-sibling sets overlap and libxml2 merges each one in with a linear duplicate scan, which makes matching cubic in the list length (lxml 6.1.3 / libxml2 2.14.6:li ~ litakes 1.5 s on a 19 KiB page of 2000<li>, 13 s at 4000 and 154 s at 8000, same story fordt ~ ddandli ~ li ~ li, so one fetched page is enough to stall a spider); the siblings after the first match cover those after every later one, so for a plain name test on the leftxpath_indirect_adjacent_combinator()now only walks from that one via[position() = 1 or not(preceding-sibling::a[1])](position() = 1keeps the first match from each context node, i.e. the context node itself or the first match after each node of a~chain), which bringsli ~ lidown to 2 ms at 8000 siblings and 37 ms at 128000 and selects the same elements as before on ~1.1M random selector/document/context-node checks with every prefix exceptancestor::,following::andpreceding::ones; a left side with a condition (li.a ~ li) is left alone because copying it into the predicate would double the XPath at each nested:has()level.