Commit c01a3892
c01a389239c1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · +1 added
Description
docs: add limitations.md (what an AOT compiler can and cannot do)
A canonical, current catalogue of Spinel's limits, organized by kind: fundamental (inherent to AOT), partial/relaxable, by-design, and now-supported. Corrects community write-ups that described the older Ruby self-hosted compiler (mutable string literals, hash-missing-key nil, define_method, block arity, closures-through-containers, oct/first-on-empty, and implicit-self send all work now). Points at INCOMPATIBILITIES.md for intentional deviations.
Refs #1261.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- c01a389239c1
- Parent
- a3d3929b5c9a
docs/limitations.mdaddedView file
1+
# Spinel limitations: what an AOT compiler can and cannot do2+
3+
Spinel is a whole-program **ahead-of-time** compiler: it reads the entire4+
program, infers a static type for every value, emits C, compiles it, and runs5+
the binary. There is no Ruby interpreter, parser, or type-inference engine in6+
the running program — it is just C. That model is what buys the speedup, and it7+
is also the source of every limitation below.8+
9+
This document is the honest catalogue. It is organized by *kind* of limit:10+
11+
- **Fundamental** — incompatible with whole-program AOT; will not change without12+
abandoning the model (e.g. bundling an interpreter).13+
- **Partial / relaxable** — genuinely limited today, but additively fixable.14+
- **By design** — a deliberate, documented choice (see also15+
[`INCOMPATIBILITIES.md`](INCOMPATIBILITIES.md), the canonical list of16+
intentional CRuby deviations).17+
- **Now supported** — things that are *not* limits (corrects older write-ups18+
that described an earlier version of the compiler).19+
20+
---21+
22+
## Fundamental limits (inherent to AOT)23+
24+
These need a runtime parser, a runtime metaobject protocol, an allocation25+
registry, or stack reification — none of which exist in a flat compiled binary.26+
27+
| Feature | Behaviour | Why it's fundamental |28+
|---|---|---|29+
| `eval` / `instance_eval("str")` / `class_eval("str")` | unsupported | needs a runtime parser + type system. (Block forms — `instance_eval { }` — DO work; the block is compiled.) |30+
| `method_missing` on an arbitrary receiver | unresolved call → stub | every call site is a direct C call; there is no per-receiver dispatch fallback |31+
| `define_method` with a runtime-computed name/body | only literal names work | a runtime-built method has no compiled body |32+
| `ObjectSpace` (`each_object`, `count_objects`) | unsupported | no class-keyed allocation registry; the GC tracks bytes, not a live-object index |33+
| `TracePoint` / `set_trace_func` / `binding` | unsupported | require an interpreter loop and reified local scopes |34+
| Refinements (`refine` / `using`) | no-op / unresolved | scope-keyed dispatch is incompatible with direct C calls |35+
| `callcc` / `Continuation` | unsupported | multi-shot full-stack capture has no flat-C analogue |36+
| `Class.new(parent) { ... }` (runtime class) | unsupported | the class graph is baked at compile time |37+
| General reflection (`instance_variable_get(var)`, `methods`, `instance_variables`) | unsupported | ivars are C struct offsets with no name→offset table; DCE strips method names |38+
| User-defined `#hash` / `#eql?` for hash *keys* | not dispatched (identity probe) | the hash machinery can't call back into a user method per key |39+
| `require` of stdlib `.rb` (e.g. `time`, `set`, `json/pure`) | unsupported | stdlib leans on metaprogramming / C extensions off the AOT path |40+
| `Marshal` of arbitrary objects | unsupported | load needs a runtime class registry keyed by name |41+
| Mixed / non-UTF-8 encodings | UTF-8 / ASCII-8BIT only | one internal representation; transcoding tables are out of scope |42+
| Embedded `NUL` in general binary strings | `char *` boundary assumption | most string ops are NUL-terminated at the C boundary |43+
44+
`send`/`public_send`/`__send__` with a **non-literal** name (`send(meth)`) is in45+
this group: the target can't be resolved statically. A **literal** name is46+
supported — see below.47+
48+
---49+
50+
## Partial / relaxable limits51+
52+
Limited today, but additively fixable; listed roughly easiest-first.53+
54+
| Feature | Today | Path to relax |55+
|---|---|---|56+
| `Exception#backtrace` / `Kernel#caller` | return `[]` (class + message work) | populate frames from a compile-time call-site→source side-table (the `--line-map` map already exists) |57+
| `Thread` real parallelism | `Thread`/`Mutex` are modelled as single-threaded Fibers (`Thread.new{}.value` works, `synchronize` runs inline) | true parallelism needs a concurrent GC and scheduler — large |58+
| `Marshal` of primitives (Int/Float/String/Array/Hash) | unsupported | a fixed wire format — medium; arbitrary objects stay fundamental |59+
| Mixin/inheritance lifecycle hooks (`included` / `inherited` / `extended`) | defined but not fired | emit a startup call with the literal class arg (the include/inherit graph is known at compile time) |60+
| External `Enumerator` (`.each`/`.map` with no block → `.next`/`.peek`/lazy) | unsupported | needs Fiber-style suspension — large. Chained block→`.to_a` forms (`each_slice(n).to_a`, `filter_map`, `map{}.to_a`) already work. |61+
| `Array#hash` (and arrays as hash keys) | unsupported | a builtin is additive, but array *keys* need the fundamental key-dispatch above |62+
63+
---64+
65+
## By design (deliberate choices)66+
67+
- **Integer overflow** — pick one mode at compile time: `raise` (default,68+
`RangeError` on overflow) or `--int-overflow=promote` (auto-bignum). Not both69+
in one binary, because the representation is chosen statically.70+
- **Float `round(ndigits)`** — the value is always correct; the *return class*71+
follows CRuby (Integer for `round` with 0 digits, Float otherwise).72+
- **Frozen literals** — explicit `.freeze` then mutation raises `FrozenError`,73+
matching CRuby. (String literals are *not* implicitly frozen — see below.)74+
75+
See [`INCOMPATIBILITIES.md`](INCOMPATIBILITIES.md) for the full intentional list.76+
77+
---78+
79+
## Now supported (older write-ups are stale here)80+
81+
These were limits in an earlier (Ruby self-hosted) version of the compiler and82+
now work on current master:83+
84+
| Feature | Status |85+
|---|---|86+
| Mutable string literals (`s = "x"; s << "y"`; `"x".frozen?` → `false`) | works |87+
| Hash missing key → `nil` (string- and int-keyed, including `Hash.new(default)`) | works |88+
| `define_method(:name) { ... }` with a literal name | works |89+
| Block-param arity (un-yielded params are `nil`, not a sentinel) | works |90+
| Closures flowing through containers (`{op: ->(a,b){a+b}}[:op].call(2,3)`) | works |91+
| `String#oct` (`0x`/`0b`/`0o` prefixes) and `Array#first` on empty → `nil` | works |92+
| `send(:literal)` / `__send__("literal")` / `public_send(:literal)` on **implicit self** | works (resolved on the AST, so a `send(:` inside a string literal is left untouched) |93+
| Hash variant inference (a wrong initial guess widens to poly transparently) | correct (a perf cost, not a correctness limit) |94+
95+
There is no Ruby self-host "bootstrap fixpoint" constraint: the C compiler is96+
the master implementation.97+
98+
---99+
100+
## Why this still works101+
102+
Most real programs use the dynamic features above sparingly, in setup code, or103+
not at all. Spinel targets the large static core of Ruby — classes, methods,104+
blocks, the collection protocols, exceptions, mixins — and compiles it to fast105+
native code. When a program does need a feature in the *fundamental* table, that106+
program is not a fit for AOT; for everything else, the limits are either by107+
design or on the relaxable list.