Repository navigation
Speed up hash_no_attrs by interning element names directly - #474
Merged
Merged
Conversation
Watson1978
marked this pull request as ready for review
September 5, 2026 12:42
ohler55
approved these changes
Sep 6, 2026
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_no_attrs)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()directly on the name bytes, preserving the parser's encoding and the ASCII-8BIT fallback ofrb_str_new2(). This removes one temporary String per element. The change is limited tohash_no_attrs.On Ruby 4.0.6 / arm64, parsing 40,000 records with five text fields per record:
This is a 14% reduction in parse time. Each process warmed up twice, then measured seven parses with
GC.startbefore each measurement. Results above are the medians of four process-level medians, alternating the before/after execution order. All result digests matched.The input was generated with:
Validation:
element_key_mod. These tests also pass against the original implementation.Ox.load_filecoverage for the ASCII-8BIT fallback with Symbol and String keys. The test fails when that fallback is deliberately changed to UTF-8 in an isolated build.test/hash_load_test.rb: 15 tests, 72 assertions passed.*_test.rbsuite (using the Rake test task's exclusions): 340 tests, 7,007 assertions passed.test/tests.rbpassed.git diff --checkpassed.