Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions benchmark/attribute.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
loop_count: 1000
contexts:
- gems:
rexml: 3.2.6
require: false
prelude: require 'rexml'
- name: master
prelude: |
$LOAD_PATH.unshift(File.expand_path("lib"))
require 'rexml'
- name: 3.2.6(YJIT)
gems:
rexml: 3.2.6
require: false
prelude: |
require 'rexml'
RubyVM::YJIT.enable
- name: master(YJIT)
prelude: |
$LOAD_PATH.unshift(File.expand_path("lib"))
require 'rexml'
RubyVM::YJIT.enable

prelude: |
require 'rexml/document'

xml_source = "<deepest x:with_ns='foo' without_ns='bar'></deepest>"
100.times do
xml_source = "<nest>#{xml_source}</nest>"
end
xml_source = "<root xmlns:x='xyz'>#{xml_source}</root>"

document = REXML::Document.new(xml_source)
deepest_node = document.elements["//deepest"]

benchmark:
with_ns: deepest_node.attribute("with_ns", "xyz")
without_ns: deepest_node.attribute("without_ns")
9 changes: 2 additions & 7 deletions lib/rexml/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1276,16 +1276,11 @@ def [](name_or_index)
# document.root.attribute("x", "a") # => a:x='a:x'
#
def attribute( name, namespace=nil )
prefix = nil
if namespaces.respond_to? :key
prefix = namespaces.key(namespace) if namespace
else
prefix = namespaces.index(namespace) if namespace
end
prefix = namespaces.key(namespace) if namespace
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prefix = namespaces.key(namespace) if namespace
prefix = namespaces[namespace] if namespace

may be faster than namespaces.key(namespace).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespaces.key(namespace) means namespaces.invert[namespace]. Your suggestion changes the code's meaning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry.

prefix = nil if prefix == 'xmlns'

ret_val =
attributes.get_attribute( "#{prefix ? prefix + ':' : ''}#{name}" )
attributes.get_attribute( prefix ? "#{prefix}:#{name}" : name )

return ret_val unless ret_val.nil?
return nil if prefix.nil?
Expand Down