Commit ec200ad1
ec200ad10f3aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
perf: gate sym runtime emission — minimal programs stay lean
Restructure emit_sym_runtime so unused symbol infrastructure is not emitted:
- No symbols: only a sp_sym_to_s stub (1 line, for SP_TAG_SYM in sp_runtime.h). Before: 14 lines of dead code.
- Symbols but no String#to_sym: static table + sp_sym_to_s only. No dynamic intern pool (sp_sym_dyn_*, sp_sym_intern).
- String#to_sym used: full dynamic intern pool emitted, gated on @needs_sym_intern flag set in scan_features.
Sort helpers (sp_sym_sort_cmp, sp_sym_array_sort) are emitted when symbols exist, with attribute((unused)) so the compiler strips them in programs that don't sort.
Fix bootstrap regression: has_syms = x.length > 0 local var
triggered a self-host codegen bug (garbled >= 0 comparison in
unrelated code). Inlined the condition instead.
75/75 tests pass, bootstrap stable.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- ec200ad10f3a
- Parent
- 96e7f4b37fd9
spinel_codegen.rbmodifiedView file
@@ -158,6 +158,7 @@
158158
@needs_str_str_hash = 0159159
@needs_sym_int_hash = 0160160
@needs_sym_str_hash = 0161+
@needs_sym_intern = 0161162
@needs_string_helpers = 0162163
@needs_setjmp = 0163164
@needs_file_io = 0@@ -6134,6 +6135,14 @@
61346135
@needs_str_array = 161356136
@needs_gc = 161366137
end6138+
if mname == "to_sym" || mname == "intern"6139+
if @nd_receiver[nid] >= 06140+
rt = infer_type(@nd_receiver[nid])6141+
if rt == "string"6142+
@needs_sym_intern = 16143+
end6144+
end6145+
end61376146
# Methods that need string helpers only when receiver is string61386147
if mname == "+" || mname == "*" || mname == "reverse"61396148
if @nd_receiver[nid] >= 0@@ -7684,11 +7693,9 @@
76847693
# Symbol type Phase 2, Step 2: emit the intern table and helpers.76857694
# SymbolNode now compiles to sp_sym values that index into sp_sym_names.76867695
def emit_sym_runtime7687-
emit_raw("/* sp_sym intern table (sp_sym typedef is in sp_runtime.h) */")7688-
emit_raw("#define SP_SYM_COUNT " + @sym_names.length.to_s)7689-
if @sym_names.length == 07690-
emit_raw("static const char *const sp_sym_names[1] __attribute__((unused)) = {0};")7691-
else7696+
if @sym_names.length > 07697+
emit_raw("/* sp_sym intern table */")7698+
emit_raw("#define SP_SYM_COUNT " + @sym_names.length.to_s)76927699
line = "static const char *const sp_sym_names[" + @sym_names.length.to_s + "] = {"76937700
i = 076947701
while i < @sym_names.length@@ -7700,27 +7707,36 @@
77007707
end77017708
line = line + "};"77027709
emit_raw(line)7703-
end7704-
# Dynamic intern pool: symbols created via String#to_sym at runtime.7705-
# IDs beyond SP_SYM_COUNT index into this pool.7706-
emit_raw("static const char **sp_sym_dyn_names = NULL;")7707-
emit_raw("static mrb_int sp_sym_dyn_count = 0;")7708-
emit_raw("static mrb_int sp_sym_dyn_cap = 0;")7709-
emit_raw("static sp_sym sp_sym_intern(const char *s) __attribute__((unused));")7710-
emit_raw("static sp_sym sp_sym_intern(const char *s){mrb_int i;for(i=0;i<SP_SYM_COUNT;i++){if(strcmp(sp_sym_names[i],s)==0)return (sp_sym)i;}for(i=0;i<sp_sym_dyn_count;i++){if(strcmp(sp_sym_dyn_names[i],s)==0)return (sp_sym)(SP_SYM_COUNT+i);}if(sp_sym_dyn_count>=sp_sym_dyn_cap){sp_sym_dyn_cap=sp_sym_dyn_cap?sp_sym_dyn_cap*2:8;sp_sym_dyn_names=(const char**)realloc(sp_sym_dyn_names,sizeof(char*)*sp_sym_dyn_cap);}{size_t sl=strlen(s);char*dup=(char*)malloc(sl+1);memcpy(dup,s,sl+1);sp_sym_dyn_names[sp_sym_dyn_count]=dup;}return (sp_sym)(SP_SYM_COUNT+sp_sym_dyn_count++);}")7711-
emit_raw("static const char *sp_sym_to_s(sp_sym id) __attribute__((unused));")7712-
emit_raw("static const char *sp_sym_to_s(sp_sym id){if(id<0)return \"\";if(id<SP_SYM_COUNT)return sp_sym_names[id];mrb_int idx=(mrb_int)id-SP_SYM_COUNT;if(idx>=sp_sym_dyn_count)return \"\";return sp_sym_dyn_names[idx];}")7713-
# Sort comparator for sym arrays: lexical by symbol name.7714-
emit_raw("static int sp_sym_sort_cmp(const void*a,const void*b){return strcmp(sp_sym_to_s(*(const sp_sym*)a),sp_sym_to_s(*(const sp_sym*)b));}")7715-
emit_raw("static void sp_sym_array_sort(sp_IntArray*a){qsort(a->data+a->start,a->len,sizeof(mrb_int),sp_sym_sort_cmp);}")7716-
# Emit SPS_<name> defines for symbols that form valid C identifiers.7717-
i = 07718-
while i < @sym_names.length7719-
nm = @sym_names[i]7720-
if sym_is_c_ident(nm) == 17721-
emit_raw("#define SPS_" + nm + " ((sp_sym)" + i.to_s + ")")7710+
if @needs_sym_intern == 17711+
# Dynamic intern pool: String#to_sym at runtime.7712+
emit_raw("static const char **sp_sym_dyn_names = NULL;")7713+
emit_raw("static mrb_int sp_sym_dyn_count = 0;")7714+
emit_raw("static mrb_int sp_sym_dyn_cap = 0;")7715+
emit_raw("static sp_sym sp_sym_intern(const char *s) __attribute__((unused));")7716+
emit_raw("static sp_sym sp_sym_intern(const char *s){mrb_int i;for(i=0;i<SP_SYM_COUNT;i++){if(strcmp(sp_sym_names[i],s)==0)return (sp_sym)i;}for(i=0;i<sp_sym_dyn_count;i++){if(strcmp(sp_sym_dyn_names[i],s)==0)return (sp_sym)(SP_SYM_COUNT+i);}if(sp_sym_dyn_count>=sp_sym_dyn_cap){sp_sym_dyn_cap=sp_sym_dyn_cap?sp_sym_dyn_cap*2:8;sp_sym_dyn_names=(const char**)realloc(sp_sym_dyn_names,sizeof(char*)*sp_sym_dyn_cap);}{size_t sl=strlen(s);char*dup=(char*)malloc(sl+1);memcpy(dup,s,sl+1);sp_sym_dyn_names[sp_sym_dyn_count]=dup;}return (sp_sym)(SP_SYM_COUNT+sp_sym_dyn_count++);}")7717+
emit_raw("static const char *sp_sym_to_s(sp_sym id) __attribute__((unused));")7718+
emit_raw("static const char *sp_sym_to_s(sp_sym id){if(id<0)return \"\";if(id<SP_SYM_COUNT)return sp_sym_names[id];mrb_int idx=(mrb_int)id-SP_SYM_COUNT;if(idx>=sp_sym_dyn_count)return \"\";return sp_sym_dyn_names[idx];}")7719+
else7720+
emit_raw("static const char *sp_sym_to_s(sp_sym id) __attribute__((unused));")7721+
emit_raw("static const char *sp_sym_to_s(sp_sym id){if(id>=0&&id<SP_SYM_COUNT)return sp_sym_names[id];return \"\";}")7722+
end7723+
# Emit SPS_<name> defines for symbols that form valid C identifiers.7724+
i = 07725+
while i < @sym_names.length7726+
nm = @sym_names[i]7727+
if sym_is_c_ident(nm) == 17728+
emit_raw("#define SPS_" + nm + " ((sp_sym)" + i.to_s + ")")7729+
end7730+
i = i + 177227731
end7723-
i = i + 17732+
# Sort comparator for sym arrays: lexical by symbol name.7733+
emit_raw("static int sp_sym_sort_cmp(const void*a,const void*b) __attribute__((unused));")7734+
emit_raw("static int sp_sym_sort_cmp(const void*a,const void*b){return strcmp(sp_sym_to_s(*(const sp_sym*)a),sp_sym_to_s(*(const sp_sym*)b));}")7735+
emit_raw("static void sp_sym_array_sort(sp_IntArray*a) __attribute__((unused));")7736+
emit_raw("static void sp_sym_array_sort(sp_IntArray*a){qsort(a->data+a->start,a->len,sizeof(mrb_int),sp_sym_sort_cmp);}")7737+
else7738+
# No symbols used at all — provide stub for sp_runtime.h's SP_TAG_SYM7739+
emit_raw("static const char *sp_sym_to_s(sp_sym id){(void)id;return \"\";}")77247740
end77257741
emit_raw("")77267742
if @needs_sym_int_hash == 1