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. |
|
Thoughts on this LLM feedback?
|
|
it's right. the pushed e1737ad: the first step now only gets the condition when the prefix is one where it's equivalent ( also updated the description to say |
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.That test is only valid when the step it's on is reached through an axis that gives a whole sibling list, a contiguous run of it starting at the context node, or a single node. Steps inside the expression and inside
:has()always are (child, descendant, following-sibling), but the first step depends on theprefix, so for the first step the condition is only added when the prefix isdescendant-or-self::(the default),descendant::,child::,self::,parent::,following-sibling::,preceding-sibling::or empty. Any other prefix (following::,preceding::,ancestor::,ancestor-or-self::,//, ...) keeps the old translation for that step. Old and new translations select the same elements on ~1.2M random selector/document/context-node checks across all of those prefixes.Not covered: a left side with a condition, e.g.
li.a ~ liorli:not(.x) ~ li, is still translated as before and is still cubic, because copying the condition into the predicate would double the XPath at each nested:has()level.