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:
1 parent
421e6e910b
commit
b0359b37e5
3 files changed
+53
-17
No files matched your search
@@ -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
|
||||
|
||||
+29
-13
@@ -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
|
||||
|
||||
##
|
||||
|
||||
Reference in new issue
Block a user