Commit 0845da76

0845da762132Merged

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

Description

refactor: conditional emission of type-specific runtime helpers

Add needs_intarray, needs_floatarray, needs_range, needs_time, needs_stringio flags. Only emit C helper functions for types actually used by the program.

Detection uses source text scanning (conservative: false positives are harmless, just add unused static functions) plus type inference results.

Results for puts "hello": Before: 585 lines generated C After: 209 lines generated C (64% reduction)

Also refactored needs_* detection: unified SET_NEEDS_FOR_TYPE macro replaces 6 separate type-scanning loops.

60/60 tests, 39/39 benchmarks pass.

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

Details

Commit
0845da762132
Parent
bd7a6cef24f8

src/codegen.cmodifiedView file

@@ -2566,6 +2566,42 @@
25662566 /* Detect string split usage */
25672567 ctx->needs_str_split = has_split_calls(ctx, root);
25682568
2569+ /* Detect type-specific needs by scanning source text for type references.
2570+ * This is a conservative approximation — false positives just add unused
2571+ * static functions (harmless), false negatives are very unlikely. */
2572+ {
2573+ const char *src = (const char *)ctx->parser->start;
2574+ size_t slen = ctx->parser->end - ctx->parser->start;
2575+ for (size_t i = 0; i < slen; i++) {
2576+ if (src[i] == 'F' && i + 4 < slen && (memcmp(src + i, "File.", 5) == 0 || memcmp(src + i, "File ", 5) == 0))
2577+ ctx->needs_file_io = true;
2578+ else if (src[i] == 'D' && i + 3 < slen && memcmp(src + i, "Dir.", 4) == 0)
2579+ ctx->needs_file_io = true;
2580+ else if (src[i] == 'T' && i + 4 < slen && memcmp(src + i, "Time.", 5) == 0)
2581+ ctx->needs_time = true;
2582+ else if (src[i] == 'S' && i + 9 < slen && memcmp(src + i, "StringIO.", 9) == 0)
2583+ { ctx->needs_stringio = true; ctx->needs_gc = true; }
2584+ else if (src[i] == '.' && i + 1 < slen && src[i + 1] == '.')
2585+ { ctx->needs_range = true; ctx->needs_intarray = true; ctx->needs_gc = true; i++; }
2586+ else if (src[i] == '.' && i + 5 < slen && memcmp(src + i, ".bytes", 6) == 0)
2587+ { ctx->needs_intarray = true; ctx->needs_gc = true; }
2588+ }
2589+ /* Also scan required files */
2590+ for (int ri = 0; ri < ctx->required_file_count; ri++) {
2591+ const char *rs = ctx->required_files[ri].source;
2592+ if (!rs) continue;
2593+ size_t rlen = strlen(rs);
2594+ for (size_t i = 0; i < rlen; i++) {
2595+ if (rs[i] == 'F' && i + 4 < rlen && memcmp(rs + i, "File.", 5) == 0)
2596+ ctx->needs_file_io = true;
2597+ else if (rs[i] == 'T' && i + 4 < rlen && memcmp(rs + i, "Time.", 5) == 0)
2598+ ctx->needs_time = true;
2599+ else if (rs[i] == '.' && i + 1 < rlen && rs[i + 1] == '.')
2600+ { ctx->needs_range = true; ctx->needs_intarray = true; ctx->needs_gc = true; i++; }
2601+ }
2602+ }
2603+ }
2604+
25692605 /* Detect regexp usage and collect patterns (must be before emit_header) */
25702606 collect_regexp_patterns(ctx, root);
25712607
@@ -2726,6 +2762,10 @@
27262762
27272763 /* For large multi-file programs, enable all runtime features */
27282764 if (ctx->required_file_count > 5) {
2765+ ctx->needs_intarray = true;
2766+ ctx->needs_floatarray = true;
2767+ ctx->needs_range = true;
2768+ ctx->needs_time = true;
27292769 ctx->needs_hash = true;
27302770 ctx->needs_rb_array = true;
27312771 ctx->needs_rb_hash = true;
@@ -2736,52 +2776,38 @@
27362776 ctx->needs_proc = true;
27372777 }
27382778
2779+ /* Helper: set needs_* flags based on a type kind */
2780+ #define SET_NEEDS_FOR_TYPE(k) do { \
2781+ if ((k) == SPINEL_TYPE_ARRAY) ctx->needs_intarray = true; \
2782+ if ((k) == SPINEL_TYPE_FLOAT_ARRAY) ctx->needs_floatarray = true; \
2783+ if ((k) == SPINEL_TYPE_RANGE) ctx->needs_range = true; \
2784+ if ((k) == SPINEL_TYPE_TIME) ctx->needs_time = true; \
2785+ if ((k) == SPINEL_TYPE_STRINGIO) ctx->needs_stringio = true; \
2786+ if ((k) == SPINEL_TYPE_HASH) { ctx->needs_hash = true; ctx->needs_intarray = true; } \
2787+ if ((k) == SPINEL_TYPE_SP_STRING) ctx->needs_sp_string = true; \
2788+ if ((k) == SPINEL_TYPE_STR_ARRAY) ctx->needs_str_split = true; \
2789+ if ((k) == SPINEL_TYPE_RB_ARRAY) { ctx->needs_rb_array = true; ctx->needs_poly = true; } \
2790+ if ((k) == SPINEL_TYPE_RB_HASH) { ctx->needs_rb_hash = true; ctx->needs_poly = true; } \
2791+ if ((k) == SPINEL_TYPE_POLY) ctx->needs_poly = true; \
2792+ if ((k) == SPINEL_TYPE_FILE) ctx->needs_file_io = true; \
2793+ } while(0)
2794+
27392795 /* Detect needs from class ivars and method params/returns */
27402796 for (int ci = 0; ci < ctx->class_count; ci++) {
27412797 class_info_t *cls = &ctx->classes[ci];
2742- for (int ii = 0; ii < cls->ivar_count; ii++) {
2743- spinel_type_t k = cls->ivars[ii].type.kind;
2744- if (k == SPINEL_TYPE_SP_STRING) ctx->needs_sp_string = true;
2745- if (k == SPINEL_TYPE_HASH) ctx->needs_hash = true;
2746- if (k == SPINEL_TYPE_ARRAY || k == SPINEL_TYPE_FLOAT_ARRAY) ctx->needs_gc = true;
2747- if (k == SPINEL_TYPE_RB_ARRAY) { ctx->needs_rb_array = true; ctx->needs_poly = true; }
2748- if (k == SPINEL_TYPE_RB_HASH) { ctx->needs_rb_hash = true; ctx->needs_poly = true; }
2749- if (k == SPINEL_TYPE_STR_ARRAY) ctx->needs_str_split = true;
2750- if (k == SPINEL_TYPE_POLY) ctx->needs_poly = true;
2751- }
2798+ for (int ii = 0; ii < cls->ivar_count; ii++)
2799+ SET_NEEDS_FOR_TYPE(cls->ivars[ii].type.kind);
27522800 for (int mi = 0; mi < cls->method_count; mi++) {
27532801 method_info_t *m = &cls->methods[mi];
2754- spinel_type_t rk = m->return_type.kind;
2755- if (rk == SPINEL_TYPE_HASH) ctx->needs_hash = true;
2756- if (rk == SPINEL_TYPE_RB_ARRAY) { ctx->needs_rb_array = true; ctx->needs_poly = true; }
2757- if (rk == SPINEL_TYPE_RB_HASH) { ctx->needs_rb_hash = true; ctx->needs_poly = true; }
2758- if (rk == SPINEL_TYPE_STR_ARRAY) ctx->needs_str_split = true;
2759- if (rk == SPINEL_TYPE_SP_STRING) ctx->needs_sp_string = true;
2760- if (rk == SPINEL_TYPE_POLY) ctx->needs_poly = true;
2761- for (int pi = 0; pi < m->param_count; pi++) {
2762- spinel_type_t pk = m->params[pi].type.kind;
2763- if (pk == SPINEL_TYPE_HASH) ctx->needs_hash = true;
2764- if (pk == SPINEL_TYPE_RB_ARRAY) { ctx->needs_rb_array = true; ctx->needs_poly = true; }
2765- if (pk == SPINEL_TYPE_POLY) ctx->needs_poly = true;
2766- }
2802+ SET_NEEDS_FOR_TYPE(m->return_type.kind);
2803+ for (int pi = 0; pi < m->param_count; pi++)
2804+ SET_NEEDS_FOR_TYPE(m->params[pi].type.kind);
27672805 }
27682806 }
27692807
2770- /* Detect needs_sp_string: any SP_STRING-typed variable triggers mutable string runtime */
2771- for (int i = 0; i < ctx->var_count; i++) {
2772- if (ctx->vars[i].type.kind == SPINEL_TYPE_SP_STRING) {
2773- ctx->needs_sp_string = true;
2774- break;
2775- }
2776- }
2777-
2778- /* Detect needs_hash: any HASH-typed variable triggers hash runtime */
2779- for (int i = 0; i < ctx->var_count; i++) {
2780- if (ctx->vars[i].type.kind == SPINEL_TYPE_HASH) {
2781- ctx->needs_hash = true;
2782- break;
2783- }
2784- }
2808+ /* Detect needs from top-level variables */
2809+ for (int i = 0; i < ctx->var_count; i++)
2810+ SET_NEEDS_FOR_TYPE(ctx->vars[i].type.kind);
27852811
27862812 /* Detect needs_proc: any has_block_param function, or PROC-typed variable (from proc {}/Proc.new) */
27872813 for (int i = 0; i < ctx->func_count && !ctx->needs_proc; i++) {
@@ -2796,56 +2822,33 @@
27962822 ctx->needs_proc = true;
27972823 }
27982824
2799- /* Detect needs_gc: any non-value-type class, sp_IntArray, or sp_StrIntHash triggers GC */
2800- for (int i = 0; i < ctx->class_count && !ctx->needs_gc; i++) {
2801- if (!ctx->classes[i].is_value_type)
2802- ctx->needs_gc = true;
2803- }
2804- for (int i = 0; i < ctx->var_count && !ctx->needs_gc; i++) {
2805- if (ctx->vars[i].type.kind == SPINEL_TYPE_ARRAY ||
2806- ctx->vars[i].type.kind == SPINEL_TYPE_FLOAT_ARRAY ||
2807- ctx->vars[i].type.kind == SPINEL_TYPE_HASH)
2808- ctx->needs_gc = true;
2809- }
2810- for (int i = 0; i < ctx->func_count && !ctx->needs_gc; i++) {
2825+ /* Detect needs from top-level function params, returns, and body variables */
2826+ for (int i = 0; i < ctx->func_count; i++) {
28112827 func_info_t *f = &ctx->funcs[i];
2812- if (f->return_type.kind == SPINEL_TYPE_ARRAY ||
2813- f->return_type.kind == SPINEL_TYPE_FLOAT_ARRAY ||
2814- f->return_type.kind == SPINEL_TYPE_HASH) { ctx->needs_gc = true; break; }
2815- for (int j = 0; j < f->param_count && !ctx->needs_gc; j++)
2816- if (f->params[j].type.kind == SPINEL_TYPE_ARRAY ||
2817- f->params[j].type.kind == SPINEL_TYPE_FLOAT_ARRAY ||
2818- f->params[j].type.kind == SPINEL_TYPE_HASH) ctx->needs_gc = true;
2819- /* Also infer function body variables to detect array/hash usage */
2820- if (!ctx->needs_gc && f->body_node) {
2828+ SET_NEEDS_FOR_TYPE(f->return_type.kind);
2829+ for (int j = 0; j < f->param_count; j++)
2830+ SET_NEEDS_FOR_TYPE(f->params[j].type.kind);
2831+ if (f->body_node) {
28212832 int saved_vc = ctx->var_count;
28222833 int saved_sf = ctx->var_scope_floor;
28232834 ctx->var_scope_floor = saved_vc;
28242835 for (int j = 0; j < f->param_count; j++)
28252836 var_declare(ctx, f->params[j].name, f->params[j].type, false);
28262837 infer_pass(ctx, f->body_node);
2827- for (int j = saved_vc; j < ctx->var_count; j++) {
2828- if (ctx->vars[j].type.kind == SPINEL_TYPE_ARRAY ||
2829- ctx->vars[j].type.kind == SPINEL_TYPE_FLOAT_ARRAY ||
2830- ctx->vars[j].type.kind == SPINEL_TYPE_HASH) {
2831- ctx->needs_gc = true;
2832- break;
2833- }
2834- }
2838+ for (int j = saved_vc; j < ctx->var_count; j++)
2839+ SET_NEEDS_FOR_TYPE(ctx->vars[j].type.kind);
28352840 ctx->var_count = saved_vc;
28362841 ctx->var_scope_floor = saved_sf;
28372842 }
28382843 }
2839- /* Also check class method bodies for array/hash usage */
2840- for (int i = 0; i < ctx->class_count && !ctx->needs_gc; i++) {
2844+ /* Detect needs from class method body variables */
2845+ for (int i = 0; i < ctx->class_count; i++) {
28412846 class_info_t *cls = &ctx->classes[i];
2842- pm_parser_t *saved_gc = ctx->parser;
2847+ pm_parser_t *saved_p = ctx->parser;
28432848 if (cls->origin_parser) ctx->parser = cls->origin_parser;
2844- for (int mi = 0; mi < cls->method_count && !ctx->needs_gc; mi++) {
2849+ for (int mi = 0; mi < cls->method_count; mi++) {
28452850 method_info_t *m = &cls->methods[mi];
2846- if (m->return_type.kind == SPINEL_TYPE_ARRAY ||
2847- m->return_type.kind == SPINEL_TYPE_FLOAT_ARRAY ||
2848- m->return_type.kind == SPINEL_TYPE_HASH) { ctx->needs_gc = true; break; }
2851+ SET_NEEDS_FOR_TYPE(m->return_type.kind);
28492852 if (m->body_node) {
28502853 int saved_vc = ctx->var_count;
28512854 int saved_sf = ctx->var_scope_floor;
@@ -2853,21 +2856,22 @@
28532856 for (int j = 0; j < m->param_count; j++)
28542857 var_declare(ctx, m->params[j].name, m->params[j].type, false);
28552858 infer_pass(ctx, m->body_node);
2856- for (int j = saved_vc; j < ctx->var_count; j++) {
2857- if (ctx->vars[j].type.kind == SPINEL_TYPE_ARRAY ||
2858- ctx->vars[j].type.kind == SPINEL_TYPE_FLOAT_ARRAY ||
2859- ctx->vars[j].type.kind == SPINEL_TYPE_HASH) {
2860- ctx->needs_gc = true;
2861- break;
2862- }
2863- }
2859+ for (int j = saved_vc; j < ctx->var_count; j++)
2860+ SET_NEEDS_FOR_TYPE(ctx->vars[j].type.kind);
28642861 ctx->var_count = saved_vc;
28652862 ctx->var_scope_floor = saved_sf;
28662863 }
28672864 }
2868- ctx->parser = saved_gc;
2865+ ctx->parser = saved_p;
28692866 }
28702867
2868+ /* Derive needs_gc from type-specific flags */
2869+ for (int i = 0; i < ctx->class_count && !ctx->needs_gc; i++)
2870+ if (!ctx->classes[i].is_value_type) ctx->needs_gc = true;
2871+ if (ctx->needs_intarray || ctx->needs_floatarray || ctx->needs_hash ||
2872+ ctx->needs_stringio || ctx->needs_rb_array || ctx->needs_rb_hash)
2873+ ctx->needs_gc = true;
2874+
28712875 /* Set up lambda output buffer if needed */
28722876 FILE *lambda_buf = NULL;
28732877 char *lambda_buf_data = NULL;