Commit 3bccb3ba

3bccb3ba20c0Merged

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

Description

Speed up large-input analysis 5.8x (self-host compile 964s -> 166s)

Compiling the legacy compiler with spinelc (spinel_analyze.rb, ~100k AST nodes) took 16 minutes, almost entirely in strcmp. Three causes, all negligible on normal-size programs but quadratic/heavy at self-host scale:

  1. Node-type dispatch by string. Every per-node decision did an strcmp ladder over "CallNode" etc., re-run each fixpoint iteration. Add an integer NodeKind (X-macro keeps the enum and name->kind table in sync), computed once per node by nt_kind and cached on the node. Convert the hot per-expression dispatcher infer_uncached (65 comparisons) to switch on the cached kind; it drops from a top profile entry to 0.07%.

  2. infer_return_types rescanned ALL nodes for EVERY scope to find that scope's ReturnNodes -- O(scopes * nodes), the dominant strcmp source. Group ReturnNodes by owning scope in a single node pass instead.

  3. The (cid, ivname) full-scan narrow helpers (ivar_array_elems_*) are queried once per @ivar[i] expression -- O(nodes^2). Memoize per fixpoint iteration with a generation-stamped cache.

Also adds sp_ivwatch: an SP_IVWATCH=-gated trace of a single ivar's type transitions (zero-cost when unset), used to diagnose cross-method inference cascades that only reproduce in the full self-compile.

Gates: 845 test, 57 bench, optcarrot checksum 59662 -- all unchanged.

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

Details

Commit
3bccb3ba20c0
Parent
41c879704177

src/analyze.cmodifiedView file

@@ -962,6 +962,7 @@
962962
963963 for (int iter = 0; iter < 128; iter++) {
964964 int ch = 0;
965+ sp_narrow_memo_bump(); /* invalidate per-iteration narrow-helper memo */
965966 build_ie_map(c); /* refresh instance_exec receiver-class map each pass */
966967 ch |= infer_write_types(c);
967968 ch |= infer_param_types(c);
@@ -996,7 +997,7 @@
996997 int any = 0;
997998 for (int ci = 0; ci < c->nclasses; ci++)
998999 for (int iv = 0; iv < c->classes[ci].nivars; iv++)
999- if (c->classes[ci].ivar_types[iv] == TY_POLY) { c->classes[ci].ivar_types[iv] = TY_UNKNOWN; any = 1; }
1000+ if (c->classes[ci].ivar_types[iv] == TY_POLY) { const char *_n = c->classes[ci].ivars[iv]; sp_ivwatch(_n && _n[0]=='@' ? _n+1 : _n, "renarrow_reset", TY_POLY, TY_UNKNOWN); c->classes[ci].ivar_types[iv] = TY_UNKNOWN; any = 1; }
10001001 for (int s = 0; s < c->nscopes; s++) {
10011002 Scope *sc = &c->scopes[s];
10021003 for (int i = 0; i < sc->nparams; i++) {
@@ -1007,6 +1008,7 @@
10071008 if (any) {
10081009 for (int iter = 0; iter < 128; iter++) {
10091010 int ch = 0;
1011+ sp_narrow_memo_bump(); /* invalidate per-iteration narrow-helper memo */
10101012 ch |= infer_write_types(c);
10111013 ch |= infer_param_types(c);
10121014 ch |= propagate_prep_params(c);
@@ -1111,7 +1113,7 @@
11111113 for (int ci = 0; ci < c->nclasses; ci++) {
11121114 ClassInfo *cl = &c->classes[ci];
11131115 for (int iv = 0; iv < cl->nivars; iv++)
1114- if (cl->ivar_types[iv] == TY_UNKNOWN) { cl->ivar_types[iv] = TY_POLY; ivar_backstop_changed = 1; }
1116+ if (cl->ivar_types[iv] == TY_UNKNOWN) { const char *_n = cl->ivars[iv]; sp_ivwatch(_n && _n[0]=='@' ? _n+1 : _n, "unknown_backstop", TY_UNKNOWN, TY_POLY); cl->ivar_types[iv] = TY_POLY; ivar_backstop_changed = 1; }
11151117 }
11161118 /* An attr_reader/attr_accessor ivar typed via a writer call (scalar type),
11171119 but whose class has no initialize that writes it, starts nil on fresh
@@ -1364,6 +1366,7 @@
13641366 int ai = comp_ivar_index(&c->classes[a], ivn);
13651367 if (ai < 0) continue;
13661368 TyKind merged = ty_unify(c->classes[a].ivar_types[ai], kt);
1369+ sp_ivwatch(ivn[0] == '@' ? ivn + 1 : ivn, "inherited_merge", c->classes[a].ivar_types[ai], merged);
13671370 if (merged != c->classes[a].ivar_types[ai]) {
13681371 c->classes[a].ivar_types[ai] = merged; prop_changed = 1;
13691372 }