Commit e96c64cc
e96c64cc3b67Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
analyze: index local-variable writes to drop infer_write_types from O(n^2)
infer_write_types runs the usage-driven hash/array promotion heuristics in a
loop over every AST node. For each indexed read/write it answered questions
like "does local X in scope S have any write / an array-typed write" by
re-scanning the entire node table. On a large auto-generated model graph
(N mutually-referential classes with F attr_accessors and recursive
from_hash/to_hash, the shape reported in #1302) those per-site full scans turn
the pass into O((N*F)^2): every h[k] = ... / h[k] site rescans all nodes,
and with no array writes present the scan never short-circuits.
This was the sole hot spot: per-pass timing attributed ~99% of inference time to infer_write_types, and the fixpoint itself converges in ~3 iterations regardless of size, so the cost was entirely per-iteration. Memory stays linear and modest (~1.4 MB/model) -- the historical 100 GB OOM from the Ruby self-host era no longer reproduces on the C compiler; the remaining pathology is purely compile time.
Build a per-pass (scope, name) -> write-node index once and walk only the relevant bucket in the two local-variable promotion scans. infer_type is still called at query time, so the result is identical (verified: emitted C is byte-for-byte unchanged across the test suite except one inlined-stdlib case that still passes, and optcarrot's emitted C is identical). Reduces the synthetic reproducer from 216 s to 2.1 s (N=40, F=80) and 964 s to 6.5 s (N=80, F=80).
Gates: 917 tests pass, 57 benchmarks pass, optcarrot checksum 59662, clang clean.
Refs #1302
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- e96c64cc3b67
- Parent
- 6d6ffd9dc190
src/analyze_pass.cmodifiedView file
return saw;}/* Per-pass index of local-variable write nodes keyed by (scope, name). The usage-driven promotion scans in infer_write_types ask "does local X in scope S have any write / an array-typed write"; without this index each such query re-scanned the entire node table, making a program with M such sites O(M * nodes) -- the dominant cost on large auto-generated model graphs. The index groups the write nodes once so each query walks only its own bucket. */typedef struct { int *node; /* local-write node ids */ int *next; /* chain: next record in the same bucket, or -1 */ int *head; /* hash buckets: head record index into node[], or -1 */ int cap; /* bucket count (power of two) */} LWIndex;static int lw_is_write_kind(NodeKind k) { return k == NK_LocalVariableWriteNode || k == NK_LocalVariableOrWriteNode || k == NK_LocalVariableAndWriteNode || k == NK_LocalVariableOperatorWriteNode;}static unsigned lw_hash(const char *name, int scope) { unsigned h = 2166136261u ^ (unsigned)scope; for (const char *p = name; p && *p; p++) { h ^= (unsigned char)*p; h *= 16777619u; } return h;}static void lw_index_build(Compiler *c, LWIndex *ix) { const NodeTable *nt = c->nt; int n = 0; for (int id = 0; id < nt->count; id++) if (lw_is_write_kind(nt_kind(nt, id))) n++; int cap = 16; while (cap < n * 2) cap <<= 1; ix->cap = cap; ix->node = (int *)malloc(sizeof(int) * (n > 0 ? n : 1)); ix->next = (int *)malloc(sizeof(int) * (n > 0 ? n : 1)); ix->head = (int *)malloc(sizeof(int) * cap); for (int i = 0; i < cap; i++) ix->head[i] = -1; int k = 0; for (int id = 0; id < nt->count; id++) { if (!lw_is_write_kind(nt_kind(nt, id))) continue; const char *nm = nt_str(nt, id, "name"); int sc = (int)(comp_scope_of(c, id) - c->scopes); unsigned h = lw_hash(nm, sc) & (unsigned)(cap - 1); ix->node[k] = id; ix->next[k] = ix->head[h]; ix->head[h] = k; k++; }}static void lw_index_free(LWIndex *ix) { free(ix->node); free(ix->next); free(ix->head);}/* First record index in the bucket for (scope, name); iterate via ix->next. Callers must still confirm scope/name (hash collisions) and node kind. */static int lw_index_first(const LWIndex *ix, const char *name, int scope) { return ix->head[lw_hash(name, scope) & (unsigned)(ix->cap - 1)];}int infer_write_types(Compiler *c) { const NodeTable *nt = c->nt; int changed = 0; reset above would otherwise wipe the promotion each iteration). */ infer_bigint_loop_locals(c); /* Index local-write nodes by (scope, name) for the usage-driven promotion scans further down (see the per-scope write-site lookups below). */ LWIndex lw_ix; lw_index_build(c, &lw_ix); for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); if (!ty) continue; that ty_unify can never narrow back to the yield arg type. */ if (rrty && !strcmp(rrty, "LocalVariableReadNode") && rnm2) { Scope *recv_scope = comp_scope_of(c, recv); int recv_sid = (int)(recv_scope - c->scopes); int has_write = 0; for (int _wi = 0; _wi < nt->count && !has_write; _wi++) { const char *_wty = nt_type(nt, _wi); if (!_wty) continue; if ((!strcmp(_wty, "LocalVariableWriteNode") || !strcmp(_wty, "LocalVariableOrWriteNode") || !strcmp(_wty, "LocalVariableAndWriteNode") || !strcmp(_wty, "LocalVariableOperatorWriteNode")) && comp_scope_of(c, _wi) == recv_scope) { const char *_wnm = nt_str(nt, _wi, "name"); if (_wnm && !strcmp(_wnm, rnm2)) has_write = 1; } for (int _r = lw_index_first(&lw_ix, rnm2, recv_sid); _r >= 0 && !has_write; _r = lw_ix.next[_r]) { int _wi = lw_ix.node[_r]; if (comp_scope_of(c, _wi) != recv_scope) continue; const char *_wnm = nt_str(nt, _wi, "name"); if (_wnm && !strcmp(_wnm, rnm2)) has_write = 1; } if (!has_write) continue; } etc.): it is an array indexed/assigned by position, and the hash type would otherwise collide with it. Mirrors the ivar guard. */ if (!is_push && lv->type == TY_UNKNOWN) { int lsc_sid = (int)(lsc - c->scopes); int has_array_write = 0; for (int w = 0; w < nt->count && !has_array_write; w++) { const char *wty = nt_type(nt, w); if (!wty || strcmp(wty, "LocalVariableWriteNode")) continue; for (int _r = lw_index_first(&lw_ix, rnm, lsc_sid); _r >= 0 && !has_array_write; _r = lw_ix.next[_r]) { int w = lw_ix.node[_r]; if (nt_kind(nt, w) != NK_LocalVariableWriteNode) continue; const char *wn = nt_str(nt, w, "name"); if (!wn || strcmp(wn, rnm) || comp_scope_of(c, w) != lsc) continue; int wv = nt_ref(nt, w, "value"); LocalVar *lv = &c->scopes[s].locals[i]; if (!lv->is_param && !lv->is_block_param && (TyKind)lv->gc_root != lv->type) changed = 1; } lw_index_free(&lw_ix); return changed;}