Commit e314f84e
e314f84e9ac8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Key the method-index cache by an interned integer, not a string
cls_find_method_direct ran O(classes^2) times on large
mutually-referential class graphs and built its cache key as
ci.to_s + ":" + mname on every call -- an int-to-string plus two
joins. That per-call key allocation was the dominant analyzer
allocation churn (#1302) and CPU (sp_int_to_s was ~17% of analysis).
Intern method names to small ints (@mname_id) and key the cache by an
integer composite ci * 1e7 + mname_id, so resolution allocates no key
strings. The cache is now int->int (int_int_hash); the {-1 => -1} seed
pins that variant since a bare empty {} infers as str_int_hash in the
self-hosted build, and -1 is never a real key (the composite is always
= 0). int_int_hash tracks occupancy with a separate used[] array, so -1 is a safe key value.
This removes the O(N^2) key-build term: a 160-model synthetic drops 586 -> 137 MB peak RSS (with the pass-boundary collection) and analysis speeds up markedly. It does NOT flatten the residual super-linear growth -- cls_find_method_direct is still called O(N^2) times -- which is a separate, deeper change. IR output is byte-identical; full suite + self-host fixpoint + optcarrot all green.
Supersedes the ":" prefix-cache approach.
Refs #1302
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- e314f84e9ac8
- Parent
- 5cb15227e083
spinel_analyze.rbmodifiedView file
# heavily during the fixpoint and otherwise re-splits # @cls_meth_names[ci] on every call. Key = "<ci>:<mname>". # Invalidated on append_cls_meth (when @cls_meth_names mutates). @cls_meth_idx_cache = {} # cls_find_method_direct keys this cache by an integer composite of # (ci, interned-method-name-id) rather than a "<ci>:<mname>" string, # so the O(classes^2) calls on large mutually-referential class graphs # allocate no key strings (#1302 -- the string key build, an int-to-s # plus two joins per call, was the dominant analyzer allocation churn). # Method names are interned to small ints in @mname_id (stable for the # run); the cache itself is int->int. The {-1 => -1} seed pins the # variant to int_int_hash (an empty {} otherwise infers as str_int_hash # in the self-hosted build); -1 is never a real key (ck = ci*1e7+mid is # always >= 0). @cls_meth_idx_cache = {-1 => -1} @mname_id = {} @mname_id_next = 0 # Same shape for cls_method_return — top String#split caller. # Key = "<ci>:<mname>", value = the recorded return type. # Invalidated on append_cls_meth + at every refresh of @cls_meth_bodies[ci] = cur_bodies_u.join(";") @cls_meth_defaults[ci] = cur_defaults_u.join("|") @cls_meth_ptypes_empty[ci] = cur_pempty_u.join("|") @cls_meth_idx_cache = {} @cls_meth_idx_cache = {-1 => -1} @cls_meth_return_cache = {} end end end @cls_meth_params_version = @cls_meth_params_version + 1 @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1 @cls_meth_idx_cache = {} @cls_meth_idx_cache = {-1 => -1} @cls_meth_return_cache = {} # Append (mname, syn_name) to the chain. Chain entry shape: # `<mname>=<syn0>,<syn1>,...` joined by `;` across mnames. @cls_meth_bodies[ci] = cur_bodies.join(";") @cls_meth_defaults[ci] = cur_defaults.join("|") @cls_meth_ptypes_empty[ci] = cur_pempty.join("|") @cls_meth_idx_cache = {} @cls_meth_idx_cache = {-1 => -1} @cls_meth_return_cache = {} return end @cls_meth_ptypes_empty[ci] = "" end # Invalidate split caches keyed on @cls_meth_names / @cls_meth_returns. @cls_meth_idx_cache = {} @cls_meth_idx_cache = {-1 => -1} @cls_meth_return_cache = {} end # check. Used at method-emit time to set @current_method_block_param # so block_given? can resolve to (lv_<name> != NULL). # Intern a method name to a small integer id, stable for the run. def mname_to_id(mname) if @mname_id.key?(mname) return @mname_id[mname] end mid = @mname_id_next @mname_id[mname] = mid @mname_id_next = @mname_id_next + 1 mid end def cls_find_method_direct(ci, mname) ck = ci.to_s + ":" + mname # Integer composite key (ci, interned mname) -- no per-call string # allocation. key? gates the lookup so a miss returns -1, not the # typed-hash missing-key 0 (#801). The 1e7 stride exceeds any program's # distinct-method-name count, so (ci, mid) pairs never collide. ck = ci * 10000000 + mname_to_id(mname) if @cls_meth_idx_cache.key?(ck) return @cls_meth_idx_cache[ck] end