From d92133ef437d66e2b85fbebcc181a563c2d72363 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 2 Sep 2014 20:55:42 +0200 Subject: [PATCH] Benchmark Oga XPath evaluation without the parser. This gives better insight in the performance of the evaluator itself. --- benchmark/xpath/parser/comparing_gems_bench.rb | 9 +++++++++ lib/oga/xpath/evaluator.rb | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/benchmark/xpath/parser/comparing_gems_bench.rb b/benchmark/xpath/parser/comparing_gems_bench.rb index 9f1837d..d09963a 100644 --- a/benchmark/xpath/parser/comparing_gems_bench.rb +++ b/benchmark/xpath/parser/comparing_gems_bench.rb @@ -14,6 +14,9 @@ rex_doc = REXML::Document.new(xml) ox_exp = 'number/^Text' xpath_exp = 'root/number/text()' +oga_ast = Oga::XPath::Parser.new(xpath_exp).parse +evaluator = Oga::XPath::Evaluator.new(oga_doc) + Benchmark.ips do |bench| # Technically not XPath but it's the closest thing Ox provides. bench.report 'Ox' do @@ -28,6 +31,12 @@ Benchmark.ips do |bench| oga_doc.xpath(xpath_exp) end + # This is measured to see what the performance of the evaluator is _without_ + # the overhead of the lexer/parser. + bench.report 'Oga cached' do + evaluator.evaluate_ast(oga_ast) + end + bench.report 'REXML' do REXML::XPath.match(rex_doc, xpath_exp) end diff --git a/lib/oga/xpath/evaluator.rb b/lib/oga/xpath/evaluator.rb index a71fba4..11e6ec2 100644 --- a/lib/oga/xpath/evaluator.rb +++ b/lib/oga/xpath/evaluator.rb @@ -79,10 +79,21 @@ module Oga # evaluator.evaluate('//a') # # @param [String] string An XPath expression as a String. - # @return [Oga::XML::Node] + # @return [Mixed] # def evaluate(string) - ast = Parser.new(string).parse + ast = Parser.new(string).parse + + return evaluate_ast(ast) + end + + ## + # Evaluates a pre-parsed XPath expression. + # + # @param [Oga::XPath::Node] ast_node + # @return [Mixed] + # + def evaluate_ast(ast) context = XML::NodeSet.new([@document]) return process(ast, context)