-
-
Notifications
You must be signed in to change notification settings - Fork 905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support compiled XPath expressions #3380
Draft
flavorjones
wants to merge
2
commits into
main
Choose a base branch
from
flavorjones-compiled-xpath-queries
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains 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
flavorjones
force-pushed
the
flavorjones-compiled-xpath-queries
branch
from
December 21, 2024 22:21
a40965b
to
930e231
Compare
An example benchmark script: #! /usr/bin/env ruby
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "nokogiri", path: "."
gem "benchmark-ips"
end
doc_large = Nokogiri::HTML5.parse(File.read(File.join(__dir__, "../test/files/tlm.html")))
doc_small = Nokogiri::HTML5.parse(File.read(File.join(__dir__, "../test/files/noencoding.html")))
Benchmark.ips do |x|
x.warmup = 0
expression_str = "//p[nokogiri-builtin:css-class(@class,'br0') and count(preceding-sibling::*)=0]"
expression_comp = Nokogiri::XML::XPath::Expression.new(expression_str)
x.report("small: compiled") do
doc_small.xpath(expression_comp).length == 0 or raise("nope")
end
x.report("small: string") do
doc_small.xpath(expression_str).length == 0 or raise("nope")
end
x.compare!
end outputs:
|
which is the compiled form of an XPath expression
flavorjones
force-pushed
the
flavorjones-compiled-xpath-queries
branch
from
January 3, 2025 21:02
930e231
to
bda0ec6
Compare
Updated benchmark script, using one extremely small expression as advised by Nick in #3378: #! /usr/bin/env ruby
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "nokogiri", path: "."
gem "benchmark-ips"
end
doc = Nokogiri::HTML5.parse(File.read(File.join(__dir__, "../test/files/tlm.html")))
expressions = [
["//@href", 203],
["//*[nokogiri-builtin:css-class(@class,'br0') and count(preceding-sibling::*)=0]", 8],
]
expressions.each do |expression_str, count|
Benchmark.ips do |x|
x.warmup = 0
expression_comp = Nokogiri::XML::XPath::Expression.new(expression_str)
x.report("string") do
doc.xpath(expression_str).length == count or raise("nope #{doc.xpath(expression_comp).length}")
end
x.report("compiled") do
doc.xpath(expression_comp).length == count or raise("nope #{doc.xpath(expression_comp).length}")
end
x.compare!
end
end Result:
So I'm still not seeing much/any benefit to compiled expressions. Will continue to leave this PR open to invite folks to write a benchmark that demonstrates that this is faster in a real-world benchmark (macro or micro). |
flavorjones
force-pushed
the
flavorjones-compiled-xpath-queries
branch
from
January 3, 2025 21:17
bda0ec6
to
0c1362f
Compare
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.
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.