Commit c3c59c98

c3c59c98c111Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

perf: cache parse_id_list results in a ptr_array pool

Now that PtrArray scans its elements during GC (previous commit), the cache attempt that was reverted earlier on this branch is safe: the IntArrays we stash in @parse_id_pool stay reachable through self → parse_id_pool → elements.

AST list fields are immutable once loaded, so the same comma-separated string always parses to the same IntArray. Callers already treat the result as read-only (verified by grep — no .push/.pop/[]= on any var that ever holds a parse_id_list return).

Implementation: str_int_hash @parse_id_cache maps the input string to an index into ptr_array<int_array> @parse_id_pool. Seed the pool with [[0]] so Spinel infers int_array_ptr_array (otherwise an empty [] is defaulted to int_array).

Self-host compile of spinel_codegen.rb: ~70 s -> ~42 s (-40%). Full bootstrap wall: 5:14 -> 4:26 (-15%). Cumulative since baseline 6:19: -30%.

Bootstrap byte-identical, 74 tests / 55 benchmarks pass.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
c3c59c98c111
Parent
59477089ef6e

spinel_codegen.rbmodifiedView file

@@ -173,6 +173,15 @@
173173 @needs_regexp = 0
174174 @regexp_patterns = "".split(",")
175175 @regexp_flags = "".split(",")
176+
177+ # Cache for parse_id_list: AST list fields never change once loaded,
178+ # so the parsed IntArray can be shared across callers. The `[[0]]`
179+ # literal teaches Spinel that @parse_id_pool is ptr_array<int_array>;
180+ # slot 0 is a reserved dummy. PtrArray now scans its elements, so
181+ # cached IntArrays stay reachable.
182+ @parse_id_cache = {}
183+ @parse_id_pool = [[0]]
184+
176185 @needs_stringio = 0
177186 @needs_proc = 0
178187 @proc_counter = 0
@@ -243,10 +252,16 @@
243252 # Parse comma-sep node IDs into IntArray. Manually walks bytes to avoid
244253 # allocating the intermediate StrArray + substrings that `String#split`
245254 # would produce — this is called ~100 K times during bootstrap.
255+ # Results are cached by input string: AST fields are immutable once
256+ # parsed, so the same IntArray can be shared across callers. Callers
257+ # must treat the result as read-only.
246258 def parse_id_list(s)
247259 if s == ""
248260 return []
249261 end
262+ if @parse_id_cache.key?(s)
263+ return @parse_id_pool[@parse_id_cache[s]]
264+ end
250265 result = []
251266 bs = s.bytes
252267 i = 0
@@ -263,6 +278,8 @@
263278 i = i + 1
264279 end
265280 result.push(num)
281+ @parse_id_cache[s] = @parse_id_pool.length
282+ @parse_id_pool.push(result)
266283 result
267284 end
268285