Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Element#attribute implementation as 6500x faster #146

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions benchmark/attribute.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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.root
until deepest_node.elements.size.zero?
deepest_node = deepest_node.elements.first
end
makenowjust marked this conversation as resolved.
Show resolved Hide resolved

benchmark:
attribute_with_ns: |
deepest_node.attribute("with_ns", "xyz")
attribute_without_ns: |
Copy link
Member

Choose a reason for hiding this comment

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

attribute_ is redundant because this file is attribute.yaml.
Can we use with_ns/without_ns or something?

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