Commit cc697a74

cc697a74142aMerged

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

Description

Add mutable-string type for <<-appended string locals

A local string built up via s << x in a loop was emitted as s = sp_str_concat(s, x) — an O(n) copy per append, so the loop was O(n^2). Legacy types such locals as a mutable sp_String * and appends in place (amortized O(1)).

Introduce TY_STRBUF, a storage-only refinement of TY_STRING: a local whose every write is a bare string literal and which is appended via << (and is not a parameter, not reassign-mutated via replace/prepend/insert/ clear/bang) is stored as an sp_String*. << lowers to sp_String_append; reads hand out an independent GC string copy (the sp_String buffer is not itself a GC object, so a bare cstr would dangle past the wrapper), so a STRBUF never escapes. comp_ntype demotes it to TY_STRING, leaving all type-directed logic unchanged.

bm_huffman 1314x -> 0.99x and bm_str_concat 117x -> 0.99x vs the legacy backend. make test 845, make bench 57, optcarrot checksum 59662 (fps unchanged), legacy self-host fixpoint all green.

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

Details

Commit
cc697a74142a
Parent
9652fc7e9a96

src/analyze.cmodifiedView file

@@ -1415,5 +1415,64 @@
14151415 for (int k2 = 0; k2 < bn2; k2++) infer_type(c, bb2[k2]);
14161416 an_ie_class_id = saved2;
14171417 }
1418+
1419+ /* Promote `<<`-appended string locals to mutable strings (TY_STRBUF) so the
1420+ append is amortized O(1) instead of an O(n) copy-concat (which makes a
1421+ build-in-a-loop O(n^2)). This is a storage-only refinement: comp_ntype
1422+ demotes it to TY_STRING, and every read hands out an immutable copy, so a
1423+ STRBUF never escapes its sp_String wrapper. Parameters are excluded (their
1424+ type is part of the function signature). Runs last, after the node-type
1425+ cache is finalized, so reads keep their TY_STRING cache entry. */
1426+ for (int id = 0; id < c->nt->count; id++) {
1427+ const char *ty = nt_type(c->nt, id);
1428+ if (!ty || strcmp(ty, "CallNode")) continue;
1429+ const char *nm = nt_str(c->nt, id, "name");
1430+ if (!nm || strcmp(nm, "<<")) continue;
1431+ int recv = nt_ref(c->nt, id, "receiver");
1432+ if (recv < 0 || !nt_type(c->nt, recv) ||
1433+ strcmp(nt_type(c->nt, recv), "LocalVariableReadNode")) continue;
1434+ if (c->ntype[recv] != TY_STRING) continue; /* string append only */
1435+ const char *vn = nt_str(c->nt, recv, "name");
1436+ Scope *s = vn ? comp_scope_of(c, recv) : NULL;
1437+ LocalVar *lv = s ? scope_local(s, vn) : NULL;
1438+ if (!lv || lv->is_param || lv->type != TY_STRING) continue;
1439+ /* Only promote when every write to this local is a bare string literal:
1440+ such a value is a fresh mutable string. A `.freeze`/`.dup`/method-result
1441+ write may carry runtime frozen state that sp_String_new would discard,
1442+ so `<<` would fail to raise FrozenError. Conservative but sound. */
1443+ int all_literal_writes = 1, saw_write = 0;
1444+ for (int w = 0; w < c->nt->count; w++) {
1445+ const char *wty = nt_type(c->nt, w);
1446+ if (!wty || strcmp(wty, "LocalVariableWriteNode")) continue;
1447+ const char *wn = nt_str(c->nt, w, "name");
1448+ if (!wn || strcmp(wn, vn) || comp_scope_of(c, w) != s) continue;
1449+ saw_write = 1;
1450+ int wv = nt_ref(c->nt, w, "value");
1451+ const char *wvty = wv >= 0 ? nt_type(c->nt, wv) : NULL;
1452+ if (!wvty || strcmp(wvty, "StringNode")) { all_literal_writes = 0; break; }
1453+ }
1454+ if (!saw_write || !all_literal_writes) continue;
1455+ /* Exclude vars used with a reassigning string mutator (replace/prepend/
1456+ insert/clear or a bang method): codegen emits those as `recv = ...`,
1457+ which needs a plain lvalue, not the copy a STRBUF read produces. */
1458+ int has_reassign_mutate = 0;
1459+ for (int u = 0; u < c->nt->count && !has_reassign_mutate; u++) {
1460+ const char *uty = nt_type(c->nt, u);
1461+ if (!uty || strcmp(uty, "CallNode")) continue;
1462+ int urecv = nt_ref(c->nt, u, "receiver");
1463+ if (urecv < 0 || !nt_type(c->nt, urecv) ||
1464+ strcmp(nt_type(c->nt, urecv), "LocalVariableReadNode")) continue;
1465+ const char *urn = nt_str(c->nt, urecv, "name");
1466+ if (!urn || strcmp(urn, vn) || comp_scope_of(c, urecv) != s) continue;
1467+ const char *un = nt_str(c->nt, u, "name");
1468+ if (!un) continue;
1469+ size_t ul = strlen(un);
1470+ if (!strcmp(un, "replace") || !strcmp(un, "prepend") || !strcmp(un, "insert") ||
1471+ !strcmp(un, "clear") || (ul > 0 && un[ul - 1] == '!'))
1472+ has_reassign_mutate = 1;
1473+ }
1474+ if (has_reassign_mutate) continue;
1475+ lv->type = TY_STRBUF;
1476+ }
14181477 }
14191478