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

@@ -240,18 +240,29 @@
240240 end
241241
242242
243- # Parse comma-sep node IDs into IntArray
243+ # Parse comma-sep node IDs into IntArray. Manually walks bytes to avoid
244+ # allocating the intermediate StrArray + substrings that `String#split`
245+ # would produce — this is called ~100 K times during bootstrap.
244246 def parse_id_list(s)
245247 if s == ""
246248 return []
247249 end
248- parts = s.split(",")
249250 result = []
251+ bs = s.bytes
250252 i = 0
251- while i < parts.length
252- result.push(parts[i].to_i)
253+ n = bs.length
254+ num = 0
255+ while i < n
256+ b = bs[i]
257+ if b == 44 # ','
258+ result.push(num)
259+ num = 0
260+ else
261+ num = num * 10 + (b - 48)
262+ end
253263 i = i + 1
254264 end
265+ result.push(num)
255266 result
256267 end
257268