Commit 4cd394c8

4cd394c88b84Merged

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

Description

refactor: conditional emission of string helper functions

Use SRC_HAS() macro to scan source text for method names and only emit the corresponding C helper functions when actually referenced.

Each string helper (upcase, downcase, strip, chomp, gsub, sub, tr, capitalize, reverse, ljust, rjust, center, etc.) is now individually guarded. sp_str_concat/sp_int_to_s are emitted when string literals or + operator appear. backtick/format helpers only when ` or printf are used.

Results for puts "hello": Before: 209 lines After: 61 lines (71% further reduction, 90% total from 585)

60/60 tests, 39/39 benchmarks pass.

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

Details

Commit
4cd394c88b84
Parent
0845da762132

src/emit.cmodifiedView file

@@ -1048,26 +1048,49 @@
10481048 emit_raw(ctx, " return r;\n}\n\n");
10491049 }
10501050
1051- /* ---- String helpers ---- */
1051+ /* ---- String helpers (only emit those actually referenced) ---- */
1052+ /* Build a combined source string for method name detection */
1053+ const char *_src = (const char *)ctx->parser->start;
1054+ size_t _slen = ctx->parser->end - ctx->parser->start;
1055+ /* Also check required file sources */
1056+ char *_all_src = (char *)malloc(_slen + 1);
1057+ memcpy(_all_src, _src, _slen); _all_src[_slen] = '\0';
1058+ for (int _ri = 0; _ri < ctx->required_file_count; _ri++) {
1059+ if (!ctx->required_files[_ri].source) continue;
1060+ size_t rl = strlen(ctx->required_files[_ri].source);
1061+ _all_src = (char *)realloc(_all_src, _slen + rl + 2);
1062+ _all_src[_slen] = '\n'; memcpy(_all_src + _slen + 1, ctx->required_files[_ri].source, rl + 1);
1063+ _slen += rl + 1;
1064+ }
1065+ #define SRC_HAS(s) (memmem(_all_src, _slen, s, sizeof(s)-1) != NULL)
1066+ if (SRC_HAS("upcase") || ctx->needs_sp_string) {
10521067 emit_raw(ctx, "static const char *sp_str_upcase(const char *s) {\n");
10531068 emit_raw(ctx, " size_t n = strlen(s); char *r = (char *)malloc(n + 1);\n");
10541069 emit_raw(ctx, " for (size_t i = 0; i <= n; i++) r[i] = toupper((unsigned char)s[i]);\n");
10551070 emit_raw(ctx, " return r;\n}\n");
1071+ }
1072+ if (SRC_HAS("downcase") || ctx->needs_sp_string) {
10561073 emit_raw(ctx, "static const char *sp_str_downcase(const char *s) {\n");
10571074 emit_raw(ctx, " size_t n = strlen(s); char *r = (char *)malloc(n + 1);\n");
10581075 emit_raw(ctx, " for (size_t i = 0; i <= n; i++) r[i] = tolower((unsigned char)s[i]);\n");
10591076 emit_raw(ctx, " return r;\n}\n");
1077+ }
1078+ /* sp_str_concat and sp_int_to_s: needed for string interpolation and + operator */
1079+ if (SRC_HAS("\"") || SRC_HAS("+") || SRC_HAS("to_s")) {
10601080 emit_raw(ctx, "static const char *sp_str_concat(const char *a, const char *b) {\n");
10611081 emit_raw(ctx, " size_t la = strlen(a), lb = strlen(b);\n");
10621082 emit_raw(ctx, " char *r = (char *)malloc(la + lb + 1);\n");
10631083 emit_raw(ctx, " memcpy(r, a, la); memcpy(r + la, b, lb + 1); return r;\n}\n");
10641084 emit_raw(ctx, "static const char *sp_int_to_s(mrb_int n) {\n");
10651085 emit_raw(ctx, " char *r = (char *)malloc(24); snprintf(r, 24, \"%%lld\", (long long)n); return r;\n}\n");
1086+ }
1087+ if (SRC_HAS("[")) {
10661088 emit_raw(ctx, "static const char *sp_str_char_at(const char *s, mrb_int idx) {\n");
10671089 emit_raw(ctx, " mrb_int len = (mrb_int)strlen(s);\n");
10681090 emit_raw(ctx, " if (idx < 0) idx += len;\n");
10691091 emit_raw(ctx, " if (idx < 0 || idx >= len) return \"\";\n");
10701092 emit_raw(ctx, " char *r = (char *)malloc(2); r[0] = s[idx]; r[1] = '\\0'; return r;\n}\n\n");
1093+ }
10711094
10721095 /* ---- File I/O helpers (only when needed) ---- */
10731096 if (ctx->needs_file_io) {
@@ -1217,43 +1240,51 @@
12171240 emit_raw(ctx, "static int64_t sp_StringIO_fileno(sp_StringIO *s) { (void)s; return -1; }\n\n");
12181241 } /* needs_stringio */
12191242
1220- /* ---- Additional string helpers ---- */
1243+ /* ---- Additional string helpers (conditional) ---- */
1244+ if (SRC_HAS("strip")) {
12211245 emit_raw(ctx, "static const char *sp_str_strip(const char *s) {\n");
12221246 emit_raw(ctx, " while (*s && isspace((unsigned char)*s)) s++;\n");
12231247 emit_raw(ctx, " size_t n = strlen(s);\n");
12241248 emit_raw(ctx, " while (n > 0 && isspace((unsigned char)s[n-1])) n--;\n");
12251249 emit_raw(ctx, " char *r = (char *)malloc(n + 1);\n");
12261250 emit_raw(ctx, " memcpy(r, s, n); r[n] = '\\0'; return r;\n}\n");
1227-
1251+ }
1252+ if (SRC_HAS("chomp")) {
12281253 emit_raw(ctx, "static const char *sp_str_chomp(const char *s) {\n");
12291254 emit_raw(ctx, " size_t n = strlen(s);\n");
12301255 emit_raw(ctx, " if (n > 0 && s[n-1] == '\\n') { if (n > 1 && s[n-2] == '\\r') n--; n--; }\n");
12311256 emit_raw(ctx, " char *r = (char *)malloc(n + 1);\n");
12321257 emit_raw(ctx, " memcpy(r, s, n); r[n] = '\\0'; return r;\n}\n");
1233-
1258+ }
1259+ if (SRC_HAS("capitalize")) {
12341260 emit_raw(ctx, "static const char *sp_str_capitalize(const char *s) {\n");
12351261 emit_raw(ctx, " size_t n = strlen(s); char *r = (char *)malloc(n + 1);\n");
12361262 emit_raw(ctx, " for (size_t i = 0; i <= n; i++) r[i] = (i == 0) ? toupper((unsigned char)s[i]) : tolower((unsigned char)s[i]);\n");
12371263 emit_raw(ctx, " return r;\n}\n");
1238-
1264+ }
1265+ if (SRC_HAS("reverse") || ctx->needs_sp_string) {
12391266 emit_raw(ctx, "static const char *sp_str_reverse(const char *s) {\n");
12401267 emit_raw(ctx, " size_t n = strlen(s); char *r = (char *)malloc(n + 1);\n");
12411268 emit_raw(ctx, " for (size_t i = 0; i < n; i++) r[i] = s[n - 1 - i];\n");
12421269 emit_raw(ctx, " r[n] = '\\0'; return r;\n}\n");
1243-
1270+ }
1271+ if (SRC_HAS("count")) {
12441272 emit_raw(ctx, "static mrb_int sp_str_count(const char *s, const char *chars) {\n");
12451273 emit_raw(ctx, " mrb_int c = 0;\n");
12461274 emit_raw(ctx, " for (; *s; s++) { for (const char *p = chars; *p; p++) { if (*s == *p) { c++; break; } } }\n");
12471275 emit_raw(ctx, " return c;\n}\n");
1248-
1276+ }
1277+ if (SRC_HAS("start_with")) {
12491278 emit_raw(ctx, "static mrb_bool sp_str_starts_with(const char *s, const char *prefix) {\n");
12501279 emit_raw(ctx, " size_t pn = strlen(prefix);\n");
12511280 emit_raw(ctx, " return strncmp(s, prefix, pn) == 0;\n}\n");
1252-
1281+ }
1282+ if (SRC_HAS("end_with")) {
12531283 emit_raw(ctx, "static mrb_bool sp_str_ends_with(const char *s, const char *suffix) {\n");
12541284 emit_raw(ctx, " size_t sn = strlen(s), xn = strlen(suffix);\n");
12551285 emit_raw(ctx, " return sn >= xn && strcmp(s + sn - xn, suffix) == 0;\n}\n");
1256-
1286+ }
1287+ if (SRC_HAS("gsub")) {
12571288 emit_raw(ctx, "static const char *sp_str_gsub(const char *s, const char *from, const char *to) {\n");
12581289 emit_raw(ctx, " size_t fl = strlen(from), tl = strlen(to);\n");
12591290 emit_raw(ctx, " size_t cap = strlen(s) * 2 + 16; char *r = (char *)malloc(cap); size_t ri = 0;\n");
@@ -1267,7 +1298,8 @@
12671298 emit_raw(ctx, " }\n");
12681299 emit_raw(ctx, " }\n");
12691300 emit_raw(ctx, " r[ri] = '\\0'; return r;\n}\n");
1270-
1301+ }
1302+ if (SRC_HAS("sub")) {
12711303 emit_raw(ctx, "static const char *sp_str_sub(const char *s, const char *from, const char *to) {\n");
12721304 emit_raw(ctx, " const char *p = strstr(s, from);\n");
12731305 emit_raw(ctx, " if (!p) { size_t n = strlen(s); char *r = (char *)malloc(n+1); memcpy(r,s,n+1); return r; }\n");
@@ -1276,34 +1308,41 @@
12761308 emit_raw(ctx, " size_t before = p - s;\n");
12771309 emit_raw(ctx, " memcpy(r, s, before); memcpy(r + before, to, tl);\n");
12781310 emit_raw(ctx, " memcpy(r + before + tl, p + fl, sn - before - fl + 1); return r;\n}\n");
1279-
1311+ }
1312+ if (SRC_HAS("*")) {
12801313 emit_raw(ctx, "static const char *sp_str_repeat(const char *s, mrb_int n) {\n");
12811314 emit_raw(ctx, " size_t sl = strlen(s); char *r = (char *)malloc(sl * n + 1);\n");
12821315 emit_raw(ctx, " for (mrb_int i = 0; i < n; i++) memcpy(r + sl * i, s, sl);\n");
12831316 emit_raw(ctx, " r[sl * n] = '\\0'; return r;\n}\n\n");
1284-
1285- /* ljust / rjust / center */
1317+ }
1318+ if (SRC_HAS("ljust")) {
12861319 emit_raw(ctx, "static const char *sp_str_ljust(const char *s, mrb_int w, char pad) {\n");
12871320 emit_raw(ctx, " size_t n = strlen(s); if ((mrb_int)n >= w) { char *r = (char *)malloc(n+1); memcpy(r,s,n+1); return r; }\n");
12881321 emit_raw(ctx, " char *r = (char *)malloc(w+1); memcpy(r,s,n); memset(r+n,pad,w-n); r[w]='\\0'; return r;\n}\n");
1322+ }
1323+ if (SRC_HAS("rjust")) {
12891324 emit_raw(ctx, "static const char *sp_str_rjust(const char *s, mrb_int w, char pad) {\n");
12901325 emit_raw(ctx, " size_t n = strlen(s); if ((mrb_int)n >= w) { char *r = (char *)malloc(n+1); memcpy(r,s,n+1); return r; }\n");
12911326 emit_raw(ctx, " char *r = (char *)malloc(w+1); memset(r,pad,w-n); memcpy(r+w-n,s,n+1); return r;\n}\n");
1327+ }
1328+ if (SRC_HAS("center")) {
12921329 emit_raw(ctx, "static const char *sp_str_center(const char *s, mrb_int w, char pad) {\n");
12931330 emit_raw(ctx, " size_t n = strlen(s); if ((mrb_int)n >= w) { char *r = (char *)malloc(n+1); memcpy(r,s,n+1); return r; }\n");
12941331 emit_raw(ctx, " mrb_int left = (w - (mrb_int)n) / 2; mrb_int right = w - (mrb_int)n - left;\n");
12951332 emit_raw(ctx, " char *r = (char *)malloc(w+1); memset(r,pad,w); memcpy(r+left,s,n); r[w]='\\0'; return r;\n}\n");
1296-
1297- /* lstrip / rstrip */
1333+ }
1334+ if (SRC_HAS("lstrip")) {
12981335 emit_raw(ctx, "static const char *sp_str_lstrip(const char *s) {\n");
12991336 emit_raw(ctx, " while (*s && isspace((unsigned char)*s)) s++;\n");
13001337 emit_raw(ctx, " size_t n = strlen(s); char *r = (char *)malloc(n+1); memcpy(r,s,n+1); return r;\n}\n");
1338+ }
1339+ if (SRC_HAS("rstrip")) {
13011340 emit_raw(ctx, "static const char *sp_str_rstrip(const char *s) {\n");
13021341 emit_raw(ctx, " size_t n = strlen(s);\n");
13031342 emit_raw(ctx, " while (n > 0 && isspace((unsigned char)s[n-1])) n--;\n");
13041343 emit_raw(ctx, " char *r = (char *)malloc(n+1); memcpy(r,s,n); r[n]='\\0'; return r;\n}\n");
1305-
1306- /* tr / delete / squeeze */
1344+ }
1345+ if (SRC_HAS(".tr")) {
13071346 emit_raw(ctx, "static const char *sp_str_tr(const char *s, const char *from, const char *to) {\n");
13081347 emit_raw(ctx, " size_t n = strlen(s); char *r = (char *)malloc(n+1);\n");
13091348 emit_raw(ctx, " size_t fl = strlen(from), tl = strlen(to);\n");
@@ -1312,16 +1351,20 @@
13121351 emit_raw(ctx, " if (p && s[i]) { size_t idx = p - from; r[i] = (idx < tl) ? to[idx] : to[tl-1]; }\n");
13131352 emit_raw(ctx, " else r[i] = s[i];\n");
13141353 emit_raw(ctx, " } return r;\n}\n");
1354+ }
1355+ if (SRC_HAS("delete")) {
13151356 emit_raw(ctx, "static const char *sp_str_delete(const char *s, const char *chars) {\n");
13161357 emit_raw(ctx, " size_t n = strlen(s); char *r = (char *)malloc(n+1); size_t ri = 0;\n");
13171358 emit_raw(ctx, " for (size_t i = 0; i < n; i++) { if (!memchr(chars, s[i], strlen(chars))) r[ri++] = s[i]; }\n");
13181359 emit_raw(ctx, " r[ri] = '\\0'; return r;\n}\n");
1360+ }
1361+ if (SRC_HAS("squeeze")) {
13191362 emit_raw(ctx, "static const char *sp_str_squeeze(const char *s) {\n");
13201363 emit_raw(ctx, " size_t n = strlen(s); char *r = (char *)malloc(n+1); size_t ri = 0;\n");
13211364 emit_raw(ctx, " for (size_t i = 0; i < n; i++) { if (i == 0 || s[i] != s[i-1]) r[ri++] = s[i]; }\n");
13221365 emit_raw(ctx, " r[ri] = '\\0'; return r;\n}\n");
1323-
1324- /* slice / [range] */
1366+ }
1367+ if (SRC_HAS("slice")) {
13251368 emit_raw(ctx, "static const char *sp_str_slice(const char *s, mrb_int start, mrb_int len) {\n");
13261369 emit_raw(ctx, " mrb_int sn = (mrb_int)strlen(s);\n");
13271370 emit_raw(ctx, " if (start < 0) start += sn;\n");
@@ -1329,14 +1372,10 @@
13291372 emit_raw(ctx, " if (start >= sn || len <= 0) { char *r = (char *)malloc(1); r[0]='\\0'; return r; }\n");
13301373 emit_raw(ctx, " if (start + len > sn) len = sn - start;\n");
13311374 emit_raw(ctx, " char *r = (char *)malloc(len+1); memcpy(r, s+start, len); r[len]='\\0'; return r;\n}\n");
1332-
1333- /* to_f */
1375+ }
1376+ if (SRC_HAS("to_f")) {
13341377 emit_raw(ctx, "static mrb_float sp_str_to_f(const char *s) { return strtod(s, NULL); }\n");
1335-
1336- /* bytes/chars helpers emitted after array types (see below) */
1337-
1338-
1339- /* sp_str_char_at already emitted above */
1378+ }
13401379
13411380 /* ---- Mutable string (sp_String) ---- */
13421381 if (ctx->needs_sp_string) {
@@ -1382,27 +1421,33 @@
13821421 }
13831422
13841423 /* ---- Float format (Ruby-style: always show decimal point) ---- */
1424+ if (SRC_HAS("to_s") || SRC_HAS("puts") || ctx->needs_poly) {
13851425 emit_raw(ctx, "static const char *sp_float_to_s(mrb_float f) {\n");
13861426 emit_raw(ctx, " char *r = (char *)malloc(32);\n");
13871427 emit_raw(ctx, " snprintf(r, 32, \"%%g\", f);\n");
13881428 emit_raw(ctx, " if (!strchr(r, '.') && !strchr(r, 'e') && !strchr(r, 'E')) strcat(r, \".0\");\n");
13891429 emit_raw(ctx, " return r;\n}\n\n");
1430+ }
13901431
13911432 /* ---- Backtick helper (popen/pclose) ---- */
1433+ if (SRC_HAS("`")) {
13921434 emit_raw(ctx, "static const char *sp_backtick(const char *cmd) {\n");
13931435 emit_raw(ctx, " FILE *p = popen(cmd, \"r\");\n");
13941436 emit_raw(ctx, " if (!p) return \"\";\n");
13951437 emit_raw(ctx, " char buf[4096]; size_t n = fread(buf, 1, sizeof(buf)-1, p);\n");
13961438 emit_raw(ctx, " buf[n] = '\\0'; pclose(p);\n");
13971439 emit_raw(ctx, " char *r = (char *)malloc(n+1); memcpy(r, buf, n+1); return r;\n}\n\n");
1440+ }
13981441
13991442 /* ---- format() helper (snprintf wrapper) ---- */
1443+ if (SRC_HAS("format") || SRC_HAS("sprintf") || SRC_HAS("printf")) {
14001444 emit_raw(ctx, "static const char *sp_format(const char *fmt, ...) {\n");
14011445 emit_raw(ctx, " va_list ap; va_start(ap, fmt);\n");
14021446 emit_raw(ctx, " int n = vsnprintf(NULL, 0, fmt, ap); va_end(ap);\n");
14031447 emit_raw(ctx, " char *r = (char *)malloc(n + 1);\n");
14041448 emit_raw(ctx, " va_start(ap, fmt); vsnprintf(r, n + 1, fmt, ap); va_end(ap);\n");
14051449 emit_raw(ctx, " return r;\n}\n\n");
1450+ }
14061451
14071452 /* ---- Polymorphic to_s (after sp_int_to_s/sp_float_to_s) ---- */
14081453 if (ctx->needs_poly) {
@@ -2325,6 +2370,9 @@
23252370 emit_raw(ctx, " if (a->len >= a->cap) { a->cap *= 2; a->data = (sp_Val **)realloc(a->data, sizeof(sp_Val *) * a->cap); }\n");
23262371 emit_raw(ctx, " a->data[a->len++] = v;\n}\n\n");
23272372 }
2373+
2374+ #undef SRC_HAS
2375+ free(_all_src);
23282376 }
23292377
23302378 /* Emit hand-written FizzBuzz helper functions for lambda mode */
@@ -2431,4 +2479,5 @@
24312479 emit_raw(ctx, "}\n\n");
24322480 }
24332481 }
2482+
24342483 }