Speed up hash mode by interning element names directly - #475
Open
Watson1978 wants to merge 1 commit into
Open
Conversation
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.
Ox.load(..., mode: :hash)creates a temporary Ruby String for every element name, associates its encoding, and immediately interns it. Repeated tags therefore allocate a new String even when the name is already interned.Use
rb_intern3()on the name bytes, preserving the parser's encoding and the ASCII-8BIT fallback for file input without an encoding. Keep interning after attribute processing, including attribute key modifier callbacks. This extends the element-name optimization in #474 to hash mode; attribute key handling is unchanged.On Ruby 4.0.6 / arm64, parsing 40,000 records with five text fields per record:
This is a 9.4% reduction in parse time. Each process warmed up twice, then measured seven parses with
GC.startbefore each measurement. These are the medians of four process-level medians, alternating the before/after execution order. All result digests matched. The unchangedhash_no_attrscontrol measured 28.27 ms before and 29.23 ms after, with identical allocation counts.Input:
Validation:
test/hash_load_test.rb: 19 tests, 109 assertions passed against both the original and updated implementation. Also passed with ASCII-8BIT configured as the default encoding before running the suite.*_test.rbsuite, using the Rake test task's exclusions: 344 tests, 7,044 assertions passed.test/tests.rbandtest/sax/sax_test.rbpassed.clang-format --dry-run --Werror ext/ox/hash_load.candgit diff --checkpassed.