Commit b21ad532

b21ad532afafMerged

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

Description

Value-type objects: allow string ivars (fixes object_new_init)

Extend value-type eligibility to classes with string instance variables, not just int/float/bool. A value-type local lives on the stack, so each of its string (heap-pointer) fields is GC-rooted at the local's declaration -- the field slot is a stable root, so the referent survives collection without per-assignment rooting.

This lets object_new_init's class C (four int ivars plus @e = "c") be a value type; with by-value return (Stage 4) its test returns the struct by value and the per-iteration heap allocation disappears.

bm_object_new_init 15x -> 1x vs legacy (the last slow object bench). make test 845, make bench 57, optcarrot checksum 59662.

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

Details

Commit
b21ad532afaf
Parent
93bd2c0718fe

src/analyze.cmodifiedView file

@@ -1490,7 +1490,9 @@
14901490 int scalar = 1;
14911491 for (int j = 0; j < ci->nivars; j++) {
14921492 TyKind t = ci->ivar_types[j];
1493- if (t != TY_INT && t != TY_FLOAT && t != TY_BOOL) { scalar = 0; break; }
1493+ /* int/float/bool need no GC; string fields are heap pointers but get
1494+ GC-rooted per value-type local (the field slot is a stable root). */
1495+ if (t != TY_INT && t != TY_FLOAT && t != TY_BOOL && t != TY_STRING) { scalar = 0; break; }
14941496 }
14951497 if (!scalar) continue;
14961498 int has_sub = 0;