Commit 8990f4cd

8990f4cdb11cMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

docs: update README with current benchmarks and optimization list

  • Refresh benchmark tables with current numbers (75 tests, 52 bench pass). Geometric mean ~49x vs CRuby across 50 benchmarks.
  • Add Symbols section (sp_sym, SymIntHash/SymStrHash details).
  • Add Optimizations section documenting all Tier 1/2 passes: value-type promotion (≤8 ivars), constant propagation, loop length hoisting with mutation detection, method inlining via static inline, concat chain flattening, bigint auto-promotion, bigint to_s via mpz_get_str, static symbol interning, split reuse, -ffunction-sections dead-code elimination.
  • Bump file line counts to current state.

HANDOFF.md updates done separately (gitignored internal doc).

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

Details

Commit
8990f4cdb11c
Parent
f1aaf058a17e

README.mdmodifiedView file

@@ -78,44 +78,48 @@
7878
7979 ## Benchmarks
8080
81-52/53 benchmarks pass. 65 tests pass.
81+75 tests pass. 52/54 benchmarks pass (2 pre-existing failures).
82+Geometric mean: **~49x faster** than CRuby across 50 benchmarks.
8283
8384 ### Computation
8485
8586 | Benchmark | Spinel | CRuby | Speedup |
86-|-----------|--------|-------|---------|
87-| life (Game of Life) | 2 ms | 1,487 ms | **743x** |
88-| ackermann | 6 ms | 397 ms | **66x** |
89-| mandelbrot | 24 ms | 1,179 ms | **49x** |
90-| matmul | 8 ms | 319 ms | **39x** |
91-| nqueens | 770 ms | 25,600 ms | **33x** |
92-| tarai | 16 ms | 433 ms | **27x** |
93-| tak | 22 ms | 506 ms | **23x** |
94-| sudoku | 7 ms | 148 ms | **21x** |
95-| sieve | 20 ms | 410 ms | **20x** |
96-| partial_sums | 82 ms | 1,282 ms | **15x** |
87+|---|---|---|---|
88+| ackermann | 5 ms | 394 ms | **78.8x** |
89+| fib (recursive) | 11 ms | 560 ms | **50.9x** |
90+| mandelbrot | 24 ms | 1,187 ms | **49.5x** |
91+| matmul | 9 ms | 329 ms | **36.6x** |
92+| fasta (DNA seq gen) | 2 ms | 72 ms | **36.0x** |
93+| fannkuch | 2 ms | 71 ms | **35.5x** |
94+| nqueens | 777 ms | 24,912 ms | **32.1x** |
95+| tarai | 16 ms | 423 ms | **26.4x** |
96+| tak | 20 ms | 499 ms | **24.9x** |
97+| sudoku | 6 ms | 148 ms | **24.7x** |
98+| partial_sums | 81 ms | 1,282 ms | **15.8x** |
99+| sieve | 38 ms | 435 ms | **11.4x** |
97100
98101 ### Data Structures & GC
99102
100103 | Benchmark | Spinel | CRuby | Speedup |
101-|-----------|--------|-------|---------|
102-| rbtree (red-black tree) | 18 ms | 579 ms | **32x** |
103-| splay tree | 10 ms | 210 ms | **21x** |
104-| so_lists | 29 ms | 520 ms | **17x** |
105-| gcbench | 486 ms | 4,425 ms | **9x** |
106-| linked_list | 63 ms | 484 ms | **7.7x** |
107-| binary_trees | 5 ms | 80 ms | **16x** |
104+|---|---|---|---|
105+| rbtree (red-black tree) | 23 ms | 571 ms | **24.8x** |
106+| splay tree | 14 ms | 241 ms | **17.2x** |
107+| binary_trees | 11 ms | 105 ms | **9.5x** |
108+| so_lists | 74 ms | 510 ms | **6.9x** |
109+| linked_list | 101 ms | 455 ms | **4.5x** |
110+| gcbench | 1,634 ms | 4,247 ms | **2.6x** |
108111
109112 ### Real-World Programs
110113
111114 | Benchmark | Spinel | CRuby | Speedup |
112-|-----------|--------|-------|---------|
113-| fasta (DNA seq gen) | 1,260 ms | 7,700 ms | **6.1x** |
114-| io_wordcount | 29 ms | 151 ms | **5.2x** |
115-| json_parse | 104 ms | 453 ms | **4.4x** |
116-| template engine | 227 ms | 983 ms | **4.3x** |
117-| csv_process | 273 ms | 952 ms | **3.5x** |
118-| pidigits (bigint) | 6,300 ms | 11,600 ms | **1.8x** |
115+|---|---|---|---|
116+| bigint_fib (1000 digits) | 2 ms | 66 ms | **33.0x** |
117+| pidigits (bigint) | 2 ms | 65 ms | **32.5x** |
118+| str_concat | 2 ms | 67 ms | **33.5x** |
119+| json_parse | 38 ms | 436 ms | **11.5x** |
120+| template engine | 137 ms | 961 ms | **7.0x** |
121+| io_wordcount | 26 ms | 153 ms | **5.9x** |
122+| csv_process | 229 ms | 936 ms | **4.1x** |
119123
120124 ## Supported Ruby Features
121125
@@ -147,6 +151,10 @@
147151 for O(n) in-place append. `+`, interpolation, `tr`, `ljust`/`rjust`/`center`,
148152 and all standard methods work on both. Character comparisons like
149153 `s[i] == "c"` are optimized to direct char array access (zero allocation).
154+Chained concatenation (`a + b + c + d`) collapses to a single malloc
155+via `sp_str_concat4` / `sp_str_concat_arr` -- N-1 fewer allocations.
156+Loop-local `str.split(sep)` reuses the same `sp_StrArray` across
157+iterations (csv_process: 4 M allocations eliminated).
150158
151159 **Regexp**: Built-in NFA regexp engine (no external dependency).
152160 `=~`, `$1`-`$9`, `match?`, `gsub(/re/, str)`, `sub(/re/, str)`,
@@ -161,24 +169,66 @@
161169 variables via heap-promoted cells.
162170
163171 **Memory**: Mark-and-sweep GC with size-segregated free lists, non-recursive
164-marking, and sticky mark bits. Small classes (<=4 fields, no inheritance,
165-no mutation through parameters) are automatically stack-allocated as
166-**value types**. Programs using only value types emit no GC runtime at all.
172+marking, and sticky mark bits. Small classes (≤8 scalar fields, no
173+inheritance, no mutation through parameters) are automatically
174+stack-allocated as **value types** -- 1M allocations of a 5-field class
175+drop from 85 ms to 2 ms. Programs using only value types emit no GC
176+runtime at all.
177+
178+**Symbols**: Separate `sp_sym` type, distinct from strings (`:a != "a"`).
179+Symbol literals are interned at compile time (`SPS_name` constants);
180+`String#to_sym` uses a dynamic pool only when needed. Symbol-keyed
181+hashes (`{a: 1}`) use a dedicated `sp_SymIntHash` that stores
182+`sp_sym` (integer) keys directly rather than strings -- no strcmp, no
183+dynamic string allocation.
167184
168185 **I/O**: `puts`, `print`, `printf`, `p`, `gets`, `ARGV`, `ENV[]`,
169186 `File.read/write/open` (with blocks), `system()`, backtick.
170187
188+## Optimizations
189+
190+Whole-program type inference drives several compile-time optimizations:
191+
192+- **Value-type promotion**: small immutable classes (≤8 scalar fields)
193+ become C structs on the stack, eliminating GC overhead entirely.
194+- **Constant propagation**: simple literal constants (`N = 100`) are
195+ inlined at use sites instead of going through `cst_N` runtime lookup.
196+- **Loop-invariant length hoisting**: `while i < arr.length` evaluates
197+ `arr.length` once before the loop; `while i < str.length` hoists
198+ `strlen`. Mutation of the receiver inside the body (e.g. `arr.push`)
199+ correctly disables the hoist.
200+- **Method inlining**: short methods (≤3 statements, non-recursive)
201+ get `static inline` so gcc can inline them at call sites.
202+- **String concat chain flattening**: `a + b + c + d` compiles to a
203+ single `sp_str_concat4` / `sp_str_concat_arr` call -- one malloc
204+ instead of N-1 intermediate strings.
205+- **Bigint auto-promotion**: loops with `x = x * y` or fibonacci-style
206+ `c = a + b` self-referential addition auto-promote to bigint.
207+- **Bigint `to_s`**: divide-and-conquer O(n log²n) via mruby-bigint's
208+ `mpz_get_str` instead of naive O(n²).
209+- **Static symbol interning**: `"literal".to_sym` resolves to a
210+ compile-time `SPS_<name>` constant; the runtime dynamic pool is
211+ only emitted when dynamic interning is actually used.
212+- **`strlen` caching in sub_range**: when a string's length is
213+ hoisted, `str[i]` accesses use `sp_str_sub_range_len` to skip the
214+ internal strlen call.
215+- **split reuse**: `fields = line.split(",")` inside a loop reuses
216+ the existing `sp_StrArray` rather than allocating a new one.
217+- **Dead-code elimination**: compiled with `-ffunction-sections
218+ -fdata-sections` and linked with `--gc-sections`; each unused
219+ runtime function is stripped from the final binary.
220+
171221 ## Architecture
172222
173223 ```
174224 spinel One-command wrapper script (POSIX shell)
175225 spinel_parse.c C frontend: libprism → text AST (1,061 lines)
176-spinel_codegen.rb Compiler backend: AST → C code (17,162 lines)
177-lib/sp_runtime.h Runtime library header (455 lines)
178-lib/sp_bigint.c Arbitrary precision integers (5,406 lines)
179-lib/regexp/ Built-in regexp engine (1,759 lines)
180-test/ 65 feature tests
181-benchmark/ 53 benchmarks
226+spinel_codegen.rb Compiler backend: AST → C code (20,851 lines)
227+lib/sp_runtime.h Runtime library header (581 lines)
228+lib/sp_bigint.c Arbitrary precision integers (5,394 lines)
229+lib/regexp/ Built-in regexp engine (1,626 lines)
230+test/ 75 feature tests
231+benchmark/ 54 benchmarks
182232 Makefile Build automation
183233 ```
184234
@@ -206,8 +256,8 @@
206256
207257 ```bash
208258 make # build parser + regexp library + bootstrap compiler
209-make test # run 65 feature tests (requires bootstrap)
210-make bench # run 53 benchmarks (requires bootstrap)
259+make test # run 75 feature tests (requires bootstrap)
260+make bench # run 54 benchmarks (requires bootstrap)
211261 make bootstrap # rebuild compiler from source
212262 sudo make install # install to /usr/local (spinel in PATH)
213263 make clean # remove build artifacts