Repository navigation
Commit ff56bbf
authored
Inline floats in Value: float arithmetic no longer allocates (#5675)
Every float result allocated.
`AddFloat`/`SubFloat`/`MulFloat`/`DivFloat`, `float` natives (`sqrt`,
…), JSON decode and int→float widening all went through `alloc_float`: a
64-byte TLAB slot holding `Object::Float(f64)`, a meter charge and a
finalizer check, and the collector then dropped the dead box. On
`compute::nbody 20k` (2.7x slower than CPython 3.13) the four float
arithmetic arms took 40% of the VM thread's time and GC dropping dead
floats another 9% (profile below).
Floats now live inline in `Value`, "self-tagged" as in *Float
Self-Tagging* (Melançon, Serrano, Feeley, OOPSLA '25): the tag bits of
an inline float are bits of its own exponent, so encoding is an add and
a rotate, and no float arithmetic allocates.
## Encoding
`Value` keeps its existing words: low bit `1` is an i63 int, low 3 bits
`000` (nonzero) a heap pointer, exact `0/2/4/6` null/false/true/omitted.
Floats take the words that were unused:
| low bits | floats | encode |
|---|---|---|
| `10` (not 2, 6) | biased exponent in [768, 1279], i.e. \|x\| in
[2^-255, 2^257) | `(bits + (256 << 52)).rotate_left(3)` |
| `100` (not 4) | biased exponent in [0, 255]: ±0.0, subnormals, \|x\| <
2^-767 | `((bits ^ 1) + (1024 << 52)).rotate_left(4)` |
The offset moves the exponent range so its top bits equal the tag, and
the rotation brings them (with the sign) to the bottom. The `^ 1` keeps
`+0.0` off the `TRUE` word. Everything else stays boxed as
`Object::Float`: NaN, ±inf, \|x\| ≥ 2^257, \|x\| in [2^-767, 2^-255),
and the three floats whose encoding would be exactly a singleton word
(±2^-255 and the smallest positive subnormal).
Properties the code keeps:
- Ints and pointers are encoded exactly as before.
- Lossless: decode(encode(x)) has the same 64 bits for every f64 (sign
of zero, NaN payloads).
- Canonical: one representation per float. Floats are built only through
`Tlab::float_value` / `BexVm::float_value` (inline when possible, box
otherwise; `Tlab::alloc` debug-asserts that no representable float is
boxed), and float constants are boxed at load only when they have no
inline form.
- One accessor: `Value::as_float` reads inline or boxed floats.
`ValueKind::Float` is the inline form, so every exhaustive `kind()`
match had to handle it; a boxed float still reaches those matches as
`ValueKind::Object`.
## What changed
- `bex_vm_types::Value`: `try_inline_float`, `as_inline_float`,
`is_inline_float`, `as_float`, `ValueKind::Float`, a `ValueWire::Float`
variant appended last (old variant indices unchanged).
- `alloc_float(f) -> HeapPtr` is replaced by `float_value(f) -> Value`
on the TLAB and the VM; the free `value_as_float` in `vm.rs` is replaced
by `Value::as_float`.
- `box_compile_time_floats` (engine and test loaders, graft relocation)
boxes only floats with no inline form; `ConstValue::to_value` resolves
the rest inline.
- Every site that read `Object::Float` or classified a value by kind now
handles inline floats too: type tags and `type_of`/`IsType`, truthiness,
`==` (`EqualsDriver`, `array.includes`), `baml.Hash` (`hash_float`
shared by both forms), natural sort, `string.from`, JSON encode/decode
and the serde bridge, CSV, TOML, trace context metadata, telemetry
snapshots, host conversion (`bex_engine` conversion, hydration, trace
heap) and the builtins codegen's float field/argument extraction.
- The Rust backend (`pack --rust`) is unchanged: it already boxed
through `BexVm::float_value` and read through `compiled_rt::expect_f64`,
which now go inline. Generated code is identical.
## Numbers
| workload | canary | this PR | new/canary | spread |
|---|---:|---:|---:|---:|
| compute/nbody-20k | 113.69 ms | 66.06 ms | **0.581x** | 0.57..0.59 |
| compute/mixed-ops-5k | 1.30 ms | 1.30 ms | 0.999x | 0.93..1.01 |
| compute/json-aggregate-2k | 25.96 ms | 25.62 ms | 0.987x | 0.96..1.02
|
| compute/json-request-body | 54.26 µs | 55.19 µs | 1.017x | 0.97..1.04
|
| compute/json-tree-path | 3.06 ms | 3.12 ms | 1.021x | 0.98..1.04 |
| *sentinels* | | | | |
| compute/array-build-sum-100k | 10.56 ms | 10.49 ms | 0.994x |
0.95..1.03 |
| compute/closure-apply-1m | 160.60 ms | 156.63 ms | 0.975x | 0.95..1.02
|
| compute/fib-iterative-100k | 132.30 ms | 131.62 ms | 0.995x |
0.99..1.00 |
| compute/k-nucleotide-25k | 80.33 ms | 81.27 ms | 1.012x | 0.99..1.04 |
| compute/pure-call-1m | 34.54 ms | 34.09 ms | 0.987x | 0.98..1.00 |
| interfaces/default-method-100k | 35.76 ms | 35.21 ms | 0.985x |
0.96..1.00 |
| interfaces/enum-equals-100k | 23.11 ms | 22.71 ms | 0.983x |
0.94..1.05 |
| interfaces/iterator-pipeline-30k | 21.31 ms | 21.11 ms | 0.991x |
0.96..1.03 |
| memory/binary-trees-14 | 578.68 ms | 572.34 ms | 0.989x | 0.95..1.02 |
| programs/agent-loop-300 | 26.66 ms | 26.93 ms | 1.010x | 0.97..1.10 |
| programs/json-parser-15 | 140.50 ms | 138.71 ms | 0.987x | 0.95..1.01
|
| programs/richards-50 | 212.01 ms | 214.98 ms | 1.014x | 1.00..1.02 |
| string/split-short-literal-100k | 22.95 ms | 23.14 ms | 1.008x |
0.98..1.03 |
Geomean 0.968x over these 18 workloads. M5 Max, release builds,
`speedtest` vm-off rows, canary (ac8b525) and this PR interleaved
over 5 rounds under the machine's exclusive benchmark lock (foreign load
averaged 0.01 cores). Spread is the min..max of the per-round ratio.
nbody won every round. richards (no floats) lost every round of this run
by 1.4%; a second 5-round run of it alone gave 0.990x, so that is layout
noise. The JSON workloads parse and print few floats per job, so they
stay flat.
Profile of nbody (samply, ~14k samples each, time under each
dispatch-loop arm):
| | canary | this PR |
|---|---:|---:|
| `MulFloat` | 20.5% | 7.4% |
| `AddFloat` | 9.4% | 5.5% |
| `SubFloat` | 8.6% | 3.1% |
| `DivFloat` | 1.3% | 0.4% |
| GC | 8.6% | — |
On canary `Tlab::alloc`, `Object::measure`, `Tlab::charge` and the drop
of dead `Object`s were among the top leaf functions; none of them appear
now. What is left in the float arms is the stack pops/push plus about 3%
in `try_inline_float` / `as_inline_float`.
## Risks / what to review
- A site that classifies a value with `as_object_ptr()` and a non-float
fallback would silently mistype an inline float. I went through every
`Object::Float` match, every `ValueKind` match and every "int / bool /
null / else object" classifier in `bex_vm`, `bex_engine`, `bex_heap`;
the list is in the diff. Reviewers who know other such helpers: please
point at them.
- `Value`'s derived `PartialEq`/`Hash` is bit equality, so two inline
`0.0` values are now `==` as `Value`s (before, two boxes never were),
and `-0.0`/`0.0` differ. User-visible `==` never used that alone: the
`EqualsDriver` identity short-circuit is still reflexive-correct, and
the float arms compare by value.
- Deep copy of a boxed float still makes a new box (NaN/inf only).
## Testing
- `bex_vm_types`: property tests over 4M random bit patterns (uniform
and aimed at each exponent range), every exponent range edge, ±0.0,
±2^-255, ±2^257, smallest/largest subnormal, ±inf and several NaN
payloads: the inline/boxed decision follows the documented rule, inline
round trips are bit-identical, never collide with
ints/pointers/singletons, and every word in the float tag space
re-encodes to itself (canonical). Borsh round trip of inline floats.
- Updated tests: `ops_comparison` float equality (inline numbers, boxed
NaNs), telemetry snapshot captures an inline and a boxed float alike,
heap tests that used floats as filler objects now use strings.
- `cargo nextest run --all-features` (CI filter) and `cargo test -p
baml_tests` pass; no bytecode snapshot changed. nbody's printed result
is unchanged.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **New Features**
* Floating-point values are now handled directly across parsing,
serialization, comparisons, arrays, and runtime operations.
* Common floats use a compact representation; values that cannot use it
remain supported through boxed storage.
* Float values are now included in snapshots and trace output.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 576453c commit ff56bbf
37 files changed
Lines changed: 679 additions & 260 deletions
File tree
- baml_language/crates
- baml_builtins2_codegen/src
- baml_tests/src
- bex_engine/src
- bex_heap/src
- bex_vm_types/src
- types
- bex_vm
- src
- package_baml
- json
- package_reflect
- telemetry
- vm/compiled_rt
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
297 | 297 | | |
298 | 298 | | |
299 | 299 | | |
300 | | - | |
| 300 | + | |
301 | 301 | | |
302 | 302 | | |
303 | 303 | | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
| 304 | + | |
316 | 305 | | |
317 | 306 | | |
318 | 307 | | |
| |||
486 | 475 | | |
487 | 476 | | |
488 | 477 | | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
| 478 | + | |
496 | 479 | | |
497 | 480 | | |
498 | 481 | | |
| |||
654 | 637 | | |
655 | 638 | | |
656 | 639 | | |
657 | | - | |
| 640 | + | |
658 | 641 | | |
659 | 642 | | |
660 | 643 | | |
| |||
1629 | 1612 | | |
1630 | 1613 | | |
1631 | 1614 | | |
1632 | | - | |
1633 | | - | |
1634 | | - | |
| 1615 | + | |
1635 | 1616 | | |
1636 | | - | |
| 1617 | + | |
1637 | 1618 | | |
1638 | 1619 | | |
1639 | 1620 | | |
| |||
1689 | 1670 | | |
1690 | 1671 | | |
1691 | 1672 | | |
1692 | | - | |
1693 | | - | |
1694 | | - | |
| 1673 | + | |
1695 | 1674 | | |
1696 | | - | |
| 1675 | + | |
1697 | 1676 | | |
1698 | 1677 | | |
1699 | 1678 | | |
| |||
2106 | 2085 | | |
2107 | 2086 | | |
2108 | 2087 | | |
2109 | | - | |
| 2088 | + | |
2110 | 2089 | | |
2111 | 2090 | | |
2112 | 2091 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
291 | 291 | | |
292 | 292 | | |
293 | 293 | | |
294 | | - | |
295 | | - | |
| 294 | + | |
| 295 | + | |
296 | 296 | | |
297 | 297 | | |
298 | 298 | | |
| |||
499 | 499 | | |
500 | 500 | | |
501 | 501 | | |
502 | | - | |
| 502 | + | |
503 | 503 | | |
504 | 504 | | |
505 | 505 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
142 | | - | |
143 | | - | |
144 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
157 | | - | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
158 | 162 | | |
159 | 163 | | |
160 | 164 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
386 | 386 | | |
387 | 387 | | |
388 | 388 | | |
| 389 | + | |
389 | 390 | | |
390 | 391 | | |
391 | 392 | | |
| |||
770 | 771 | | |
771 | 772 | | |
772 | 773 | | |
773 | | - | |
774 | | - | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
775 | 777 | | |
776 | 778 | | |
777 | 779 | | |
| |||
898 | 900 | | |
899 | 901 | | |
900 | 902 | | |
| 903 | + | |
901 | 904 | | |
902 | 905 | | |
903 | 906 | | |
| |||
938 | 941 | | |
939 | 942 | | |
940 | 943 | | |
| 944 | + | |
941 | 945 | | |
942 | 946 | | |
943 | 947 | | |
| |||
1737 | 1741 | | |
1738 | 1742 | | |
1739 | 1743 | | |
| 1744 | + | |
| 1745 | + | |
| 1746 | + | |
| 1747 | + | |
| 1748 | + | |
| 1749 | + | |
| 1750 | + | |
| 1751 | + | |
| 1752 | + | |
| 1753 | + | |
| 1754 | + | |
| 1755 | + | |
| 1756 | + | |
| 1757 | + | |
| 1758 | + | |
| 1759 | + | |
| 1760 | + | |
| 1761 | + | |
1740 | 1762 | | |
1741 | 1763 | | |
1742 | 1764 | | |
| |||
1765 | 1787 | | |
1766 | 1788 | | |
1767 | 1789 | | |
| 1790 | + | |
1768 | 1791 | | |
1769 | 1792 | | |
1770 | 1793 | | |
1771 | | - | |
1772 | | - | |
1773 | | - | |
1774 | | - | |
1775 | | - | |
1776 | | - | |
1777 | | - | |
1778 | | - | |
1779 | | - | |
1780 | | - | |
1781 | | - | |
| 1794 | + | |
1782 | 1795 | | |
1783 | 1796 | | |
1784 | 1797 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
440 | 440 | | |
441 | 441 | | |
442 | 442 | | |
443 | | - | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
444 | 446 | | |
445 | 447 | | |
446 | 448 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1911 | 1911 | | |
1912 | 1912 | | |
1913 | 1913 | | |
1914 | | - | |
1915 | | - | |
1916 | | - | |
1917 | | - | |
| 1914 | + | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
| 1918 | + | |
1918 | 1919 | | |
1919 | 1920 | | |
1920 | 1921 | | |
| |||
2036 | 2037 | | |
2037 | 2038 | | |
2038 | 2039 | | |
2039 | | - | |
2040 | | - | |
| 2040 | + | |
| 2041 | + | |
| 2042 | + | |
2041 | 2043 | | |
2042 | 2044 | | |
2043 | 2045 | | |
2044 | 2046 | | |
2045 | | - | |
| 2047 | + | |
2046 | 2048 | | |
2047 | 2049 | | |
2048 | 2050 | | |
| |||
5297 | 5299 | | |
5298 | 5300 | | |
5299 | 5301 | | |
| 5302 | + | |
5300 | 5303 | | |
5301 | 5304 | | |
5302 | 5305 | | |
| |||
6169 | 6172 | | |
6170 | 6173 | | |
6171 | 6174 | | |
6172 | | - | |
| 6175 | + | |
6173 | 6176 | | |
6174 | 6177 | | |
6175 | 6178 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
314 | 314 | | |
315 | 315 | | |
316 | 316 | | |
| 317 | + | |
317 | 318 | | |
318 | 319 | | |
319 | 320 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
612 | 612 | | |
613 | 613 | | |
614 | 614 | | |
| 615 | + | |
615 | 616 | | |
616 | 617 | | |
617 | 618 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1074 | 1074 | | |
1075 | 1075 | | |
1076 | 1076 | | |
1077 | | - | |
| 1077 | + | |
1078 | 1078 | | |
1079 | 1079 | | |
1080 | 1080 | | |
| |||
2994 | 2994 | | |
2995 | 2995 | | |
2996 | 2996 | | |
2997 | | - | |
| 2997 | + | |
2998 | 2998 | | |
2999 | 2999 | | |
3000 | 3000 | | |
| |||
3006 | 3006 | | |
3007 | 3007 | | |
3008 | 3008 | | |
3009 | | - | |
| 3009 | + | |
3010 | 3010 | | |
3011 | 3011 | | |
3012 | 3012 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
166 | 170 | | |
167 | 171 | | |
168 | 172 | | |
| |||
267 | 271 | | |
268 | 272 | | |
269 | 273 | | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
274 | 291 | | |
275 | 292 | | |
276 | 293 | | |
| |||
499 | 516 | | |
500 | 517 | | |
501 | 518 | | |
502 | | - | |
503 | | - | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
504 | 522 | | |
505 | 523 | | |
506 | 524 | | |
| |||
1140 | 1158 | | |
1141 | 1159 | | |
1142 | 1160 | | |
1143 | | - | |
| 1161 | + | |
1144 | 1162 | | |
1145 | 1163 | | |
1146 | 1164 | | |
| |||
1162 | 1180 | | |
1163 | 1181 | | |
1164 | 1182 | | |
1165 | | - | |
| 1183 | + | |
1166 | 1184 | | |
1167 | 1185 | | |
1168 | 1186 | | |
| |||
0 commit comments