Stof 0.13: 2.4x Faster, Half the Memory
Stof 0.13 is the performance release. Every benchmark we track is between 2.1x and 3x faster than 0.12, 2.4x overall, and collections take about half the memory. On real code, a Limitr pricing policy deciding whether a customer can make a call, it's 1.9x faster.
It also settles how values behave: assigning gives a variable a new value, x[k] indexes by key, and references work both ways. Those are breaking changes, covered below.
npm i @formata/stof@latest # TypeScript / JavaScript
pip install -U stof # Python
cargo add stof # Rust
The numbers
Each benchmark is a small Stof program and the same program in Lua 5.4, run in release builds on the same Linux machine. Times are the median of five runs, in milliseconds.
| Benchmark | Stof 0.12 | Stof 0.13 | Faster | vs. Lua (0.12 → 0.13) |
|---|---|---|---|---|
| fib (recursive calls) | 1,312 | 441 | 3.0x | 265x → 89x |
| loop (arithmetic) | 2,945 | 1,064 | 2.8x | 289x → 104x |
| sort | 1,280 | 508 | 2.5x | 16x → 7x |
| closures | 499 | 213 | 2.3x | 134x → 57x |
| lists | 8,570 | 3,683 | 2.3x | 192x → 83x |
| methods | 5,957 | 2,571 | 2.3x | 161x → 69x |
| maps | 2,101 | 956 | 2.2x | 9x → 4x |
| fields | 3,117 | 1,482 | 2.1x | 237x → 113x |
| strings | 902 | 430 | 2.1x | 11x → 5x |
| All (geometric mean) | 2.4x | |||
| Limitr policy, 2,000 decisions | 2,544 | 1,330 | 1.9x |
Memory dropped too. These are peak memory for the same runs:
| Benchmark | Stof 0.12 | Stof 0.13 |
|---|---|---|
| fib | 702 MB | 13 MB |
| closures | 266 MB | 75 MB |
| lists | 159 MB | 62 MB |
| a million-item list and a 200,000-entry map | 227 MB | 103 MB |
| sort | 42 MB | 23 MB |
| strings | 65 MB | 36 MB |
fib used to keep every finished call's frame around until the run ended. Now a call's memory goes away when it returns.
What changed
None of this came from caching things that can change. A Stof document can parse new functions into itself, replace them, or move them while it runs, so every call still finds what it calls as the document is right now. The speed came from doing less work to get there:
- Code runs in frames. A function's body is laid out once and shared by every call, instead of being copied onto a stack each time it runs.
- Smaller values. A value went from 96 bytes to 40, and a plain value is held in place instead of in its own locked, allocated cell.
- Native calls. Calling a Stof function binds its arguments directly, without building a list of instructions to do it.
- Library calls (
list.len(),Num.abs(x)) reuse what the call site already worked out about its arguments. - Loops are built once. A loop used to rebuild its code every time it started. Now it keeps its starting depths on the stack, so its code never changes and is laid out once with the function.
- Collections hold their items in place. A number in a list was a separate allocation of about 80 bytes. Now it's 40 bytes inside the list.
- Paths are walked once.
self.customers.get(id)used to walk its path up to six times to find what to call. Now it walks it once and checks each possibility, in the same order, against what it found.
Why not Lua?
Lua is the usual comparison for an embedded language, and it's still far faster at raw interpretation: about 60 to 110 times on the call-heavy and loop-heavy benchmarks above. Where the work happens in library code (maps, sorting, strings), Stof is now within 4 to 7 times.
The gap is mostly by design. Lua compiles a function to compact bytecode, with local variables in numbered slots, and nothing outside the function can change it while it runs. A Stof document is different:
- It's a live graph. Fields and functions live on objects in a document, and the document can change itself while it runs: parse new code into itself, replace a function, move an object. So every name is resolved when it's used.
- It's sandboxed per call. Seals, time limits, and memory limits are checked as code runs, so a document someone else wrote can only touch what you hand it.
- It's data first. The same document is the config, the logic, and the export: it round-trips to JSON, YAML, TOML, and binary, with types and units (
5km + 300m) along the way. - It runs as cooperative processes. Async calls and loops take turns on one thread, in every host, including the browser.
Stof isn't trying to be the fastest way to run a loop. It's for logic that arrives as data, from a model or a customer, and has to run safely inside your app. For that, the question is whether it's fast enough to sit in the request path, and 0.13 moves it a long way in that direction. A Limitr decision now takes under a millisecond.
There's more to do. Compiling function bodies to a compact bytecode, while keeping lookups dynamic, would close more of the gap with Lua, and that's on the list.
Values behave the way you expect
0.13 changes how values behave, so these are breaking changes:
- Assigning gives a new value. After
let a = [1]; const b = a; a = [9];,bis still[1]. Declaring a variable or passing an argument shares a list, sopush_backshows wherever it's held, but assigning one (to a variable, a field, or an item) takes a copy. - Indexing is by key.
x[k]isx.get(k), andx[k] = visx.set(k, v): a map by key (prices['pro']), an object by field name, and a list, string, or tuple by position. Aforloop still useslenandat, so an object can define its own indexing and its own looping separately. For an entry by position, usem.at(0). - References work both ways.
let r = &self.count;is the field itself: assigningrsets the field, and setting the field changes whatrreads. The same goes for items:&list[0]is that place in the list, even afterlist[0] = 5.
prices: { pro: 20 }
#[main]
fn main() {
let pro = &self.prices.pro;
self.prices.pro = 25;
pln(pro); // 25: a reference reads the field
const plans = {'pro': 20, 'team': 50};
plans['pro'] = 25; // by key
pln(plans['pro']); // 25
}
See Variables & References and Indexing.
Also in this release
- Names that start with a keyword (
looped,returnValue,asyncTasks) parse as names. List.set,Map.set, andgeton every collection, forx[k] = vandx[k].- Documents compiled to binary (BSTF) by 0.12 load and run the same.
stof-cli 0.16.0 is built on this release. The changelog has every detail. If you're running logic someone else wrote, I'd love to hear how 0.13 does for you: find us on Discord or GitHub.
