Commit 0229f80
Prevent ObjectSpace.count_objects from allocating extra arrays
`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>1 parent 3956308 commit 0229f80
1 file changed
+8
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2498 | 2498 | | |
2499 | 2499 | | |
2500 | 2500 | | |
| 2501 | + | |
2501 | 2502 | | |
2502 | 2503 | | |
2503 | 2504 | | |
2504 | 2505 | | |
2505 | 2506 | | |
2506 | 2507 | | |
2507 | 2508 | | |
| 2509 | + | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
2508 | 2513 | | |
2509 | 2514 | | |
2510 | 2515 | | |
| |||
2517 | 2522 | | |
2518 | 2523 | | |
2519 | 2524 | | |
2520 | | - | |
2521 | | - | |
2522 | | - | |
| 2525 | + | |
| 2526 | + | |
| 2527 | + | |
2523 | 2528 | | |
2524 | 2529 | | |
2525 | 2530 | | |
| |||
0 commit comments