Support compiled XPath expressions #3380
Draft
+221
−11
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.
What problem is this PR intended to solve?
There has been some discussion, summarized at #3266, about exposing libxml2's support for compiled XPath expressions. The idea is that, if you have a complex expression that you use a lot and you don't want to pay the cost of parsing/compiling it multiple times, then you can compile it once and presumably your document search will be faster.
This PR implements a new T_DATA class,
XML::XPath::Expression
, which stores the result of compiling an XPath expression viaxmlXPathCompile
. TheXPathContext
class knows how to accept either a String or an Expression.However, I'm not seeing noticeable improvements in speed, though my benchmark may not capture the benefits.
I'm posting this as a draft in case someone wants to write me a benchmark that shows compiled XPath expressions are compellingly faster than just using Strings. Right now, based on what I'm seeing, I'm not at all sure the complexity is worth the benefit.
Have you included adequate test coverage?
Yes.
Does this change affect the behavior of either the C or the Java implementations?
This is an optimization available on CRuby only; though the idea is that the shorthand methods
Nokogiri::XML::XPath.expression
andNokogiri::CSS.selector
will be no-ops on JRuby (returning the string argument) and code that uses Expressions could be portable across both implementations.