Prevent ObjectSpace.count_objects from allocating extra arrays#13906
Merged
Prevent ObjectSpace.count_objects from allocating extra arrays#13906
Conversation
`ObjectSpace.count_objects` could cause an unintended array allocation.
It returns a hash like `{ :T_ARRAY => 100, :T_STRING => 100, ... }`, so
it creates the key symbol (e.g., `:T_STRING`) for the first time. On
rare occations, this symbol creation internally allocates a new array
for symbol management.
This led to a problematic side effect where calling `count_objects`
twice in a row could produce inconsistent results: the first call would
trigger the hidden array allocation, and the second call would then
report an increased count for `:T_ARRAY`.
This behavior caused test failures in `test/ruby/test_allocation.rb`,
which performs a baseline measurement before an operation and then
asserts the exact number of new allocations.
https://rubyci.s3.amazonaws.com/openbsd-current/ruby-master/log/20250716T053005Z.fail.html.gz
> 1) Failure:
> TestAllocation::ProcCall::WithBlock#test_ruby2_keywords [...]:
> Expected 1 array allocations for "r2k.(1, a: 2, &block)", but 2 arrays allocated.
This change resolves the issue by pre-interning all key symbols used by
`ObjectSpace.count_objects` before its counting. This eliminates the
side effect and ensures the stability of allocation-sensitive tests.
Co-authored-by: Koichi Sasada <ko1@atdot.net>
b4d59b9 to
0229f80
Compare
eregon
added a commit
to eregon/ruby
that referenced
this pull request
Aug 25, 2025
* To not count them as program allocations. * Similar to ruby#13906
hsbt
pushed a commit
that referenced
this pull request
Aug 26, 2025
* To not count them as program allocations. * Similar to #13906
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.
ObjectSpace.count_objectscould cause an unintended array allocation. It returns a hash like{ :T_ARRAY => 100, :T_STRING => 100, ... }, so it creates the key symbol (e.g.,:T_STRING) for the first time. On rare occations, this symbol creation internally allocates a new array for symbol management.This led to a problematic side effect where calling
count_objectstwice in a row could produce inconsistent results: the first call would trigger the hidden array allocation, and the second call would then report an increased count for:T_ARRAY.This behavior caused test failures in
test/ruby/test_allocation.rb, which performs a baseline measurement before an operation and then asserts the exact number of new allocations.https://rubyci.s3.amazonaws.com/openbsd-current/ruby-master/log/20250716T053005Z.fail.html.gz
This change resolves the issue by pre-interning all key symbols used by
ObjectSpace.count_objectsbefore its counting. This eliminates the side effect and ensures the stability of allocation-sensitive tests.