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
@needs_regexp = 0 @regexp_patterns = "".split(",") @regexp_flags = "".split(",") # Cache for parse_id_list: AST list fields never change once loaded, # so the parsed IntArray can be shared across callers. The `[[0]]` # literal teaches Spinel that @parse_id_pool is ptr_array<int_array>; # slot 0 is a reserved dummy. PtrArray now scans its elements, so # cached IntArrays stay reachable. @parse_id_cache = {} @parse_id_pool = [[0]] @needs_stringio = 0 @needs_proc = 0 @proc_counter = 0 # 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. # Results are cached by input string: AST fields are immutable once # parsed, so the same IntArray can be shared across callers. Callers # must treat the result as read-only. def parse_id_list(s) if s == "" return [] end if @parse_id_cache.key?(s) return @parse_id_pool[@parse_id_cache[s]] end result = [] bs = s.bytes i = 0 i = i + 1 end result.push(num) @parse_id_cache[s] = @parse_id_pool.length @parse_id_pool.push(result) result end