Commit f1b3d21d
f1b3d21dcdd7Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
perf: reduce bootstrap memory from 42GB to 12GB
Replace O(N²) string concatenation in emit/emit_raw with StrArray-based line accumulation (@out_lines). Each emit is now O(1) amortized push instead of copying the entire output buffer.
- emit/emit_raw: push lines to @out_lines instead of @out += s
- Lambda/fiber/tuple deferred insertion: use separate string buffers (@deferred_lambda, @deferred_tuple) with marker-based insertion in final_output via gsub
- Save/restore pattern: swap @out_lines for sub-compilation, join on retrieval
- final_output: join @out_lines and replace markers with deferred content
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- f1b3d21dcdd7
- Parent
- 52b63438cbdd
spinel_codegen.rbmodifiedView file
@@ -11,8 +11,26 @@
1111
class Compiler1212
attr_accessor :out1313
14+
def final_output15+
result = @out_lines.join(10.chr) + 10.chr16+
if @deferred_tuple != ""17+
result = result.gsub("/*TUPLE_INSERT_POINT*/", @deferred_tuple)18+
else19+
result = result.gsub("/*TUPLE_INSERT_POINT*/" + 10.chr, "")20+
end21+
if @deferred_lambda != ""22+
result = result.gsub("/*LAMBDA_INSERT_POINT*/", @deferred_lambda)23+
else24+
result = result.gsub("/*LAMBDA_INSERT_POINT*/" + 10.chr, "")25+
end26+
result27+
end28+
1429
def initialize30+
@out_lines = "".split(",")1531
@out = ""32+
@deferred_tuple = ""33+
@deferred_lambda = ""1634
@indent = 01735
@temp_counter = 01836
@label_counter = 0@@ -775,11 +793,11 @@
775793
ind = ind + " "776794
j = j + 1777795
end778-
@out = @out + ind + s + 10.chr796+
@out_lines.push(ind + s)779797
end780798
781799
def emit_raw(s)782-
@out = @out + s + 10.chr800+
@out_lines.push(s)783801
end784802
785803
@@ -6979,7 +6997,7 @@
69796997
emit_regexp_runtime69806998
end69816999
emit_class_structs6982-
@tuple_insert_pos = @out.length7000+
emit_raw("/*TUPLE_INSERT_POINT*/")69837001
emit_gc_scan_functions69847002
# Emit global variable declarations before functions69857003
gi = 0@@ -7000,16 +7018,14 @@
70007018
end70017019
emit_forward_decls70027020
emit_global_constants7003-
# Lambda functions will be inserted here (before class/toplevel methods)7004-
@lambda_insert_pos = @out.length7021+
emit_raw("/*LAMBDA_INSERT_POINT*/")70057022
emit_class_methods70067023
emit_toplevel_methods70077024
# Emit lambda functions before main (they are generated during compilation)70087025
# We emit them in emit_main after forward declarations70097026
emit_main7010-
# Insert tuple struct definitions at the saved position7027+
# Build tuple struct definitions into @deferred_tuple70117028
if @tuple_types.length > 07012-
tuple_lines = ""70137029
k = 070147030
while k < @tuple_types.length70157031
t = @tuple_types[k]@@ -7024,11 +7040,13 @@
70247040
fields = fields + c_type(parts[fi]) + " _" + fi.to_s + ";"70257041
fi = fi + 170267042
end7027-
tuple_lines = tuple_lines + "typedef struct { " + fields + " } " + name + ";\n"7043+
@deferred_tuple << "typedef struct { "7044+
@deferred_tuple << fields7045+
@deferred_tuple << " } "7046+
@deferred_tuple << name7047+
@deferred_tuple << ";\n"70287048
k = k + 170297049
end7030-
# Prepend before the content at @tuple_insert_pos7031-
@out[@tuple_insert_pos] = tuple_lines + @out[@tuple_insert_pos]70327050
end70337051
070347052
end@@ -10086,17 +10104,14 @@
1008610104
pop_scope1008710105
@in_main = 01008810106
10089-
# Insert lambda and fiber functions before main10090-
inserted = ""10107+
# Accumulate lambda and fiber functions into deferred buffer1009110108
if @lambda_funcs != ""10092-
inserted = inserted + @lambda_funcs10109+
@deferred_lambda << @lambda_funcs1009310110
end1009410111
if @fiber_funcs != ""10095-
inserted = inserted + @fiber_funcs10096-
end10097-
if inserted != ""10098-
@out = @out[0, @lambda_insert_pos] + inserted + @out[@lambda_insert_pos, @out.length - @lambda_insert_pos]10112+
@deferred_lambda << @fiber_funcs1009910113
end10114+
01010010115
end1010110116
1010210117
# ---- Expression compiler ----@@ -10872,14 +10887,14 @@
1087210887
end1087310888
1087410889
# Save/restore compiler state10875-
saved_out = @out10890+
saved_out = @out_lines1087610891
saved_indent = @indent1087710892
saved_in_fiber_body = @in_fiber_body1087810893
saved_fiber_captures = @fiber_captures1087910894
saved_fiber_capture_types = @fiber_capture_types1088010895
saved_hp_names_len = @heap_promoted_names.length1088110896
saved_hp_cells_len = @heap_promoted_cells.length10882-
@out = ""10897+
@out_lines = "".split(",")1088310898
@indent = 11088410899
@in_fiber_body = 11088510900
@fiber_captures = free_vars@@ -10919,11 +10934,12 @@
1091910934
end1092010935
pop_scope1092110936
10922-
fbody = fbody + @out10937+
fbody = fbody + @out_lines.join(10.chr) + 10.chr1092310938
fbody = fbody + "}" + 10.chr10924-
@fiber_funcs = @fiber_funcs + cap_typedef + fbody10939+
@fiber_funcs << cap_typedef10940+
@fiber_funcs << fbody1092510941
10926-
@out = saved_out10942+
@out_lines = saved_out1092710943
@indent = saved_indent1092810944
@in_fiber_body = saved_in_fiber_body1092910945
@fiber_captures = saved_fiber_captures@@ -16919,11 +16935,11 @@
1691916935
bs = get_stmts(body)1692016936
if bs.length > 0 && has_typed_caps == 11692116937
# Typed captures: compile body using regular compiler16922-
save_out = @out16938+
save_out = @out_lines1692316939
save_indent = @indent1692416940
save_hp_names_len = @heap_promoted_names.length1692516941
save_hp_cells_len = @heap_promoted_cells.length16926-
@out = ""16942+
@out_lines = "".split(",")1692716943
@indent = 11692816944
1692916945
push_scope@@ -16981,8 +16997,8 @@
1698116997
last_val = compile_expr(last)1698216998
1698316999
pop_scope16984-
body_stmts = @out16985-
@out = save_out17000+
body_stmts = @out_lines.join(10.chr) + 10.chr17001+
@out_lines = save_out1698617002
@indent = save_indent1698717003
# Restore heap promoted1698817004
while @heap_promoted_names.length > save_hp_names_len@@ -16993,21 +17009,25 @@
1699317009
end1699417010
1699517011
# Build lambda function with typed body16996-
@lambda_funcs = @lambda_funcs + "static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) {\n"17012+
@lambda_funcs <<"static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) {\n"1699717013
if pname != ""16998-
@lambda_funcs = @lambda_funcs + " mrb_int lv_" + pname + " = sp_lam_to_int(arg);\n"16999-
end17000-
@lambda_funcs = @lambda_funcs + body_stmts + 10.chr17014+
@lambda_funcs << " mrb_int lv_"17015+
@lambda_funcs << pname17016+
@lambda_funcs << " = sp_lam_to_int(arg);\n"17017+
end17018+
@lambda_funcs << body_stmts17019+
@lambda_funcs << 10.chr1700117020
bexpr = lam_box(last_val, last_type)17002-
@lambda_funcs = @lambda_funcs + " return " + bexpr + ";\n"17003-
@lambda_funcs = @lambda_funcs + "}\n\n"17021+
@lambda_funcs << " return "17022+
@lambda_funcs << bexpr17023+
@lambda_funcs << ";\n}\n\n"1700417024
elsif bs.length > 01700517025
# No typed captures: use sp_Val* lambda body compiler17006-
save_out = @out17026+
save_out = @out_lines1700717027
save_params = @lambda_params1700817028
save_captures = @lambda_captures1700917029
save_cell_types = @lambda_capture_cell_types17010-
@out = ""17030+
@out_lines = "".split(",")1701117031
@lambda_params = param_arr1701217032
@lambda_captures = free_vars1701317033
@lambda_capture_cell_types = cap_cell_types@@ -17018,35 +17038,35 @@
1701817038
si = si + 11701917039
end1702017040
bexpr = compile_lambda_body_expr(bs.last, param_arr, free_vars)17021-
body_stmts = @out17022-
@out = save_out17041+
body_stmts = @out_lines.join(10.chr) + 10.chr17042+
@out_lines = save_out1702317043
@lambda_params = save_params1702417044
@lambda_captures = save_captures1702517045
@lambda_capture_cell_types = save_cell_types1702617046
1702717047
if body_stmts != ""17028-
@lambda_funcs = @lambda_funcs + "static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) {\n"17048+
@lambda_funcs <<"static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) {\n"1702917049
if pname != ""17030-
@lambda_funcs = @lambda_funcs + " sp_Val *lv_" + pname + " = arg;\n"17031-
end17032-
@lambda_funcs = @lambda_funcs + " (void)self;\n"17033-
@lambda_funcs = @lambda_funcs + body_stmts + 10.chr17034-
@lambda_funcs = @lambda_funcs + " return " + bexpr + ";\n"17035-
@lambda_funcs = @lambda_funcs + "}\n\n"17050+
@lambda_funcs <<" sp_Val *lv_" + pname + " = arg;\n"17051+
end17052+
@lambda_funcs <<" (void)self;\n"17053+
@lambda_funcs <<body_stmts + 10.chr17054+
@lambda_funcs <<" return " + bexpr + ";\n"17055+
@lambda_funcs <<"}\n\n"1703617056
else17037-
@lambda_funcs = @lambda_funcs + "static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) {\n"17057+
@lambda_funcs <<"static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) {\n"1703817058
if pname != ""17039-
@lambda_funcs = @lambda_funcs + " sp_Val *lv_" + pname + " = arg;\n"17059+
@lambda_funcs <<" sp_Val *lv_" + pname + " = arg;\n"1704017060
end17041-
@lambda_funcs = @lambda_funcs + " (void)self;\n"17042-
@lambda_funcs = @lambda_funcs + " return " + bexpr + ";\n"17043-
@lambda_funcs = @lambda_funcs + "}\n\n"17061+
@lambda_funcs <<" (void)self;\n"17062+
@lambda_funcs <<" return " + bexpr + ";\n"17063+
@lambda_funcs <<"}\n\n"1704417064
end1704517065
else17046-
@lambda_funcs = @lambda_funcs + "static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) { (void)self; (void)arg; return &sp_lam_nil_val; }\n\n"17066+
@lambda_funcs <<"static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) { (void)self; (void)arg; return &sp_lam_nil_val; }\n\n"1704717067
end1704817068
else17049-
@lambda_funcs = @lambda_funcs + "static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) { (void)self; (void)arg; return &sp_lam_nil_val; }\n\n"17069+
@lambda_funcs <<"static sp_Val *" + fname + "(sp_Val *self, sp_Val *arg) { (void)self; (void)arg; return &sp_lam_nil_val; }\n\n"1705017070
end1705117071
1705217072
# Build the closure creation expression@@ -17130,8 +17150,8 @@
1713017150
fname = "_sp_proc_fn_" + @proc_counter.to_s1713117151
bbody = @nd_body[blk]1713217152
# Save current output, compile body into a separate buffer17133-
save_out = @out17134-
@out = ""17153+
save_out = @out_lines17154+
@out_lines = "".split(",")1713517155
push_scope1713617156
declare_var(bp, "int")1713717157
bexpr = "0"@@ -17146,11 +17166,11 @@
1714617166
if k == bs.length - 11714717167
if lt != "void"1714817168
# Last statement: compile all previous, then get return expr17149-
body_stmts = @out17150-
@out = ""17169+
body_stmts = @out_lines.join(10.chr) + 10.chr17170+
@out_lines = "".split(",")1715117171
bexpr = compile_expr(bs[k])17152-
extra = @out17153-
@out = ""17172+
extra = @out_lines.join(10.chr) + 10.chr17173+
@out_lines = "".split(",")1715417174
body_stmts = body_stmts + extra1715517175
else1715617176
# Last is void (like puts): compile as statement, return 0@@ -17162,13 +17182,13 @@
1716217182
k = k + 11716317183
end1716417184
if body_stmts == ""17165-
body_stmts = @out17166-
@out = ""17185+
body_stmts = @out_lines.join(10.chr) + 10.chr17186+
@out_lines = "".split(",")1716717187
end1716817188
end1716917189
end1717017190
pop_scope17171-
@out = save_out17191+
@out_lines = save_out1717217192
# Build function body1717317193
if body_stmts != ""1717417194
return "({ mrb_int " + fname + "(mrb_int lv_" + bp + ") { " + body_stmts.strip + " return " + bexpr + "; } sp_proc_new(" + fname + "); })"@@ -19624,8 +19644,9 @@
1962419644
compiler.read_text_ast(data)1962519645
compiler.compile1962619646
19647+
final = compiler.final_output1962719648
if out_file != nil19628-
File.write(out_file, compiler.out)19649+
File.write(out_file, final)1962919650
else19630-
print compiler.out19651+
print final1963119652
end