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:
-
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%.
-
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.
-
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
for (int iter = 0; iter < 128; iter++) { int ch = 0; sp_narrow_memo_bump(); /* invalidate per-iteration narrow-helper memo */ build_ie_map(c); /* refresh instance_exec receiver-class map each pass */ ch |= infer_write_types(c); ch |= infer_param_types(c); int any = 0; for (int ci = 0; ci < c->nclasses; ci++) for (int iv = 0; iv < c->classes[ci].nivars; iv++) if (c->classes[ci].ivar_types[iv] == TY_POLY) { c->classes[ci].ivar_types[iv] = TY_UNKNOWN; any = 1; } 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; } for (int s = 0; s < c->nscopes; s++) { Scope *sc = &c->scopes[s]; for (int i = 0; i < sc->nparams; i++) { if (any) { for (int iter = 0; iter < 128; iter++) { int ch = 0; sp_narrow_memo_bump(); /* invalidate per-iteration narrow-helper memo */ ch |= infer_write_types(c); ch |= infer_param_types(c); ch |= propagate_prep_params(c); for (int ci = 0; ci < c->nclasses; ci++) { ClassInfo *cl = &c->classes[ci]; for (int iv = 0; iv < cl->nivars; iv++) if (cl->ivar_types[iv] == TY_UNKNOWN) { cl->ivar_types[iv] = TY_POLY; ivar_backstop_changed = 1; } 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; } } /* An attr_reader/attr_accessor ivar typed via a writer call (scalar type), but whose class has no initialize that writes it, starts nil on fresh int ai = comp_ivar_index(&c->classes[a], ivn); if (ai < 0) continue; TyKind merged = ty_unify(c->classes[a].ivar_types[ai], kt); sp_ivwatch(ivn[0] == '@' ? ivn + 1 : ivn, "inherited_merge", c->classes[a].ivar_types[ai], merged); if (merged != c->classes[a].ivar_types[ai]) { c->classes[a].ivar_types[ai] = merged; prop_changed = 1; }src/analyze_infer.cmodifiedView file
Loading diff…
src/analyze_internal.hmodifiedView file
Loading diff…
src/analyze_pass.cmodifiedView file
Loading diff…
src/analyze_scope.cmodifiedView file
Loading diff…
src/node_table.cmodifiedView file
Loading diff…
src/node_table.hmodifiedView file
Loading diff…