Commit d0d65581

d0d65581a374Merged

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

Description

gc: keep frozen heap strings immortal instead of marking them in the hot path

The #1449 fix marked a reachable frozen string 0xf1 -> 0xf0 inside sp_mark_string, but that inline is on optcarrot's GC mark path and is layout-sensitive: the extra branch cost ~19 fps (same generated optcarrot C, header-only A/B). Restore sp_mark_string to its original 0xfe -> 0xfc form and instead keep every frozen heap string (0xf1) across sp_str_sweep -- a live frozen global still survives (the #1449 UAF stays fixed, ASAN clean), and frozen literal constants are immortal anyway. The cost is that a rare dynamically-frozen-then-dropped string is no longer reclaimed, which is a good trade for the hot-path recovery.

make test 999 / make bench 57 / optcarrot checksum 59662, fps back to the pre-promote ~700 level.

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

Details

Commit
d0d65581a374
Parent
7c6cb277a731

lib/sp_gc.hmodifiedView file

@@ -78,12 +78,12 @@
7878 * String tag bytes: 0xfe heap-unmarked -> 0xfc marked; others skipped. */
7979 static inline void sp_mark_string(const char *s) {
8080 if (!s) return;
81- unsigned char m = (unsigned char)s[-1];
82- if (m == 0xfe) ((char *)s)[-1] = (char)0xfc;
83- /* A heap string frozen by .freeze (0xf1) lives on the sweepable string heap
84- too; give it a reachable-this-cycle state (0xf0) so sp_str_sweep keeps it
85- while rooted instead of freeing a live frozen global (#1449). */
86- else if (m == 0xf1) ((char *)s)[-1] = (char)0xf0;
81+ if ((unsigned char)s[-1] == 0xfe) {
82+ ((char *)s)[-1] = (char)0xfc;
83+ }
84+ /* No frozen (0xf1) branch here: this is inlined into optcarrot's GC mark and
85+ is layout-sensitive. A live frozen heap string is kept immortal by
86+ sp_str_sweep instead (#1449). */
8787 }
8888 static inline void sp_mark_rbval(sp_RbVal v) {
8989 if (v.tag == SP_TAG_STR) sp_mark_string(v.v.s);