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

@@ -222,7 +222,19 @@
222222 # heavily during the fixpoint and otherwise re-splits
223223 # @cls_meth_names[ci] on every call. Key = "<ci>:<mname>".
224224 # Invalidated on append_cls_meth (when @cls_meth_names mutates).
225- @cls_meth_idx_cache = {}
225+ # cls_find_method_direct keys this cache by an integer composite of
226+ # (ci, interned-method-name-id) rather than a "<ci>:<mname>" string,
227+ # so the O(classes^2) calls on large mutually-referential class graphs
228+ # allocate no key strings (#1302 -- the string key build, an int-to-s
229+ # plus two joins per call, was the dominant analyzer allocation churn).
230+ # Method names are interned to small ints in @mname_id (stable for the
231+ # run); the cache itself is int->int. The {-1 => -1} seed pins the
232+ # variant to int_int_hash (an empty {} otherwise infers as str_int_hash
233+ # in the self-hosted build); -1 is never a real key (ck = ci*1e7+mid is
234+ # always >= 0).
235+ @cls_meth_idx_cache = {-1 => -1}
236+ @mname_id = {}
237+ @mname_id_next = 0
226238 # Same shape for cls_method_return — top String#split caller.
227239 # Key = "<ci>:<mname>", value = the recorded return type.
228240 # Invalidated on append_cls_meth + at every refresh of
@@ -10636,7 +10648,7 @@
1063610648 @cls_meth_bodies[ci] = cur_bodies_u.join(";")
1063710649 @cls_meth_defaults[ci] = cur_defaults_u.join("|")
1063810650 @cls_meth_ptypes_empty[ci] = cur_pempty_u.join("|")
10639- @cls_meth_idx_cache = {}
10651+ @cls_meth_idx_cache = {-1 => -1}
1064010652 @cls_meth_return_cache = {}
1064110653 end
1064210654 end
@@ -10890,7 +10902,7 @@
1089010902 end
1089110903 @cls_meth_params_version = @cls_meth_params_version + 1
1089210904 @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1
10893- @cls_meth_idx_cache = {}
10905+ @cls_meth_idx_cache = {-1 => -1}
1089410906 @cls_meth_return_cache = {}
1089510907 # Append (mname, syn_name) to the chain. Chain entry shape:
1089610908 # `<mname>=<syn0>,<syn1>,...` joined by `;` across mnames.
@@ -11589,7 +11601,7 @@
1158911601 @cls_meth_bodies[ci] = cur_bodies.join(";")
1159011602 @cls_meth_defaults[ci] = cur_defaults.join("|")
1159111603 @cls_meth_ptypes_empty[ci] = cur_pempty.join("|")
11592- @cls_meth_idx_cache = {}
11604+ @cls_meth_idx_cache = {-1 => -1}
1159311605 @cls_meth_return_cache = {}
1159411606 return
1159511607 end
@@ -11614,7 +11626,7 @@
1161411626 @cls_meth_ptypes_empty[ci] = ""
1161511627 end
1161611628 # Invalidate split caches keyed on @cls_meth_names / @cls_meth_returns.
11617- @cls_meth_idx_cache = {}
11629+ @cls_meth_idx_cache = {-1 => -1}
1161811630 @cls_meth_return_cache = {}
1161911631 end
1162011632
@@ -28519,8 +28531,23 @@
2851928531 # check. Used at method-emit time to set @current_method_block_param
2852028532 # so block_given? can resolve to (lv_<name> != NULL).
2852128533
28534+ # Intern a method name to a small integer id, stable for the run.
28535+ def mname_to_id(mname)
28536+ if @mname_id.key?(mname)
28537+ return @mname_id[mname]
28538+ end
28539+ mid = @mname_id_next
28540+ @mname_id[mname] = mid
28541+ @mname_id_next = @mname_id_next + 1
28542+ mid
28543+ end
28544+
2852228545 def cls_find_method_direct(ci, mname)
28523- ck = ci.to_s + ":" + mname
28546+ # Integer composite key (ci, interned mname) -- no per-call string
28547+ # allocation. key? gates the lookup so a miss returns -1, not the
28548+ # typed-hash missing-key 0 (#801). The 1e7 stride exceeds any program's
28549+ # distinct-method-name count, so (ci, mid) pairs never collide.
28550+ ck = ci * 10000000 + mname_to_id(mname)
2852428551 if @cls_meth_idx_cache.key?(ck)
2852528552 return @cls_meth_idx_cache[ck]
2852628553 end