-
Notifications
You must be signed in to change notification settings - Fork 65
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
Conversation
`Element#namespaces` is heavy method because this method needs to traverse all ancestors of the element. `Element#attribute` calls `namespaces` redundantly, so it is much slower. This commit reduces `namespaces` calls in `Element#attribute`. Also, this commit removes a redundant `respond_to?` because `namespaces` must return `Hash` in the current implementation.
benchmark/attribute.yaml
Outdated
attribute_with_ns: | | ||
deepest_node.attribute("with_ns", "xyz") | ||
attribute_without_ns: | |
There was a problem hiding this comment.
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?
else | ||
prefix = namespaces.index(namespace) if namespace | ||
end | ||
prefix = namespaces.key(namespace) if namespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefix = namespaces.key(namespace) if namespace | |
prefix = namespaces[namespace] if namespace |
may be faster than namespaces.key(namespace)
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, sorry.
Co-authored-by: Sutou Kouhei <[email protected]>
Element#namespaces
is heavy method because this method needs to traverse all ancestors of the element.Element#attribute
callsnamespaces
redundantly, so it is much slower.This PR reduces
namespaces
calls inElement#attribute
. Also, this PR removes a redundantrespond_to?
becausenamespaces
must returnHash
in the current implementation.Below is the result of a benchmark for this on my laptop.
This result shows that
Element#attribute
is now 6500x faster than the old implementation ifnamespace
is not supplied.It seems strange that it is slower when YJIT is enabled, but we believe this is a separate issue.
Thank you.