Commit 15a8e52f

15a8e52f90c8Merged

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

Description

Value-type objects (Stage 1): stack-allocate non-escaping immutable classes

A small, immutable, scalar-ivar leaf class whose instances never need a heap pointer (never boxed, stored, passed, or captured) is detected as a value type (ClassInfo.is_value_type). X.new for such a class is constructed on the stack (a block-scoped temp initialized in place) and the instance pointer is left unrooted, eliminating the per-object heap allocation and GC pressure.

The object representation stays a pointer (sp_X *), so method dispatch, ivar access, and method bodies are unchanged -- only the new site and the local's GC rooting differ. Detection mirrors legacy's detect_value_types (see reference_legacy_value_type_logic) with the conservative exclusions; Stage 1 further restricts to no-arg constructors so every construction is uniformly stack-allocated (consistent rooting).

bm_object_new 15x -> 1x vs the legacy backend. make test 845, make bench 57, optcarrot checksum 59662 (no value types, unchanged), legacy self-host fixpoint all green. (object_new_init / _no_escape need arg-carrying constructors -- a later stage.)

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
15a8e52f90c8
Parent
5253b8031cf9

src/analyze.cmodifiedView file

@@ -1474,5 +1474,90 @@
14741474 if (has_reassign_mutate) continue;
14751475 lv->type = TY_STRBUF;
14761476 }
1477+
1478+ /* Value-type object detection (Stage 1, conservative). A user class is
1479+ represented by value (sp_X, no heap/GC) when it is a small, immutable,
1480+ scalar-only leaf whose instances never need a heap pointer (never boxed,
1481+ stored, passed, or captured). See reference_legacy_value_type_logic. */
1482+ for (int i = 0; i < c->nclasses; i++) {
1483+ ClassInfo *ci = &c->classes[i];
1484+ if (ci->is_struct) continue;
1485+ if (ci->def_node < 0) continue; /* synthetic / Toplevel */
1486+ if (!ci->name || !strcmp(ci->name, "Toplevel")) continue;
1487+ if (ci->nivars < 1 || ci->nivars > 8) continue;
1488+ if (ci->nwriters > 0) continue;
1489+ if (ci->parent >= 0) continue; /* explicit superclass */
1490+ int scalar = 1;
1491+ for (int j = 0; j < ci->nivars; j++) {
1492+ TyKind t = ci->ivar_types[j];
1493+ if (t != TY_INT && t != TY_FLOAT && t != TY_BOOL) { scalar = 0; break; }
1494+ }
1495+ if (!scalar) continue;
1496+ int has_sub = 0;
1497+ for (int j = 0; j < c->nclasses; j++)
1498+ if (c->classes[j].parent == i) { has_sub = 1; break; }
1499+ if (has_sub) continue;
1500+ /* Stage 1: only no-arg constructors. Then every `X.new` is 0-arg and can be
1501+ stack-allocated uniformly, so the (unrooted) instance pointer is always a
1502+ stack address — never a mix of stack and heap that would break GC rooting. */
1503+ int initm = comp_method_in_chain(c, i, "initialize", NULL);
1504+ if (initm >= 0 && c->scopes[initm].nparams > 0) continue;
1505+ ci->is_value_type = 1; /* tentative; disqualified below */
1506+ }
1507+ for (int id = 0; id < c->nt->count; id++) {
1508+ const char *ty = nt_type(c->nt, id);
1509+ if (!ty) continue;
1510+ /* immutable check: an ivar write outside `initialize` defeats value type */
1511+ if (!strcmp(ty, "InstanceVariableWriteNode") ||
1512+ !strcmp(ty, "InstanceVariableOperatorWriteNode") ||
1513+ !strcmp(ty, "InstanceVariableTargetNode")) {
1514+ Scope *s = comp_scope_of(c, id);
1515+ if (s && s->class_id >= 0 && s->class_id < c->nclasses &&
1516+ c->classes[s->class_id].is_value_type &&
1517+ (!s->name || strcmp(s->name, "initialize")))
1518+ c->classes[s->class_id].is_value_type = 0;
1519+ }
1520+ /* unsafe uses that would need a heap pointer / boxing (void* slot) */
1521+ if (!strcmp(ty, "ArrayNode") || !strcmp(ty, "HashNode") ||
1522+ !strcmp(ty, "KeywordHashNode")) {
1523+ int n = 0; const int *els = nt_arr(c->nt, id, "elements", &n);
1524+ for (int k = 0; k < n; k++) {
1525+ TyKind et = comp_ntype(c, els[k]);
1526+ if (ty_is_object(et)) { int q = ty_object_class(et); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; }
1527+ }
1528+ }
1529+ if (!strcmp(ty, "CallNode")) {
1530+ const char *nm = nt_str(c->nt, id, "name");
1531+ int recv = nt_ref(c->nt, id, "receiver");
1532+ if (nm && !strcmp(nm, "method") && recv >= 0) {
1533+ TyKind rt = comp_ntype(c, recv);
1534+ if (ty_is_object(rt)) { int q = ty_object_class(rt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; }
1535+ }
1536+ int args = nt_ref(c->nt, id, "arguments"); int an = 0;
1537+ const int *av = args >= 0 ? nt_arr(c->nt, args, "arguments", &an) : NULL;
1538+ for (int k = 0; k < an; k++) {
1539+ TyKind at = comp_ntype(c, av[k]);
1540+ if (ty_is_object(at)) { int q = ty_object_class(at); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; }
1541+ }
1542+ }
1543+ if (!strcmp(ty, "InstanceVariableWriteNode") || !strcmp(ty, "GlobalVariableWriteNode") ||
1544+ !strcmp(ty, "ConstantWriteNode")) {
1545+ int v = nt_ref(c->nt, id, "value");
1546+ if (v >= 0) { TyKind vt = comp_ntype(c, v); if (ty_is_object(vt)) { int q = ty_object_class(vt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; } }
1547+ }
1548+ /* a value-type instance written into a poly-typed local would be boxed */
1549+ if (!strcmp(ty, "LocalVariableWriteNode")) {
1550+ int v = nt_ref(c->nt, id, "value");
1551+ if (v >= 0) {
1552+ TyKind vt = comp_ntype(c, v);
1553+ if (ty_is_object(vt)) {
1554+ const char *vn = nt_str(c->nt, id, "name");
1555+ Scope *s = comp_scope_of(c, id);
1556+ LocalVar *lv = (vn && s) ? scope_local(s, vn) : NULL;
1557+ if (lv && lv->type != vt) { int q = ty_object_class(vt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; }
1558+ }
1559+ }
1560+ }
1561+ }
14771562 }
14781563