Skip to content

Commit ff56bbf

Browse files
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/codegen.rs‎

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -297,22 +297,11 @@ fn emit_view_struct(out: &mut String, class_name: &str, def: &NativeClassDef, de
297297
writeln!(out, "{inner}pub fn {field_name}(&self) -> f64 {{").unwrap();
298298
writeln!(
299299
out,
300-
"{inner2}match self.instance.load_field({}).as_object_ptr() {{",
300+
"{inner2}match self.instance.load_field({}).as_float() {{",
301301
field.index
302302
)
303303
.unwrap();
304-
writeln!(
305-
out,
306-
"{inner2} Some(ptr) => match unsafe {{ ptr.get() }} {{"
307-
)
308-
.unwrap();
309-
writeln!(out, "{inner2} bex_vm_types::Object::Float(f) => *f,").unwrap();
310-
writeln!(
311-
out,
312-
"{inner2} _ => panic!(\"{class_name}.{raw_name}: expected Float\"),"
313-
)
314-
.unwrap();
315-
writeln!(out, "{inner2} }},").unwrap();
304+
writeln!(out, "{inner2} Some(f) => f,").unwrap();
316305
writeln!(
317306
out,
318307
"{inner2} None => panic!(\"{class_name}.{raw_name}: expected Float\"),"
@@ -486,13 +475,7 @@ fn view_optional_type_and_expr(
486475
BamlType::Float => (
487476
"Option<f64>".to_string(),
488477
format!(
489-
"{{ \
490-
let Some(ptr) = self.instance.load_field({field_index}).as_object_ptr() \
491-
else {{ panic!(\"{class_name}.{field_name}: expected Float\") }}; \
492-
let bex_vm_types::Object::Float(f) = (unsafe {{ ptr.get() }}) \
493-
else {{ panic!(\"{class_name}.{field_name}: expected Float\") }}; \
494-
*f \
495-
}}"
478+
"self.instance.load_field({field_index}).as_float().unwrap_or_else(|| panic!(\"{class_name}.{field_name}: expected Float\"))"
496479
),
497480
),
498481
BamlType::Bool => (
@@ -654,7 +637,7 @@ fn copy_field_to_value(field_name: &str, ty: &BamlType) -> String {
654637
\"failed to allocate bigint field `{field_name}`: {{p}}\"))"
655638
),
656639
BamlType::Float => {
657-
format!("bex_vm_types::Value::object(vm.alloc_float(self.{field_name}))")
640+
format!("vm.float_value(self.{field_name})")
658641
}
659642
BamlType::Bool => format!("bex_vm_types::Value::bool(self.{field_name})"),
660643
BamlType::Null => "bex_vm_types::Value::NULL".to_string(),
@@ -1629,11 +1612,9 @@ fn receiver_immut_extraction_expr(
16291612
"Float" => format!(
16301613
"{{ \
16311614
let __v = {val}; \
1632-
let Some(ptr) = __v.as_object_ptr() \
1633-
else {{ return Err(VmInternalError::TypeError {{ expected: Type::Float, got: vm.type_of(__v) }}.into()); }}; \
1634-
let bex_vm_types::Object::Float(f) = (unsafe {{ ptr.get() }}) \
1615+
let Some(f) = __v.as_float() \
16351616
else {{ return Err(VmInternalError::TypeError {{ expected: Type::Float, got: vm.type_of(__v) }}.into()); }}; \
1636-
*f \
1617+
f \
16371618
}}"
16381619
),
16391620
// Bigint is heap-allocated behind an Arc; always clone the Arc so the
@@ -1689,11 +1670,9 @@ fn extraction_expr(
16891670
BamlType::Float => format!(
16901671
"{{ \
16911672
let __v = {val}; \
1692-
let Some(ptr) = __v.as_object_ptr() \
1693-
else {{ return Err(VmInternalError::TypeError {{ expected: Type::Float, got: vm.type_of(__v) }}.into()); }}; \
1694-
let bex_vm_types::Object::Float(f) = (unsafe {{ ptr.get() }}) \
1673+
let Some(f) = __v.as_float() \
16951674
else {{ return Err(VmInternalError::TypeError {{ expected: Type::Float, got: vm.type_of(__v) }}.into()); }}; \
1696-
*f \
1675+
f \
16971676
}}"
16981677
),
16991678
BamlType::Bool => format!(
@@ -2106,7 +2085,7 @@ fn result_conversion_expr(name: &str, ty: &BamlType, generics: &[String]) -> Str
21062085
}})?"
21072086
),
21082087
BamlType::Bigint => format!("vm.try_alloc_bigint({name})?"),
2109-
BamlType::Float => format!("Value::object(vm.alloc_float({name}))"),
2088+
BamlType::Float => format!("vm.float_value({name})"),
21102089
BamlType::Bool => format!("Value::bool({name})"),
21112090
BamlType::Null => "Value::NULL".to_string(),
21122091
BamlType::List(inner) => {

‎baml_language/crates/baml_builtins2_codegen/src/codegen_io.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ fn owned_rust_type(
291291
fn view_return_type(ty: &BamlType, needs_heap: &mut bool) -> TokenStream {
292292
match ty {
293293
BamlType::Int => quote! { Result<i64, AccessError> },
294-
// Float is heap-boxed (`Object::Float`); the accessor needs a
295-
// `PermitProof` to deref the pointer soundly.
294+
// A float may be heap-boxed (`Object::Float`: NaN, ±inf, extreme
295+
// magnitudes); the accessor needs a `PermitProof` to deref it soundly.
296296
BamlType::Float => {
297297
*needs_heap = true;
298298
quote! { Result<f64, AccessError> }
@@ -499,7 +499,7 @@ fn into_owned_expr(
499499
BamlType::Int | BamlType::Bool => {
500500
quote! { self.#field_ident()? }
501501
}
502-
// Float accessor is now heap-aware (`Object::Float` deref).
502+
// Float accessor is heap-aware (a boxed `Object::Float` derefs).
503503
BamlType::Float => quote! { self.#field_ident(heap, permit)? },
504504
BamlType::String => quote! { self.#field_ident(heap, permit)?.to_string() },
505505
BamlType::RustType => quote! { self.#field_ident(heap, permit)? },

‎baml_language/crates/baml_tests/src/engine.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ pub fn display_user_functions_with_options(program: &Program, show_auto_derive:
139139
/// anything can display a type, and an unbound head renders as its tag
140140
/// (`<unresolved type #…>`).
141141
///
142-
/// Float constants are boxed into the pool first, exactly as the engine's
143-
/// load path does — `resolve_function_constants` (inside the heap build)
144-
/// refuses a raw `ConstValue::Float`.
142+
/// Float constants with no inline `Value` encoding are boxed into the pool
143+
/// first, exactly as the engine's load path does — `resolve_function_constants`
144+
/// (inside the heap build) refuses such a raw `ConstValue::Float`.
145145
pub fn bound_pool(program: &Program) -> bex_heap::BexHeap {
146146
let mut objects = program.objects.0.clone();
147147
for index in 0..objects.len() {
@@ -154,7 +154,11 @@ pub fn bound_pool(program: &Program) -> bex_heap::BexHeap {
154154
.iter()
155155
.enumerate()
156156
.filter_map(|(slot, constant)| match constant {
157-
bex_vm_types::ConstValue::Float(value) => Some((slot, *value)),
157+
bex_vm_types::ConstValue::Float(value)
158+
if bex_vm_types::Value::try_inline_float(*value).is_none() =>
159+
{
160+
Some((slot, *value))
161+
}
158162
_ => None,
159163
})
160164
.collect();

‎baml_language/crates/bex_engine/src/conversion.rs‎

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ impl BexEngine {
386386
ValueKind::Null => BexExternalValue::Null,
387387
ValueKind::Int(i) => BexExternalValue::Int(i),
388388
ValueKind::Bool(b) => BexExternalValue::Bool(b),
389+
ValueKind::Float(f) => BexExternalValue::Float(f),
389390
ValueKind::Object(idx) => {
390391
self.convert_heap_ptr_to_external_with_type(idx, effective_type, vm, permit)?
391392
}
@@ -770,8 +771,9 @@ impl BexEngine {
770771
// SysOp Argument Conversion
771772
// ============================================================================
772773

773-
/// If `ptr` references an `Object::Float`, return its `BexExternalValue::Float`
774-
/// projection so callers can surface a primitive instead of an opaque handle.
774+
/// If `ptr` references a boxed `Object::Float` (NaN, ±inf, extreme
775+
/// magnitudes), return its `BexExternalValue::Float` projection so callers can
776+
/// surface a primitive instead of an opaque handle.
775777
///
776778
/// # Safety
777779
///
@@ -898,6 +900,7 @@ impl BexEngine {
898900
ValueKind::Null => BexExternalValue::Null,
899901
ValueKind::Int(i) => BexExternalValue::Int(i),
900902
ValueKind::Bool(b) => BexExternalValue::Bool(b),
903+
ValueKind::Float(f) => BexExternalValue::Float(f),
901904
ValueKind::Object(ptr) => {
902905
// SAFETY: caller holds the engine permit (heap is borrowed).
903906
if let Some(v) = unsafe { unbox_float_object(ptr) } {
@@ -938,6 +941,7 @@ pub(crate) fn owned_external_value(
938941
ValueKind::Null => BexExternalValue::Null,
939942
ValueKind::Int(i) => BexExternalValue::Int(i),
940943
ValueKind::Bool(b) => BexExternalValue::Bool(b),
944+
ValueKind::Float(f) => BexExternalValue::Float(f),
941945
ValueKind::Object(ptr) => {
942946
// SAFETY: `permit` witnesses GC liveness for the deref.
943947
if let Some(v) = unsafe { unbox_float_object(ptr) } {
@@ -1737,6 +1741,24 @@ fn find_implemented_interface_union_member<'a>(
17371741
}
17381742
}
17391743

1744+
/// The union member a float selects: a float literal it equals, else `float`.
1745+
fn find_float_union_member(
1746+
value: f64,
1747+
members: &[bex_vm_types::RealizedTy],
1748+
) -> Option<&bex_vm_types::RealizedTy> {
1749+
members
1750+
.iter()
1751+
.find(|member| {
1752+
matches!(member, bex_vm_types::RealizedTy::Literal(Literal::Float(expected), _)
1753+
if float_literal_matches(value, expected))
1754+
})
1755+
.or_else(|| {
1756+
members
1757+
.iter()
1758+
.find(|member| matches!(member, bex_vm_types::RealizedTy::Float))
1759+
})
1760+
}
1761+
17401762
/// Find the union member that matches the runtime value's type.
17411763
fn find_matching_union_member(
17421764
value: Value,
@@ -1765,20 +1787,11 @@ fn find_matching_union_member(
17651787
.iter()
17661788
.find(|member| matches!(member, bex_vm_types::RealizedTy::Bool))
17671789
}),
1790+
ValueKind::Float(value) => find_float_union_member(value, members),
17681791
ValueKind::Object(ptr) => {
17691792
let obj = unsafe { ptr.get() };
17701793
match obj {
1771-
Object::Float(value) => members
1772-
.iter()
1773-
.find(|member| {
1774-
matches!(member, bex_vm_types::RealizedTy::Literal(Literal::Float(expected), _)
1775-
if float_literal_matches(*value, expected))
1776-
})
1777-
.or_else(|| {
1778-
members
1779-
.iter()
1780-
.find(|member| matches!(member, bex_vm_types::RealizedTy::Float))
1781-
}),
1794+
Object::Float(value) => find_float_union_member(*value, members),
17821795
Object::String(value) => members
17831796
.iter()
17841797
.find(|member| {

‎baml_language/crates/bex_engine/src/hydrate.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,9 @@ impl<H: HeapPermit<BexThread>> Hydrator<'_, H> {
440440
.alloc_bigint(big.clone()),
441441
)
442442
}
443-
BexExternalValue::Float(float) => self.alloc(Object::Float(*float)),
443+
BexExternalValue::Float(float) => {
444+
self.holder.holder_mut().tlab_mut().float_value(*float)
445+
}
444446
BexExternalValue::Bool(b) => Value::bool(*b),
445447
BexExternalValue::String(s) => {
446448
Value::object(self.holder.holder_mut().tlab_mut().alloc_string(s.clone()))

‎baml_language/crates/bex_engine/src/lib.rs‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,10 +1911,11 @@ impl BexEngine {
19111911
// Extract compile-time objects for the heap
19121912
let mut compile_time_objects: Vec<Object> = bytecode.objects.into_iter().collect();
19131913

1914-
// Box every reachable `ConstValue::Float` into a compile-time
1915-
// `Object::Float` (floats can no longer live inline in `Value`).
1916-
// `bytecode.globals` are rewritten further down, during the
1917-
// `ConstValue` → `Value` conversion, using the returned index map.
1914+
// Box every reachable `ConstValue::Float` that has no inline `Value`
1915+
// encoding (NaN, ±inf, extreme magnitudes) into a compile-time
1916+
// `Object::Float`. `bytecode.globals` are rewritten further down,
1917+
// during the `ConstValue` → `Value` conversion, using the returned
1918+
// index map.
19181919
let float_indices = bex_vm_types::types::box_compile_time_floats(
19191920
&mut compile_time_objects,
19201921
&bytecode.globals,
@@ -2036,13 +2037,14 @@ impl BexEngine {
20362037
pending_globals[slot] = true;
20372038
}
20382039

2039-
// Float globals were redirected to compile-time Object::Float entries
2040-
// by the boxing pre-pass above.
2040+
// Float globals with no inline encoding were redirected to
2041+
// compile-time Object::Float entries by the boxing pre-pass above; the
2042+
// rest resolve inline in `to_value`.
20412043
let globals_vec: Vec<Value> = bytecode
20422044
.globals
20432045
.into_iter()
20442046
.map(|cv| match cv {
2045-
bex_vm_types::ConstValue::Float(f) => {
2047+
bex_vm_types::ConstValue::Float(f) if float_indices.contains_key(&f.to_bits()) => {
20462048
let idx = float_indices[&f.to_bits()];
20472049
Value::object(heap.compile_time_ptr(idx))
20482050
}
@@ -5297,6 +5299,7 @@ impl BexEngine {
52975299
ValueKind::Null => "null".to_string(),
52985300
ValueKind::Bool(value) => value.to_string(),
52995301
ValueKind::Int(value) => value.to_string(),
5302+
ValueKind::Float(value) => value.to_string(),
53005303
ValueKind::OmittedArg => "<omitted argument>".to_string(),
53015304
ValueKind::Object(ptr) => match unsafe { ptr.get() } {
53025305
Object::String(value) => value.to_string(),
@@ -6169,7 +6172,7 @@ impl BexEngine {
61696172
// cancellation it received): per BEP-034 it runs to
61706173
// completion and its value stands, for a root call as
61716174
// for a spawned child.
6172-
// A heap-boxed float is no handle: a function declared
6175+
// A float (inline, or boxed) is no handle: a function declared
61736176
// `-> float` (or `-> float | ...`, `-> float?`) hands the
61746177
// host an inline `BexExternalValue::Float`, through the
61756178
// typed converter so the declared type's shape (a union

‎baml_language/crates/bex_engine/src/trace_heap.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ impl TraceSnapshotBuilder {
314314
ValueKind::Null => self.alloc(TraceValue::Null),
315315
ValueKind::Bool(value) => self.alloc(TraceValue::Bool(value)),
316316
ValueKind::Int(value) => self.alloc(TraceValue::Int(value)),
317+
ValueKind::Float(value) => self.alloc(TraceValue::Float(value)),
317318
ValueKind::OmittedArg => {
318319
self.omitted(TraceOmissionReason::OmittedArgument, "omitted argument")
319320
}

‎baml_language/crates/bex_heap/src/accessor.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ fn owned_inner(
612612
bex_vm_types::ValueKind::Null => Ok(BexExternalValue::Null),
613613
bex_vm_types::ValueKind::Int(i) => Ok(BexExternalValue::Int(i)),
614614
bex_vm_types::ValueKind::Bool(b) => Ok(BexExternalValue::Bool(b)),
615+
bex_vm_types::ValueKind::Float(f) => Ok(BexExternalValue::Float(f)),
615616
bex_vm_types::ValueKind::Object(_) => {
616617
unreachable!("object path handled above")
617618
}

‎baml_language/crates/bex_heap/src/gc.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ mod tests {
10741074
let heap = BexHeap::with_tlab_size(vec![], 1);
10751075
let mut tlab = Tlab::new(heap.clone());
10761076
for _ in 0..ChunkedVec::<Object>::CHUNK_SIZE - 1 {
1077-
tlab.alloc_float(0.0);
1077+
tlab.alloc_string(String::new());
10781078
}
10791079
let mut rooted = None;
10801080
for id in 0..4 {
@@ -2994,7 +2994,7 @@ mod tests {
29942994
}
29952995

29962996
/// The finalizer scan examines the registered instances only: 10,000 dead
2997-
/// floats around them are never visited.
2997+
/// strings around them are never visited.
29982998
#[test]
29992999
fn finalizer_scan_visits_only_finalizable_instances() {
30003000
let heap = BexHeap::new(vec![]);
@@ -3006,7 +3006,7 @@ mod tests {
30063006
let live = tlab.alloc_instance(class, vec![]);
30073007
let dead = tlab.alloc_instance(class, vec![]);
30083008
for i in 0..10_000_u32 {
3009-
tlab.alloc_float(f64::from(i));
3009+
tlab.alloc_string(i.to_string());
30103010
}
30113011
assert_eq!(finalizable_entries(&heap), {
30123012
let mut both = vec![live, dead];

‎baml_language/crates/bex_heap/src/tlab.rs‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ impl Tlab {
163163
/// If the current chunk is exhausted, refill from the heap.
164164
#[inline]
165165
pub fn alloc(&mut self, mut obj: Object) -> HeapPtr {
166+
debug_assert!(
167+
!matches!(obj, Object::Float(f) if Value::try_inline_float(f).is_some()),
168+
"a float with an inline encoding must not be boxed; use float_value"
169+
);
166170
self.charge(&mut obj);
167171

168172
if self.alloc_ptr >= self.alloc_limit {
@@ -267,10 +271,23 @@ impl Tlab {
267271
Ok(unsafe { self.publish(reservation, Object::Function(function)) })
268272
}
269273

270-
/// Allocate a float object.
271-
#[inline]
272-
pub fn alloc_float(&mut self, f: f64) -> HeapPtr {
273-
self.alloc(Object::Float(f))
274+
/// The `Value` of a float: inline when it has an inline encoding,
275+
/// otherwise a fresh `Object::Float` box (NaN, `±inf`, extreme
276+
/// magnitudes). The one way to build a float value, which keeps the
277+
/// representation canonical — see [`Value`].
278+
#[inline(always)]
279+
#[allow(clippy::inline_always)]
280+
pub fn float_value(&mut self, f: f64) -> Value {
281+
match Value::try_inline_float(f) {
282+
Some(v) => v,
283+
None => self.box_float(f),
284+
}
285+
}
286+
287+
#[cold]
288+
#[inline(never)]
289+
fn box_float(&mut self, f: f64) -> Value {
290+
Value::object(self.alloc(Object::Float(f)))
274291
}
275292

276293
/// Allocate a string object.
@@ -499,8 +516,9 @@ pub trait TlabHolder {
499516
self.tlab_mut().alloc(obj)
500517
}
501518

502-
fn alloc_float(&mut self, f: f64) -> HeapPtr {
503-
self.tlab_mut().alloc_float(f)
519+
/// See [`Tlab::float_value`].
520+
fn float_value(&mut self, f: f64) -> Value {
521+
self.tlab_mut().float_value(f)
504522
}
505523

506524
fn alloc_string(&mut self, s: impl Into<bex_str::BexStr>) -> HeapPtr {
@@ -1140,7 +1158,7 @@ mod tests {
11401158
(0..PER_THREAD)
11411159
.map(|i| {
11421160
let key = u32::try_from(thread * PER_THREAD + i).unwrap();
1143-
(key, tlab.alloc_float(f64::from(key)))
1161+
(key, tlab.alloc_string(key.to_string()))
11441162
})
11451163
.collect::<Vec<_>>()
11461164
})
@@ -1162,7 +1180,7 @@ mod tests {
11621180
assert!(gen0.contains_ptr(ptr.as_ptr()));
11631181
// SAFETY: the allocating thread wrote this slot and has joined.
11641182
match unsafe { ptr.get() } {
1165-
Object::Float(value) => assert_eq!(*value, f64::from(key)),
1183+
Object::String(value) => assert_eq!(value.as_str(), key.to_string()),
11661184
other => panic!("slot for {key} holds {other:?}"),
11671185
}
11681186
}

0 commit comments

Comments
 (0)