Commit 9b13d2e8

9b13d2e8dc15Merged

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

Description

analyze: index instance-variable writes to drop infer_write_types ivar scans from O(n^2)

03e669b indexed local-variable writes by (scope, name) to kill the dominant O((N*F)^2) cost in infer_write_types on large model graphs, but the two instance-variable promotion guards on the InstanceVariableReadNode branch were left scanning the entire node table per query:

  • the empty-{} guard ("are all writes to @h empty hash literals?"), and
  • the typed/array-write guard ("does @x have a non-nil-typed or [] write?").

On an ivar-hash-heavy shape (N classes x F ivar hashes, each @hN = {} in initialize and read as @hN[k] / @hN.fetch(k)) every ivar index/read site re-scanned all NF ivar writes, restoring the same O((NF)^2) pathology the local-variable index fixed.

Add an ivar-write index keyed by ivar name (the ivar analogue of LWIndex, reusing its bucket layout; ivar scans have no scope key and filter class at the call site). Both guards now walk only their own name bucket. infer_type is still called at query time, so the result is identical: emitted C is byte-for-byte unchanged on optcarrot and on the synthetic reproducers.

Synthetic ivar-hash reproducer (analyze+codegen only, -c):

N=F=60: 13.1 s -> 0.76 s (byte-identical C) N=F=90: 156.8 s -> 4.40 s (byte-identical C)

Gates: 950 tests pass, 57 benchmarks pass, optcarrot checksum 59662, clang clean.

Refs #1302

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
9b13d2e8dc15
Parent
26e8df179100

src/analyze_pass.cmodifiedView file

@@ -210,6 +210,53 @@
210210 return ix->head[lw_hash(name, scope) & (unsigned)(ix->cap - 1)];
211211 }
212212
213+/* Per-pass index of instance-variable write nodes keyed by ivar name -- the
214+ ivar analogue of LWIndex. The usage-driven promotion scans below ask "does
215+ @x have a non-empty-hash / typed write"; without this each query re-scanned
216+ the whole node table, the dominant cost on ivar-heavy model graphs (the
217+ #1302 from_hash/to_hash shape). Indexes both plain and `||=` ivar writes;
218+ callers filter by node kind and class. Reuses LWIndex's layout (scope-less,
219+ so the bucket key is name only). */
220+static int ivw_is_write_kind(NodeKind k) {
221+ return k == NK_InstanceVariableWriteNode || k == NK_InstanceVariableOrWriteNode;
222+}
223+
224+static unsigned ivw_hash(const char *name) {
225+ unsigned h = 2166136261u;
226+ for (const char *p = name; p && *p; p++) { h ^= (unsigned char)*p; h *= 16777619u; }
227+ return h;
228+}
229+
230+static void ivw_index_build(Compiler *c, LWIndex *ix) {
231+ const NodeTable *nt = c->nt;
232+ int n = 0;
233+ for (int id = 0; id < nt->count; id++)
234+ if (ivw_is_write_kind(nt_kind(nt, id))) n++;
235+ int cap = 16;
236+ while (cap < n * 2) cap <<= 1;
237+ ix->cap = cap;
238+ ix->node = (int *)malloc(sizeof(int) * (n > 0 ? n : 1));
239+ ix->next = (int *)malloc(sizeof(int) * (n > 0 ? n : 1));
240+ ix->head = (int *)malloc(sizeof(int) * cap);
241+ for (int i = 0; i < cap; i++) ix->head[i] = -1;
242+ int k = 0;
243+ for (int id = 0; id < nt->count; id++) {
244+ if (!ivw_is_write_kind(nt_kind(nt, id))) continue;
245+ const char *nm = nt_str(nt, id, "name");
246+ unsigned h = ivw_hash(nm) & (unsigned)(cap - 1);
247+ ix->node[k] = id;
248+ ix->next[k] = ix->head[h];
249+ ix->head[h] = k;
250+ k++;
251+ }
252+}
253+
254+/* First record index in the bucket for ivar `name`; iterate via ix->next.
255+ Callers must still confirm name (hash collisions), node kind, and class. */
256+static int ivw_index_first(const LWIndex *ix, const char *name) {
257+ return ix->head[ivw_hash(name) & (unsigned)(ix->cap - 1)];
258+}
259+
213260 int infer_write_types(Compiler *c) {
214261 const NodeTable *nt = c->nt;
215262 int changed = 0;
@@ -233,6 +280,10 @@
233280 scans further down (see the per-scope write-site lookups below). */
234281 LWIndex lw_ix;
235282 lw_index_build(c, &lw_ix);
283+ /* Index ivar-write nodes by name for the empty-hash / typed-write promotion
284+ guards on the InstanceVariableReadNode branches below. */
285+ LWIndex ivw_ix;
286+ ivw_index_build(c, &ivw_ix);
236287
237288 for (int id = 0; id < nt->count; id++) {
238289 const char *ty = nt_type(nt, id);
@@ -824,11 +875,8 @@
824875 if (rslot == TY_UNKNOWN) {
825876 const char *pin = nt_str(nt, recv, "name");
826877 int blocked = 0;
827- for (int wi = 0; wi < nt->count && !blocked; wi++) {
828- const char *wty = nt_type(nt, wi);
829- if (!wty) continue;
830- if (strcmp(wty, "InstanceVariableWriteNode") &&
831- strcmp(wty, "InstanceVariableOrWriteNode")) continue;
878+ for (int _r = ivw_index_first(&ivw_ix, pin); _r >= 0 && !blocked; _r = ivw_ix.next[_r]) {
879+ int wi = ivw_ix.node[_r];
832880 const char *wnm = nt_str(nt, wi, "name");
833881 if (!wnm || !pin || strcmp(wnm, pin)) continue;
834882 int wv = nt_ref(nt, wi, "value");
@@ -948,8 +996,9 @@
948996 its empty literal and is unaffected. */
949997 if (!is_push && *slot == TY_UNKNOWN && inm) {
950998 int has_typed_write = 0;
951- for (int _wi = 0; _wi < nt->count && !has_typed_write; _wi++) {
952- if (!nt_type(nt, _wi) || strcmp(nt_type(nt, _wi), "InstanceVariableWriteNode")) continue;
999+ for (int _r = ivw_index_first(&ivw_ix, inm); _r >= 0 && !has_typed_write; _r = ivw_ix.next[_r]) {
1000+ int _wi = ivw_ix.node[_r];
1001+ if (nt_kind(nt, _wi) != NK_InstanceVariableWriteNode) continue;
9531002 const char *_wnm = nt_str(nt, _wi, "name");
9541003 if (!_wnm || strcmp(_wnm, inm)) continue;
9551004 Scope *_ws = comp_scope_of(c, _wi);
@@ -1082,6 +1131,7 @@
10821131 if (!lv->is_param && !lv->is_block_param && (TyKind)lv->gc_root != lv->type) changed = 1;
10831132 }
10841133 lw_index_free(&lw_ix);
1134+ lw_index_free(&ivw_ix);
10851135 return changed;
10861136 }
10871137