Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorickpeterse/9df7d0bce147da9eb38b to your computer and use it in GitHub Desktop.
Save yorickpeterse/9df7d0bce147da9eb38b to your computer and use it in GitHub Desktop.
Oga will soon have a new XPath compiler that will compile XPath to Ruby source code. This is a quick experiment to see if this would bring any performance increases and if so, how much.
Calculating -------------------------------------
evaluator 1.097k i/100ms
generated 8.711k i/100ms
-------------------------------------------------
evaluator 10.641k (±13.5%) i/s - 52.656k
generated 99.626k (± 1.1%) i/s - 505.238k
Comparison:
generated: 99626.2 i/s
evaluator: 10641.4 i/s - 9.36x slower
$:.unshift(File.expand_path('../lib', __FILE__))
require 'oga'
require 'benchmark/ips'
document = Oga.parse_xml <<-EOF
<people>
<person>
<name>Alice</name>
<age>26</age>
</person>
<person>
<name>Bob</name>
<age>27</age>
</person>
<person>
<name>Eve</name>
<age>25</age>
</person>
</people>
EOF
expression = 'people/person[name = "Alice"]'
# This would basically be the code Oga's new XPath compiler would generate for
# the above expression. Since there's no compiler just yet this is written by
# hand instead.
generated = proc do |root|
matched = Oga::XML::NodeSet.new
root.children.each do |n1|
if n1.is_a?(Oga::XML::Element) and n1.name == 'people'
n1.children.each do |n2|
if n2.is_a?(Oga::XML::Element) and n2.name == 'person'
n2.children.each do |n3|
if n3.is_a?(Oga::XML::Element) and n3.name == 'name'
if n3.text == 'Alice'
matched << n2
end
end
end
end
end
end
end
matched
end
Benchmark.ips do |bench|
bench.report 'evaluator' do
Oga::XPath::Evaluator.new(document).evaluate(expression)
end
bench.report 'generated' do
generated.call(document)
end
bench.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment