Commit 7b996f31

7b996f317369Merged

Ryan Seys <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

refactor(codegen): one source of truth for iterator collection shape

The map/collect, select/filter, and reject classifications were re-derived as ad-hoc strcmp pairs at ~10 sites across the fold-time iterator emitters. Hoist them into ty_iter_shape (next to ty_block_yield), so the codegen collection-shape vocabulary lives in one place. Pure extraction -- identical boolean results, byte-identical output, full suite green.

(Scope note: this is the safe extent of "share the iterator vocabulary". The broader idea of driving emit_iteration_stmt's whole name dispatch off a single descriptor is declined: that dispatch keys on loop SHAPE per receiver type -- a different axis than ty_block_yield's block-param types -- and rewriting the hottest codegen path table-first would be byte-sensitive with no behavior gain.)

Details

Commit
7b996f317369
Parent
81833b44c7eb

src/codegen_fold.cmodifiedView file

@@ -38,9 +38,10 @@
3838 int block = nt_ref(nt, id, "block");
3939 const char *name = nt_str(nt, id, "name");
4040 int recv = nt_ref(nt, id, "receiver");
41- int is_sel = !strcmp(name, "select") || !strcmp(name, "filter");
42- int is_rej = !strcmp(name, "reject");
43- int is_map = !strcmp(name, "map") || !strcmp(name, "collect");
41+ TyIterShape shp = ty_iter_shape(name);
42+ int is_sel = shp == TY_ITER_SELECT;
43+ int is_rej = shp == TY_ITER_REJECT;
44+ int is_map = shp == TY_ITER_MAP;
4445 if (!is_map && !is_sel && !is_rej) return 0;
4546 TyKind rt = comp_ntype(c, recv);
4647 const char *hn = ty_hash_cname(rt);
@@ -1521,7 +1522,7 @@
15211522 if (!name || recv < 0) return 0;
15221523 TyKind rt = comp_ntype(c, recv);
15231524 /* array.each_slice(n).map { |x, y, ...| } chain: unroll into a direct slice loop */
1524- if ((!strcmp(name, "map") || !strcmp(name, "collect")) &&
1525+ if (ty_iter_shape(name) == TY_ITER_MAP &&
15251526 nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&
15261527 nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "each_slice") &&
15271528 nt_ref(nt, recv, "block") < 0) {
@@ -1592,7 +1593,7 @@
15921593 }
15931594 }
15941595 /* array.each_cons(n).map { |pair| } or { |a,b| } or { |(a,b)| } chain */
1595- if ((!strcmp(name, "map") || !strcmp(name, "collect")) &&
1596+ if (ty_iter_shape(name) == TY_ITER_MAP &&
15961597 nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&
15971598 nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "each_cons") &&
15981599 nt_ref(nt, recv, "block") < 0) {
@@ -1679,7 +1680,7 @@
16791680 }
16801681
16811682 /* array.each_cons(n).with_index(off).map { |pair, i| } or { |(a,b), i| } chain */
1682- if ((!strcmp(name, "map") || !strcmp(name, "collect")) &&
1683+ if (ty_iter_shape(name) == TY_ITER_MAP &&
16831684 nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&
16841685 nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "with_index") &&
16851686 nt_ref(nt, recv, "block") < 0) {
@@ -1790,7 +1791,7 @@
17901791 if (rt == TY_POLY) {
17911792 /* poly-typed receiver (e.g. `arr = nil` default): iterate via
17921793 sp_poly_arr_len / sp_poly_arr_get and build a typed result array */
1793- int is_map2 = !strcmp(name, "map") || !strcmp(name, "collect");
1794+ int is_map2 = ty_iter_shape(name) == TY_ITER_MAP;
17941795 if (!is_map2) return 0;
17951796 TyKind restype2 = comp_ntype(c, id);
17961797 int res_poly2 = (restype2 == TY_POLY_ARRAY);
@@ -1857,9 +1858,10 @@
18571858 const char *k = range_recv ? "Int" : (rt == TY_POLY_ARRAY ? "Poly" : array_kind(rt));
18581859 if (!k) return 0;
18591860
1860- int is_map = !strcmp(name, "map") || !strcmp(name, "collect");
1861- int is_sel = !strcmp(name, "select") || !strcmp(name, "filter");
1862- int is_rej = !strcmp(name, "reject");
1861+ TyIterShape shp = ty_iter_shape(name);
1862+ int is_map = shp == TY_ITER_MAP;
1863+ int is_sel = shp == TY_ITER_SELECT;
1864+ int is_rej = shp == TY_ITER_REJECT;
18631865 if (!is_map && !is_sel && !is_rej) return 0;
18641866
18651867 TyKind restype = comp_ntype(c, id);