Cache Node#html? and Node#root_node
The results of these methods is now cached until a Node is moved into another NodeSet. This reduces the time spent in the xpath/evaluator/big_xml_average_bench.rb benchmark from roughly 10 seconds to roughly 5 seconds per iteration.
This commit is contained in:
parent
421e6e910b
commit
b0359b37e5
|
@ -12,7 +12,7 @@ module Oga
|
|||
# The XML declaration of the document.
|
||||
# @return [Oga::XML::XmlDeclaration]
|
||||
#
|
||||
# @!attribute [rw] type
|
||||
# @!attribute [r] type
|
||||
# The document type, either `:xml` or `:html`.
|
||||
# @return [Symbol]
|
||||
#
|
||||
|
@ -20,7 +20,9 @@ module Oga
|
|||
include Querying
|
||||
include Traversal
|
||||
|
||||
attr_accessor :doctype, :xml_declaration, :type
|
||||
attr_accessor :doctype, :xml_declaration
|
||||
|
||||
attr_reader :type
|
||||
|
||||
##
|
||||
# @param [Hash] options
|
||||
|
|
|
@ -5,13 +5,13 @@ module Oga
|
|||
# {Oga::XML::NodeSet} and can be used to query surrounding and parent
|
||||
# nodes.
|
||||
#
|
||||
# @!attribute [rw] node_set
|
||||
# @!attribute [r] node_set
|
||||
# @return [Oga::XML::NodeSet]
|
||||
#
|
||||
class Node
|
||||
include Traversal
|
||||
|
||||
attr_accessor :node_set
|
||||
attr_reader :node_set
|
||||
|
||||
##
|
||||
# @param [Hash] options
|
||||
|
@ -23,11 +23,19 @@ module Oga
|
|||
# the current node.
|
||||
#
|
||||
def initialize(options = {})
|
||||
@node_set = options[:node_set]
|
||||
|
||||
self.node_set = options[:node_set]
|
||||
self.children = options[:children] if options[:children]
|
||||
end
|
||||
|
||||
##
|
||||
# @param [Oga::XML::NodeSet] set
|
||||
#
|
||||
def node_set=(set)
|
||||
@node_set = set
|
||||
@root_node = nil
|
||||
@html_p = nil
|
||||
end
|
||||
|
||||
##
|
||||
# Returns the child nodes of the current node.
|
||||
#
|
||||
|
@ -120,17 +128,21 @@ module Oga
|
|||
# @return [Oga::XML::Document|Oga::XML::Node]
|
||||
#
|
||||
def root_node
|
||||
node = self
|
||||
unless @root_node
|
||||
node = self
|
||||
|
||||
loop do
|
||||
if !node.is_a?(Document) and node.node_set
|
||||
node = node.node_set.owner
|
||||
else
|
||||
break
|
||||
loop do
|
||||
if !node.is_a?(Document) and node.node_set
|
||||
node = node.node_set.owner
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
@root_node = node
|
||||
end
|
||||
|
||||
return node
|
||||
return @root_node
|
||||
end
|
||||
|
||||
##
|
||||
|
@ -168,9 +180,13 @@ module Oga
|
|||
# @return [TrueClass|FalseClass]
|
||||
#
|
||||
def html?
|
||||
root = root_node
|
||||
if @html_p.nil?
|
||||
root = root_node
|
||||
|
||||
return root.is_a?(Document) && root.html?
|
||||
@html_p = root.is_a?(Document) && root.html?
|
||||
end
|
||||
|
||||
return @html_p
|
||||
end
|
||||
|
||||
##
|
||||
|
|
|
@ -144,13 +144,19 @@ describe Oga::XML::Node do
|
|||
@doc = Oga::XML::Document.new(:children => [@n1])
|
||||
end
|
||||
|
||||
it 'returns the root document of an element' do
|
||||
it 'returns the root document of a Node' do
|
||||
@n2.root_node.should == @doc
|
||||
end
|
||||
|
||||
it 'returns the root element of another element' do
|
||||
it 'returns the root Node of another Node' do
|
||||
@n4.root_node.should == @n3
|
||||
end
|
||||
|
||||
it 'flushes the cache when changing the NodeSet of a Node' do
|
||||
@n1.children << @n4
|
||||
|
||||
@n4.root_node.should == @doc
|
||||
end
|
||||
end
|
||||
|
||||
describe '#remove' do
|
||||
|
@ -219,6 +225,18 @@ describe Oga::XML::Node do
|
|||
|
||||
node.html?.should == false
|
||||
end
|
||||
|
||||
it 'flushes the cache when changing the NodeSet of a Node' do
|
||||
node = described_class.new
|
||||
html_doc = Oga::XML::Document.new(:type => :html)
|
||||
xml_doc = Oga::XML::Document.new(:type => :xml, :children => [node])
|
||||
|
||||
node.html?.should == false
|
||||
|
||||
html_doc.children << node
|
||||
|
||||
node.html?.should == true
|
||||
end
|
||||
end
|
||||
|
||||
describe '#xml?' do
|
||||
|
|
Loading…
Reference in New Issue