Improve Element#attribute implementation as 6500x faster#146
Merged
kou merged 3 commits intoruby:masterfrom Jun 13, 2024
Merged
Improve Element#attribute implementation as 6500x faster#146kou merged 3 commits intoruby:masterfrom
Element#attribute implementation as 6500x faster#146kou merged 3 commits intoruby:masterfrom
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.
kou
reviewed
Jun 13, 2024
Comment on lines
+40
to
+42
| attribute_with_ns: | | ||
| deepest_node.attribute("with_ns", "xyz") | ||
| attribute_without_ns: | |
Member
There was a problem hiding this comment.
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 |
Member
There was a problem hiding this comment.
Suggested change
| prefix = namespaces.key(namespace) if namespace | |
| prefix = namespaces[namespace] if namespace |
may be faster than namespaces.key(namespace).
Contributor
Author
There was a problem hiding this comment.
namespaces.key(namespace) means namespaces.invert[namespace]. Your suggestion changes the code's meaning.
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Element#namespacesis heavy method because this method needs to traverse all ancestors of the element.Element#attributecallsnamespacesredundantly, so it is much slower.This PR reduces
namespacescalls inElement#attribute. Also, this PR removes a redundantrespond_to?becausenamespacesmust returnHashin the current implementation.Below is the result of a benchmark for this on my laptop.
This result shows that
Element#attributeis now 6500x faster than the old implementation ifnamespaceis not supplied.It seems strange that it is slower when YJIT is enabled, but we believe this is a separate issue.
Thank you.