Repository navigation
Keep the context node after a nested predicate - #193
Open
LuisFigueroaG wants to merge 1 commit into
Open
LuisFigueroaG wants to merge 1 commit into
LuisFigueroaG wants to merge 1 commit into
Conversation
filterQuery moves t.Current() to each node while it evaluates the predicate and leaves it there. When the filter is one operand of an expression, the operands after it were evaluated against that node instead of the original context node, so //p[a[@x] = @id] compared a with the id of a instead of the id of p. The same happened with string(), number(), contains(), concat() and the other functions that take a filtered node-set before another argument. Move t.Current() back to the context node after each predicate. The saved node is kept on the filter and reused with MoveTo, so it is only copied once per query.
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.
When a predicate contains a path with its own predicate, the operands after that path are evaluated against the wrong context node.
//p[a[@x] = @id]compares eachawith theidof thata, not theidofp.filterQuery.Selectmovest.Current()to each node while it evaluates the predicate and leaves it there. The left operand of=moves the shared context to the lastait looked at, and@idis then evaluated from there. The same happens withstring(),number(),contains(),concat()and the other functions that take a filtered node-set before another argument.numericQuery,booleanQueryandlang()already work around this by copying the context node before evaluating their operands.On
//p[@id = a[@x]]already worked, because@idis evaluated first.The filter now moves
t.Current()back to the context node after each predicate. The saved node is kept on the filter and reused withMoveTo, so it is copied once per query rather than once per node. The filter benchmarks have one more allocation per run and no measurable change in time.I added
TestNestedPredicateKeepsContextNode, which fails on master.I also ran 46,027 expressions against lxml: 19 filtered paths (child, descendant, following-sibling, ancestor, parent, positional and
(…)[1]predicates) combined with 13 context-dependent operands (@id,.,name(),position(),last(),count(tag)…) in 24 comparisons and function calls, inside 6 location paths, pluscount()and[1]on top. 8,992 differ from lxml on master and 1,040 with this change. 72 that matched before no longer do. They all use a reverse axis as a string argument, likestarts-with(ancestor::*[@id], @n)(#182 handles this), or(tag[@k])[1]inside a predicate, which only selects for the first context node. On master, the moved context node happened to give the right answer for those. The rest of the remaining differences come from those two cases or are node-set order only (//*[count(tag) >= 0]already returns nodes out of document order).