Commit a5bb79c4
a5bb79c4a323Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
docs: update README.md to reflect v2 current state
Update benchmarks (39 measured), add mutable strings, GC, C parser, building instructions, and dual-parser architecture description.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- a5bb79c4a323
- Parent
- 7030ec323b35
README.mdmodifiedView file
@@ -2,10 +2,10 @@
22
33
Spinel compiles Ruby source code into standalone native executables.44
It performs whole-program type inference and generates optimized C code,5-
achieving 10x-200x speedup over CRuby.5+
achieving 9x-4,000x speedup over CRuby.66
7-
Spinel is **self-hosting**: the compiler is written in Ruby and compiles8-
itself into a native binary.7+
Spinel is **self-hosting**: the compiler backend is written in Ruby and8+
compiles itself into a native binary.99
1010
## How It Works1111
@@ -13,14 +13,14 @@
1313
Ruby (.rb)1414
|1515
v16-
spinel_parse.rb Parse with Prism, serialize AST17-
|16+
spinel_parse Parse with Prism (libprism), serialize AST17+
| (C binary, or CRuby + Prism gem as fallback)1818
v19-
AST text file (.ast)19+
AST text file2020
|2121
v22-
spinel_codegen.rb Type inference + C code generation23-
| (runs under CRuby or as native binary)22+
spinel_codegen Type inference + C code generation23+
| (self-hosted native binary)2424
v2525
C source (.c)2626
|@@ -34,21 +34,23 @@
3434
## Quick Start3535
3636
```bash37-
# Compile a Ruby program38-
ruby spinel_parse.rb hello.rb > hello.ast39-
ruby spinel_codegen.rb hello.ast hello.c40-
cc -O2 hello.c -lm -o hello41-
./hello37+
# One command (requires spinel wrapper + compiled binaries):38+
./spinel app.rb # compiles to ./app39+
./spinel app.rb -o myapp # compiles to ./myapp40+
./spinel app.rb -c # generates app.c only41+
./spinel app.rb -S # prints C to stdout4242
43-
# Or with the self-hosted binary (46x faster):44-
./spinel_codegen hello.ast hello.c43+
# Or step by step with CRuby:44+
ruby spinel_parse.rb app.rb > app.ast45+
ruby spinel_codegen.rb app.ast app.c46+
cc -O2 app.c -lm -o app4547
```4648
47-
Programs using regular expressions need `-lonig` (oniguruma).49+
Programs using regular expressions need `--lonig` (links oniguruma).4850
4951
## Self-Hosting5052
51-
Spinel compiles itself. The bootstrap chain:53+
Spinel compiles its own backend. The bootstrap chain:5254
5355
```5456
CRuby + spinel_parse.rb → AST@@ -58,23 +60,31 @@
5860
gen2.c == gen3.c (bootstrap loop closed)5961
```6062
61-
bin2 compiles Ruby programs 46x faster than CRuby running spinel_codegen.rb.63+
## Benchmarks6264
63-
## Benchmarks65+
39/39 benchmarks pass. 58/60 tests pass.6466
6567
| Benchmark | Spinel | CRuby 3.x | Speedup |6668
|-----------|--------|-----------|---------|67-
| life (Game of Life) | 37 ms | 1,462 ms | **40x** |68-
| nested_loop | 2 ms | 332 ms | **166x** |69-
| spectralnorm | 15 ms | 1,024 ms | **68x** |70-
| ackermann | 6 ms | 389 ms | **65x** |71-
| mandelbrot | 25 ms | 1,150 ms | **46x** |72-
| fib(34) | 12 ms | 560 ms | **47x** |73-
| nqueens | 710 ms | 24,833 ms | **35x** |74-
| sudoku | 7 ms | 150 ms | **21x** |75-
| splay | 13 ms | 236 ms | **18x** |76-
77-
58/59 tests pass. 39/39 benchmarks pass.69+
| attr_accessor_bench | 0.4 ms | 1,690 ms | **4,225x** |70+
| life (Game of Life) | 0.4 ms | 1,500 ms | **3,750x** |71+
| inline | 0.4 ms | 1,560 ms | **3,900x** |72+
| send_cfunc_block | 0.4 ms | 1,400 ms | **3,500x** |73+
| send_bmethod | 0.5 ms | 600 ms | **1,200x** |74+
| nested_loop | 0.4 ms | 330 ms | **825x** |75+
| keyword_args | 0.4 ms | 290 ms | **725x** |76+
| str_concat | 0.5 ms | 60 ms | **120x** |77+
| ruby_xor | 10 ms | 1,490 ms | **149x** |78+
| ackermann | 4.4 ms | 380 ms | **86x** |79+
| mandelbrot | 20 ms | 1,160 ms | **58x** |80+
| fib(34) | 10 ms | 560 ms | **56x** |81+
| spectralnorm | 20 ms | 1,010 ms | **50x** |82+
| nqueens | 800 ms | 25,050 ms | **31x** |83+
| sudoku | 4.9 ms | 150 ms | **31x** |84+
| so_lists | 20 ms | 530 ms | **26x** |85+
| splay | 10 ms | 240 ms | **24x** |86+
| fizzbuzz (lambda) | 1,630 ms | 32,180 ms | **20x** |87+
| gcbench | 480 ms | 4,320 ms | **9x** |7888
7989
## Supported Ruby Features8090
@@ -92,8 +102,15 @@
92102
**Exceptions**: `begin`/`rescue`/`ensure`/`retry`, `raise`,93103
custom exception classes.94104
95-
**Types**: Integer, Float, String, Array, Hash, Range, Time, StringIO,96-
File, Regexp. Polymorphic values via tagged unions.105+
**Types**: Integer, Float, String (immutable + mutable), Array, Hash,106+
Range, Time, StringIO, File, Regexp. Polymorphic values via tagged unions.107+
108+
**Strings**: `<<` automatically promotes to mutable strings (`sp_String`)109+
for O(n) in-place append. `+`, interpolation, and methods work on both.110+
111+
**Memory**: Mark-and-sweep GC with explicit roots, conservative stack112+
scanning, and adaptive threshold. Arrays track GC-managed memory for113+
accurate collection.97114
98115
**I/O**: `puts`, `print`, `printf`, `p`, `gets`, `ARGV`, `ENV[]`,99116
`File.read/write/open`, `system()`, backtick.@@ -101,19 +118,51 @@
101118
## Architecture102119
103120
```121+
spinel One-command wrapper script (Ruby)104122
spinel_parse.rb CRuby frontend: Prism AST → text format (715 lines)105-
spinel_codegen.rb Compiler backend: AST → C code (14,074 lines)123+
spinel_parse.c C frontend: libprism → text format (915 lines)124+
spinel_codegen.rb Compiler backend: AST → C code (14,280 lines)106125
lib/ Stub libraries (stringio, strscan, optparse, etc.)107-
test/ 59 test programs126+
test/ 60 test programs108127
benchmark/ 39 benchmark programs109128
```110129
111-
The compiler is written in a Ruby subset that Spinel itself can compile:112-
classes, `def`, `attr_accessor`, `if`/`case`/`while`, `each`/`map`/`select`,113-
`yield`, `begin`/`rescue`, String/Array/Hash operations, File I/O.130+
The compiler backend (`spinel_codegen.rb`) is written in a Ruby subset131+
that Spinel itself can compile: classes, `def`, `attr_accessor`,132+
`if`/`case`/`while`, `each`/`map`/`select`, `yield`, `begin`/`rescue`,133+
String/Array/Hash operations, File I/O.114134
115135
No metaprogramming, no `eval`, no `require` in the backend.116136
137+
The parser has two implementations:138+
- **spinel_parse.c** links libprism directly (no CRuby needed)139+
- **spinel_parse.rb** uses the Prism gem (CRuby fallback)140+
141+
Both produce identical AST output. The `spinel` wrapper prefers the142+
C binary if available.143+
144+
## Building145+
146+
```bash147+
# Build libprism (from Prism gem source or standalone)148+
PRISM=~/.gem/ruby/3.2.0/gems/prism-1.9.0149+
cc -O2 -c -I$PRISM/include $PRISM/src/*.c $PRISM/src/util/*.c150+
ar rcs libprism.a *.o151+
152+
# Build the C parser153+
cc -O2 -I$PRISM/include spinel_parse.c libprism.a -lm -o spinel_parse_bin154+
155+
# Bootstrap the compiler backend156+
ruby spinel_parse.rb spinel_codegen.rb > codegen.ast157+
ruby spinel_codegen.rb codegen.ast gen1.c158+
cc -O2 -Wno-all gen1.c -lm -o bin1159+
./bin1 codegen.ast gen2.c160+
cc -O2 -Wno-all gen2.c -lm -o spinel_codegen161+
162+
# Now you can compile Ruby programs:163+
./spinel app.rb164+
```165+
117166
## Limitations118167
119168
- **No eval**: `eval`, `instance_eval`, `class_eval`@@ -123,7 +172,8 @@
123172
124173
## Dependencies125174
126-
- **Parse time**: [Prism](https://github.com/ruby/prism) gem (CRuby)175+
- **Parse time**: [libprism](https://github.com/ruby/prism) (C library),176+
or Prism gem (CRuby) as fallback127177
- **Run time**: None. Generated binaries need only libc + libm.128178
- **Regexp**: Programs using regex link with [oniguruma](https://github.com/kkos/oniguruma)129179