Skip to content

Commit

Permalink
Optimization to Document#collect_namespaces suggested by #761.
Browse files Browse the repository at this point in the history
Note that benchmarks indicate this is *much* faster:

    #! /usr/bin/env ruby

    require 'nokogiri'
    require 'benchmark'

    xhtml = File.read "test/files/tlm.html"

    n = 50
    Benchmark.bm(20) do |b|
      docs = (1..n).collect { |_| Nokogiri::XML xhtml }
      b.report("using xpath (x#{n})") do
        docs.each do |doc|
          doc.collect_namespaces
        end
      end

      docs = (1..n).collect { |_| Nokogiri::XML xhtml }
      b.report("using traverse (x#{n})") do
        ENV['SLOW'] = "1"
        docs.each do |doc|
          doc.collect_namespaces
        end
      end
    end

with the result:

                               user     system      total        real
    using xpath (x50)      0.590000   0.010000   0.600000 (  0.605675)
    using traverse (x50)   2.410000   0.010000   2.420000 (  2.431678)
  • Loading branch information
flavorjones committed Sep 20, 2012
1 parent a0bec35 commit c13f9a5
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/nokogiri/xml/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,15 @@ def document
# Non-prefixed default namespaces (as in "xmlns=") are not included
# in the hash.
#
# Note this is a very expensive operation in current implementation, as it
# traverses the entire graph, and also has to bring each node across the
# libxml bridge into a ruby object.
# Note that this method does an xpath lookup for nodes with
# namespaces, and as a result the order may be dependent on the
# implementation of the underlying XML library.
#
def collect_namespaces
ns = {}
traverse { |j| ns.merge!(j.namespaces) }
ns
xpath("//namespace::*").inject({}) do |hash, ns|
hash[["xmlns",ns.prefix].compact.join(":")] = ns.href if ns.prefix != "xml"
hash
end
end

# Get the list of decorators given +key+
Expand Down

0 comments on commit c13f9a5

Please sign in to comment.