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.
|
# The XML declaration of the document.
|
||||||
# @return [Oga::XML::XmlDeclaration]
|
# @return [Oga::XML::XmlDeclaration]
|
||||||
#
|
#
|
||||||
# @!attribute [rw] type
|
# @!attribute [r] type
|
||||||
# The document type, either `:xml` or `:html`.
|
# The document type, either `:xml` or `:html`.
|
||||||
# @return [Symbol]
|
# @return [Symbol]
|
||||||
#
|
#
|
||||||
|
@ -20,7 +20,9 @@ module Oga
|
||||||
include Querying
|
include Querying
|
||||||
include Traversal
|
include Traversal
|
||||||
|
|
||||||
attr_accessor :doctype, :xml_declaration, :type
|
attr_accessor :doctype, :xml_declaration
|
||||||
|
|
||||||
|
attr_reader :type
|
||||||
|
|
||||||
##
|
##
|
||||||
# @param [Hash] options
|
# @param [Hash] options
|
||||||
|
|
|
@ -5,13 +5,13 @@ module Oga
|
||||||
# {Oga::XML::NodeSet} and can be used to query surrounding and parent
|
# {Oga::XML::NodeSet} and can be used to query surrounding and parent
|
||||||
# nodes.
|
# nodes.
|
||||||
#
|
#
|
||||||
# @!attribute [rw] node_set
|
# @!attribute [r] node_set
|
||||||
# @return [Oga::XML::NodeSet]
|
# @return [Oga::XML::NodeSet]
|
||||||
#
|
#
|
||||||
class Node
|
class Node
|
||||||
include Traversal
|
include Traversal
|
||||||
|
|
||||||
attr_accessor :node_set
|
attr_reader :node_set
|
||||||
|
|
||||||
##
|
##
|
||||||
# @param [Hash] options
|
# @param [Hash] options
|
||||||
|
@ -23,11 +23,19 @@ module Oga
|
||||||
# the current node.
|
# the current node.
|
||||||
#
|
#
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
@node_set = options[:node_set]
|
self.node_set = options[:node_set]
|
||||||
|
|
||||||
self.children = options[:children] if options[:children]
|
self.children = options[:children] if options[:children]
|
||||||
end
|
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.
|
# Returns the child nodes of the current node.
|
||||||
#
|
#
|
||||||
|
@ -120,17 +128,21 @@ module Oga
|
||||||
# @return [Oga::XML::Document|Oga::XML::Node]
|
# @return [Oga::XML::Document|Oga::XML::Node]
|
||||||
#
|
#
|
||||||
def root_node
|
def root_node
|
||||||
node = self
|
unless @root_node
|
||||||
|
node = self
|
||||||
|
|
||||||
loop do
|
loop do
|
||||||
if !node.is_a?(Document) and node.node_set
|
if !node.is_a?(Document) and node.node_set
|
||||||
node = node.node_set.owner
|
node = node.node_set.owner
|
||||||
else
|
else
|
||||||
break
|
break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@root_node = node
|
||||||
end
|
end
|
||||||
|
|
||||||
return node
|
return @root_node
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -168,9 +180,13 @@ module Oga
|
||||||
# @return [TrueClass|FalseClass]
|
# @return [TrueClass|FalseClass]
|
||||||
#
|
#
|
||||||
def html?
|
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
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
|
@ -144,13 +144,19 @@ describe Oga::XML::Node do
|
||||||
@doc = Oga::XML::Document.new(:children => [@n1])
|
@doc = Oga::XML::Document.new(:children => [@n1])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns the root document of an element' do
|
it 'returns the root document of a Node' do
|
||||||
@n2.root_node.should == @doc
|
@n2.root_node.should == @doc
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns the root element of another element' do
|
it 'returns the root Node of another Node' do
|
||||||
@n4.root_node.should == @n3
|
@n4.root_node.should == @n3
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'flushes the cache when changing the NodeSet of a Node' do
|
||||||
|
@n1.children << @n4
|
||||||
|
|
||||||
|
@n4.root_node.should == @doc
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#remove' do
|
describe '#remove' do
|
||||||
|
@ -219,6 +225,18 @@ describe Oga::XML::Node do
|
||||||
|
|
||||||
node.html?.should == false
|
node.html?.should == false
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe '#xml?' do
|
describe '#xml?' do
|
||||||
|
|
Loading…
Reference in New Issue