Commit 0f1f816c

0f1f816c3a55Merged

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

Description

analyze: re-narrow an all-int POLY_ARRAY ivar to IntArray (drop pixel boxing)

perf showed optcarrot spending ~17% in poly array ops (sp_PolyArray_push / sp_poly_arr_get / boxing) because @output_pixels was typed POLY_ARRAY even though every element is an int (@output_pixels << @output_color[i] where @output_color is an IntArray). The monotonic usage pass locks the slot to POLY_ARRAY the first time it sees a push whose element type is still unknown -- here @output_color has not yet settled to IntArray -- and never re-narrows.

Add a post-fixpoint pass: an ivar still typed POLY_ARRAY whose every write is an int push / int []= / empty-or-int-array assignment is re-narrowed to IntArray, so the per-frame pixel buffer is stored and read unboxed. Strictly conservative -- any non-int source or unmodeled write shape keeps it poly.

optcarrot ~700 -> ~730 fps (best 731), checksum 59662 unchanged (pixel exact). make test 999 / make bench 57.

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

Details

Commit
0f1f816c3a55
Parent
d0d65581a374

src/analyze.cmodifiedView file

@@ -1902,6 +1902,65 @@
19021902 is definitively an array, not a hash. */
19031903 if (lv && !lv->rbs_seeded && (lv->type == TY_UNKNOWN || ty_is_hash(lv->type))) lv->type = TY_POLY_ARRAY;
19041904 }
1905+ /* Re-narrow a POLY_ARRAY ivar to IntArray when every element source is now
1906+ (post-fixpoint) int. The monotonic usage pass locks the slot to POLY_ARRAY
1907+ the first time it sees a push whose element type is still unknown -- e.g.
1908+ `@output_pixels << @output_color[i]` evaluated before @output_color settled
1909+ to IntArray. Once the element types settle, a slot whose only writes are int
1910+ pushes / int `[]=` / empty-or-int-array assignments can drop the per-element
1911+ boxing (optcarrot's per-frame pixel buffer is the motivating case). Strictly
1912+ conservative: any non-int source, or a write shape we don't model, keeps it
1913+ poly. */
1914+ for (int ci = 0; ci < c->nclasses; ci++) {
1915+ ClassInfo *cl = &c->classes[ci];
1916+ for (int iv = 0; iv < cl->nivars; iv++) {
1917+ if (cl->ivar_types[iv] != TY_POLY_ARRAY) continue;
1918+ const char *ivn = cl->ivars[iv];
1919+ int saw = 0, ok = 1;
1920+ for (int id = 0; id < c->nt->count && ok; id++) {
1921+ const char *ty = nt_type(c->nt, id);
1922+ if (!ty) continue;
1923+ if (!strcmp(ty, "CallNode")) {
1924+ int recv = nt_ref(c->nt, id, "receiver");
1925+ if (recv < 0) continue;
1926+ const char *rty = nt_type(c->nt, recv);
1927+ if (!rty || strcmp(rty, "InstanceVariableReadNode")) continue;
1928+ const char *rivn = nt_str(c->nt, recv, "name");
1929+ if (!rivn || strcmp(rivn, ivn)) continue;
1930+ Scope *sc = comp_scope_of(c, id);
1931+ if (!sc || sc->class_id != ci) { ok = 0; break; }
1932+ const char *nm = nt_str(c->nt, id, "name");
1933+ int args = nt_ref(c->nt, id, "arguments"); int an = 0;
1934+ const int *argv = args >= 0 ? nt_arr(c->nt, args, "arguments", &an) : NULL;
1935+ if (nm && (!strcmp(nm, "<<") || !strcmp(nm, "push") || !strcmp(nm, "append"))) {
1936+ for (int a = 0; a < an; a++) { saw = 1; if (infer_type(c, argv[a]) != TY_INT) { ok = 0; break; } }
1937+ }
1938+ else if (nm && !strcmp(nm, "[]=") && an == 2) {
1939+ saw = 1; if (infer_type(c, argv[1]) != TY_INT) ok = 0;
1940+ }
1941+ }
1942+ else if (!strcmp(ty, "InstanceVariableWriteNode")) {
1943+ const char *wivn = nt_str(c->nt, id, "name");
1944+ if (!wivn || strcmp(wivn, ivn)) continue;
1945+ Scope *sc = comp_scope_of(c, id);
1946+ if (!sc || sc->class_id != ci) { ok = 0; break; }
1947+ int v = nt_ref(c->nt, id, "value");
1948+ TyKind vt = v >= 0 ? infer_type(c, v) : TY_UNKNOWN;
1949+ if (vt == TY_INT_ARRAY) { /* int array source */ }
1950+ else if (v >= 0 && nt_type(c->nt, v) && !strcmp(nt_type(c->nt, v), "ArrayNode")) {
1951+ int en = 0; const int *el = nt_arr(c->nt, v, "elements", &en);
1952+ for (int e = 0; e < en; e++) if (infer_type(c, el[e]) != TY_INT) { ok = 0; break; }
1953+ }
1954+ else { ok = 0; }
1955+ }
1956+ }
1957+ if (saw && ok) {
1958+ sp_ivwatch(ivn && ivn[0] == '@' ? ivn + 1 : ivn, "renarrow_int_array", TY_POLY_ARRAY, TY_INT_ARRAY);
1959+ cl->ivar_types[iv] = TY_INT_ARRAY;
1960+ }
1961+ }
1962+ }
1963+
19051964 /* A read-only ivar (referenced but never assigned a typed value) stays
19061965 TY_UNKNOWN -> it has no C type. Such a slot always reads nil at runtime;
19071966 give it a boxed-nil poly field so `.nil?`/`.inspect` behave (#712). */