Commit 22cb8b02
22cb8b0286a5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
perf: avoid split() allocations in parse_id_list hot path
parse_id_list is called ~120 K times during self-host compilation
of spinel_codegen.rb itself. The old body used s.split(",") which
allocated a temporary StrArray plus one string per element on every
call — profile of bin2 compiling codegen had sp_str_split at 20.7%
children time and sp_gc_alloc at 54.5%.
Rewrite to walk s.bytes manually, accumulating digits and pushing
ints directly. Keeps the same semantics (comma-separated non-negative
integers) but drops N+1 allocations per call to 2 (one bytes array,
one result).
Self-host compile of spinel_codegen.rb: 92 s -> 78 s (-15%). Full bootstrap wall: 6:19 -> 5:59 (-5%). CRuby stage slips a few % because we lose its C-optimized split, but that only runs once during initial bootstrap.
74 tests / 55 benchmarks pass. gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 22cb8b0286a5
- Parent
- 3bd5f244e390
spinel_codegen.rbmodifiedView file
end # Parse comma-sep node IDs into IntArray # Parse comma-sep node IDs into IntArray. Manually walks bytes to avoid # allocating the intermediate StrArray + substrings that `String#split` # would produce — this is called ~100 K times during bootstrap. def parse_id_list(s) if s == "" return [] end parts = s.split(",") result = [] bs = s.bytes i = 0 while i < parts.length result.push(parts[i].to_i) n = bs.length num = 0 while i < n b = bs[i] if b == 44 # ',' result.push(num) num = 0 else num = num * 10 + (b - 48) end i = i + 1 end result.push(num) result end