Commit afcf936e

afcf936e27b8Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · +1 added · ~1 modified

Description

fix: File.open write block with while/if loops

compile_file_block_stmt now recursively handles WhileNode and IfNode inside File.open blocks, so f.puts works inside while loops.

Add io_wordcount benchmark (5.2x vs Ruby): line-by-line file reading with Hash word frequency counting.

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

Details

Commit
afcf936e27b8
Parent
da62c0e2c75c

benchmark/io_wordcount.rbaddedView file

1+# Line-by-line file processing with File.open block
2+# Tests: File.open, each_line, string split, Hash
3+
4+# Write test data: word frequency file
5+words = ["the", "quick", "brown", "fox", "jumps", "over", "lazy", "dog"]
6+File.open("/tmp/spinel_io_wf.txt", "w") do |f|
7+ i = 0
8+ while i < 5000
9+ w = words[i % 8]
10+ f.puts(w + " " + (i % 100).to_s)
11+ i = i + 1
12+ end
13+end
14+
15+# Process: count word frequencies
16+counts = {}
17+File.open("/tmp/spinel_io_wf.txt", "r") do |f|
18+ f.each_line do |line|
19+ parts = line.strip.split(" ")
20+ if parts.length >= 1
21+ word = parts[0]
22+ if counts.has_key?(word)
23+ counts[word] = counts[word] + 1
24+ else
25+ counts[word] = 1
26+ end
27+ end
28+ end
29+end
30+
31+# Print sorted results
32+counts.each do |k, v|
33+ puts k + ": " + v.to_s
34+end
35+
36+# Process 20 times for benchmark
37+i = 0
38+while i < 20
39+ c = {}
40+ File.open("/tmp/spinel_io_wf.txt", "r") do |f|
41+ f.each_line do |line|
42+ parts = line.strip.split(" ")
43+ if parts.length >= 1
44+ w = parts[0]
45+ if c.has_key?(w)
46+ c[w] = c[w] + 1
47+ else
48+ c[w] = 1
49+ end
50+ end
51+ end
52+ end
53+ i = i + 1
54+end
55+
56+File.delete("/tmp/spinel_io_wf.txt")
57+puts "done"