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

@@ -151,6 +151,65 @@
151151 return saw;
152152 }
153153
154+/* Per-pass index of local-variable write nodes keyed by (scope, name). The
155+ usage-driven promotion scans in infer_write_types ask "does local X in scope
156+ S have any write / an array-typed write"; without this index each such query
157+ re-scanned the entire node table, making a program with M such sites
158+ O(M * nodes) -- the dominant cost on large auto-generated model graphs. The
159+ index groups the write nodes once so each query walks only its own bucket. */
160+typedef struct {
161+ int *node; /* local-write node ids */
162+ int *next; /* chain: next record in the same bucket, or -1 */
163+ int *head; /* hash buckets: head record index into node[], or -1 */
164+ int cap; /* bucket count (power of two) */
165+} LWIndex;
166+
167+static int lw_is_write_kind(NodeKind k) {
168+ return k == NK_LocalVariableWriteNode || k == NK_LocalVariableOrWriteNode ||
169+ k == NK_LocalVariableAndWriteNode || k == NK_LocalVariableOperatorWriteNode;
170+}
171+
172+static unsigned lw_hash(const char *name, int scope) {
173+ unsigned h = 2166136261u ^ (unsigned)scope;
174+ for (const char *p = name; p && *p; p++) { h ^= (unsigned char)*p; h *= 16777619u; }
175+ return h;
176+}
177+
178+static void lw_index_build(Compiler *c, LWIndex *ix) {
179+ const NodeTable *nt = c->nt;
180+ int n = 0;
181+ for (int id = 0; id < nt->count; id++)
182+ if (lw_is_write_kind(nt_kind(nt, id))) n++;
183+ int cap = 16;
184+ while (cap < n * 2) cap <<= 1;
185+ ix->cap = cap;
186+ ix->node = (int *)malloc(sizeof(int) * (n > 0 ? n : 1));
187+ ix->next = (int *)malloc(sizeof(int) * (n > 0 ? n : 1));
188+ ix->head = (int *)malloc(sizeof(int) * cap);
189+ for (int i = 0; i < cap; i++) ix->head[i] = -1;
190+ int k = 0;
191+ for (int id = 0; id < nt->count; id++) {
192+ if (!lw_is_write_kind(nt_kind(nt, id))) continue;
193+ const char *nm = nt_str(nt, id, "name");
194+ int sc = (int)(comp_scope_of(c, id) - c->scopes);
195+ unsigned h = lw_hash(nm, sc) & (unsigned)(cap - 1);
196+ ix->node[k] = id;
197+ ix->next[k] = ix->head[h];
198+ ix->head[h] = k;
199+ k++;
200+ }
201+}
202+
203+static void lw_index_free(LWIndex *ix) {
204+ free(ix->node); free(ix->next); free(ix->head);
205+}
206+
207+/* First record index in the bucket for (scope, name); iterate via ix->next.
208+ Callers must still confirm scope/name (hash collisions) and node kind. */
209+static int lw_index_first(const LWIndex *ix, const char *name, int scope) {
210+ return ix->head[lw_hash(name, scope) & (unsigned)(ix->cap - 1)];
211+}
212+
154213 int infer_write_types(Compiler *c) {
155214 const NodeTable *nt = c->nt;
156215 int changed = 0;
@@ -170,6 +229,11 @@
170229 reset above would otherwise wipe the promotion each iteration). */
171230 infer_bigint_loop_locals(c);
172231
232+ /* Index local-write nodes by (scope, name) for the usage-driven promotion
233+ scans further down (see the per-scope write-site lookups below). */
234+ LWIndex lw_ix;
235+ lw_index_build(c, &lw_ix);
236+
173237 for (int id = 0; id < nt->count; id++) {
174238 const char *ty = nt_type(nt, id);
175239 if (!ty) continue;
@@ -787,18 +851,13 @@
787851 that ty_unify can never narrow back to the yield arg type. */
788852 if (rrty && !strcmp(rrty, "LocalVariableReadNode") && rnm2) {
789853 Scope *recv_scope = comp_scope_of(c, recv);
854+ int recv_sid = (int)(recv_scope - c->scopes);
790855 int has_write = 0;
791- for (int _wi = 0; _wi < nt->count && !has_write; _wi++) {
792- const char *_wty = nt_type(nt, _wi);
793- if (!_wty) continue;
794- if ((!strcmp(_wty, "LocalVariableWriteNode") ||
795- !strcmp(_wty, "LocalVariableOrWriteNode") ||
796- !strcmp(_wty, "LocalVariableAndWriteNode") ||
797- !strcmp(_wty, "LocalVariableOperatorWriteNode")) &&
798- comp_scope_of(c, _wi) == recv_scope) {
799- const char *_wnm = nt_str(nt, _wi, "name");
800- if (_wnm && !strcmp(_wnm, rnm2)) has_write = 1;
801- }
856+ for (int _r = lw_index_first(&lw_ix, rnm2, recv_sid); _r >= 0 && !has_write; _r = lw_ix.next[_r]) {
857+ int _wi = lw_ix.node[_r];
858+ if (comp_scope_of(c, _wi) != recv_scope) continue;
859+ const char *_wnm = nt_str(nt, _wi, "name");
860+ if (_wnm && !strcmp(_wnm, rnm2)) has_write = 1;
802861 }
803862 if (!has_write) continue;
804863 }
@@ -851,10 +910,11 @@
851910 etc.): it is an array indexed/assigned by position, and the hash type
852911 would otherwise collide with it. Mirrors the ivar guard. */
853912 if (!is_push && lv->type == TY_UNKNOWN) {
913+ int lsc_sid = (int)(lsc - c->scopes);
854914 int has_array_write = 0;
855- for (int w = 0; w < nt->count && !has_array_write; w++) {
856- const char *wty = nt_type(nt, w);
857- if (!wty || strcmp(wty, "LocalVariableWriteNode")) continue;
915+ for (int _r = lw_index_first(&lw_ix, rnm, lsc_sid); _r >= 0 && !has_array_write; _r = lw_ix.next[_r]) {
916+ int w = lw_ix.node[_r];
917+ if (nt_kind(nt, w) != NK_LocalVariableWriteNode) continue;
858918 const char *wn = nt_str(nt, w, "name");
859919 if (!wn || strcmp(wn, rnm) || comp_scope_of(c, w) != lsc) continue;
860920 int wv = nt_ref(nt, w, "value");
@@ -1021,6 +1081,7 @@
10211081 LocalVar *lv = &c->scopes[s].locals[i];
10221082 if (!lv->is_param && !lv->is_block_param && (TyKind)lv->gc_root != lv->type) changed = 1;
10231083 }
1084+ lw_index_free(&lw_ix);
10241085 return changed;
10251086 }
10261087