Commit 743269d1
743269d1a4b9Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
refactor: replace 18 while-loop array iterations with .each blocks
Convert simple index-based while loops (i=0; while i<arr.length; x=arr[i]; ...body...; i=i+1; end) to arr.each { |x| ...body... } where the index variable is only used for array access.
Skipped: string character iterations (no .each on String), and two loops in read_text_ast where .each confuses boot_bin2 type inference.
Bootstrap verified (diff 0), all benchmarks pass.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 743269d1a4b9
- Parent
- 210ca83c0a4f
spinel_codegen.rbmodifiedView file
@@ -2076,29 +2076,21 @@
20762076
stmts = get_body_stmts(root)20772077
20782078
# Pass 0: modules (must come before classes for include)2079-
i = 02080-
while i < stmts.length2081-
sid = stmts[i]2079+
stmts.each { |sid|20822080
if @nd_type[sid] == "ModuleNode"20832081
collect_module(sid)20842082
end2085-
i = i + 12086-
end2083+
}20872084
20882085
# Pass 1: classes2089-
i = 02090-
while i < stmts.length2091-
sid = stmts[i]2086+
stmts.each { |sid|20922087
if @nd_type[sid] == "ClassNode"20932088
collect_class(sid)20942089
end2095-
i = i + 12096-
end2090+
}20972091
20982092
# Pass 2: top-level methods, constants, define_method2099-
i = 02100-
while i < stmts.length2101-
sid = stmts[i]2093+
stmts.each { |sid|21022094
if @nd_type[sid] == "DefNode"21032095
collect_toplevel_method(sid)21042096
end@@ -2110,8 +2102,7 @@
21102102
collect_define_method(sid)21112103
end21122104
end2113-
i = i + 12114-
end2105+
}21152106
21162107
# Pass 2.5: infer lambda parameter types from call sites21172108
infer_lambda_param_types@@ -2148,9 +2139,7 @@
21482139
body = @nd_body[nid]21492140
if body >= 021502141
body_stmts = get_stmts(body)2151-
j = 02152-
while j < body_stmts.length2153-
sid = body_stmts[j]2142+
body_stmts.each { |sid|21542143
if @nd_type[sid] == "DefNode"21552144
# Add as top-level method with prefix21562145
mname = @nd_name[sid]@@ -2165,8 +2154,7 @@
21652154
@meth_has_yield.push(0)21662155
@meth_has_defaults.push("0")21672156
end2168-
j = j + 12169-
end2157+
}21702158
end21712159
return21722160
end@@ -2282,9 +2270,7 @@
22822270
end22832271
body_stmts = get_stmts(body)22842272
# First pass: collect all class methods and attrs2285-
j = 02286-
while j < body_stmts.length2287-
sid = body_stmts[j]2273+
body_stmts.each { |sid|22882274
if @nd_type[sid] == "DefNode"22892275
collect_class_method(ci, sid)22902276
end@@ -2296,12 +2282,9 @@
22962282
end22972283
end22982284
end2299-
j = j + 12300-
end2285+
}23012286
# Second pass: handle includes (after all own methods are known)2302-
j = 02303-
while j < body_stmts.length2304-
sid = body_stmts[j]2287+
body_stmts.each { |sid|23052288
if @nd_type[sid] == "CallNode"23062289
if @nd_name[sid] == "include"23072290
inc_args = @nd_arguments[sid]@@ -2318,8 +2301,7 @@
23182301
end23192302
end23202303
end2321-
j = j + 12322-
end2304+
}23232305
23242306
# Collect ivars23252307
collect_ivars(ci)@@ -3027,9 +3009,7 @@
30273009
return30283010
end30293011
body_stmts = get_stmts(body)3030-
j = 03031-
while j < body_stmts.length3032-
sid = body_stmts[j]3012+
body_stmts.each { |sid|30333013
if @nd_type[sid] == "ConstantWriteNode"30343014
cname = mname + "_" + @nd_name[sid]30353015
expr_id = @nd_expression[sid]@@ -3071,8 +3051,7 @@
30713051
@const_types.push(ct)30723052
@const_expr_ids.push(expr_id)30733053
end3074-
j = j + 13075-
end3054+
}30763055
end30773056
30783057
def collect_constant(nid)@@ -3576,9 +3555,7 @@
35763555
end35773556
if bid >= 035783557
stmts = get_stmts(bid)3579-
k = 03580-
while k < stmts.length3581-
sid = stmts[k]3558+
stmts.each { |sid|35823559
if @nd_type[sid] == "InstanceVariableWriteNode"35833560
expr = @nd_expression[sid]35843561
if expr >= 0@@ -3615,8 +3592,7 @@
36153592
end36163593
end36173594
end3618-
k = k + 13619-
end3595+
}36203596
end36213597
mi = mi + 136223598
end@@ -3850,9 +3826,7 @@
38503826
lnames = "".split(",")38513827
ltypes = "".split(",")38523828
empty_p = "".split(",")3853-
i = 03854-
while i < stmts.length3855-
sid = stmts[i]3829+
stmts.each { |sid|38563830
if @nd_type[sid] != "DefNode"38573831
if @nd_type[sid] != "ClassNode"38583832
if @nd_type[sid] != "ConstantWriteNode"@@ -3862,8 +3836,7 @@
38623836
end38633837
end38643838
end3865-
i = i + 13866-
end3839+
}38673840
k = 038683841
while k < lnames.length38693842
declare_var(lnames[k], ltypes[k])@@ -4562,9 +4535,7 @@
45624535
end45634536
if bid >= 045644537
stmts = get_stmts(bid)4565-
k = 04566-
while k < stmts.length4567-
sid = stmts[k]4538+
stmts.each { |sid|45684539
if @nd_type[sid] == "InstanceVariableWriteNode"45694540
expr = @nd_expression[sid]45704541
if expr >= 0@@ -4606,8 +4577,7 @@
46064577
end46074578
end46084579
end4609-
k = k + 14610-
end4580+
}46114581
end46124582
end46134583
j = j + 1@@ -4746,9 +4716,7 @@
47464716
args_id = @nd_arguments[nid]47474717
if args_id >= 047484718
aargs = get_args(args_id)4749-
k = 04750-
while k < aargs.length4751-
aid = aargs[k]4719+
aargs.each { |aid|47524720
if @nd_type[aid] == "CallNode"47534721
if @nd_name[aid] == mname47544722
if @nd_receiver[aid] < 0@@ -4760,8 +4728,7 @@
47604728
end47614729
end47624730
end4763-
k = k + 14764-
end4731+
}47654732
end47664733
end47674734
# Check if method is called in a negation context (boolean)@@ -4878,9 +4845,7 @@
48784845
lnames = "".split(",")48794846
ltypes = "".split(",")48804847
empty_p = "".split(",")4881-
i = 04882-
while i < stmts.length4883-
sid = stmts[i]4848+
stmts.each { |sid|48844849
if @nd_type[sid] != "DefNode"48854850
if @nd_type[sid] != "ClassNode"48864851
if @nd_type[sid] != "ConstantWriteNode"@@ -4890,8 +4855,7 @@
48904855
end48914856
end48924857
end4893-
i = i + 14894-
end4858+
}48954859
k = 048964860
while k < lnames.length48974861
declare_var(lnames[k], ltypes[k])@@ -5264,9 +5228,7 @@
52645228
lnames = "".split(",")52655229
ltypes = "".split(",")52665230
empty_p = "".split(",")5267-
i = 05268-
while i < stmts.length5269-
sid = stmts[i]5231+
stmts.each { |sid|52705232
if @nd_type[sid] != "DefNode"52715233
if @nd_type[sid] != "ClassNode"52725234
if @nd_type[sid] != "ConstantWriteNode"@@ -5274,8 +5236,7 @@
52745236
end52755237
end52765238
end5277-
i = i + 15278-
end5239+
}52795240
k = 052805241
while k < lnames.length52815242
declare_var(lnames[k], ltypes[k])@@ -5399,16 +5360,13 @@
53995360
stmts = get_body_stmts(@root_id)54005361
local_types = "".split(",")54015362
local_names = "".split(",")5402-
i = 05403-
while i < stmts.length5404-
sid = stmts[i]5363+
stmts.each { |sid|54055364
if @nd_type[sid] != "DefNode"54065365
if @nd_type[sid] != "ClassNode"54075366
scan_poly_assigns(sid, local_names, local_types)54085367
end54095368
end5410-
i = i + 15411-
end5369+
}54125370
end54135371
54145372
def scan_poly_assigns(nid, names, types)@@ -5519,9 +5477,7 @@
55195477
end55205478
if @nd_type[nid] == "MultiWriteNode"55215479
targets = parse_id_list(@nd_targets[nid])5522-
k = 05523-
while k < targets.length5524-
tid = targets[k]5480+
targets.each { |tid|55255481
if @nd_type[tid] == "LocalVariableTargetNode"55265482
lname = @nd_name[tid]55275483
if not_in(lname, names) == 1@@ -5531,8 +5487,7 @@
55315487
end55325488
end55335489
end5534-
k = k + 15535-
end5490+
}55365491
end55375492
# Recurse55385493
if @nd_body[nid] >= 0@@ -6768,9 +6723,7 @@
67686723
k = k + 167696724
end67706725
stmts = get_stmts(bid)6771-
k = 06772-
while k < stmts.length6773-
sid = stmts[k]6726+
stmts.each { |sid|67746727
if @nd_type[sid] == "SuperNode"67756728
if @cls_parents[ci] != ""67766729
pi = find_class_idx(@cls_parents[ci])@@ -6802,8 +6755,7 @@
68026755
compile_stmt(sid)68036756
end68046757
end6805-
k = k + 16806-
end6758+
}68076759
pop_scope68086760
@current_class_idx = -168096761
end@@ -6872,16 +6824,13 @@
68726824
k = k + 168736825
end68746826
stmts = get_stmts(bid)6875-
k = 06876-
while k < stmts.length6877-
sid = stmts[k]6827+
stmts.each { |sid|68786828
if @nd_type[sid] == "InstanceVariableWriteNode"68796829
ivar = sanitize_ivar(@nd_name[sid])68806830
val = compile_expr(@nd_expression[sid])68816831
emit_raw(" self->" + ivar + " = " + val + ";")68826832
end6883-
k = k + 16884-
end6833+
}68856834
pop_scope68866835
@current_class_idx = -168876836
end@@ -7179,9 +7128,7 @@
71797128
end71807129
if @nd_type[nid] == "MultiWriteNode"71817130
targets = parse_id_list(@nd_targets[nid])7182-
k = 07183-
while k < targets.length7184-
tid = targets[k]7131+
targets.each { |tid|71857132
if @nd_type[tid] == "LocalVariableTargetNode"71867133
lname = @nd_name[tid]71877134
if not_in(lname, names) == 1@@ -7191,8 +7138,7 @@
71917138
end71927139
end71937140
end7194-
k = k + 17195-
end7141+
}71967142
if @nd_expression[nid] >= 071977143
scan_locals(@nd_expression[nid], names, types, params)71987144
end