cd3d1ca9ed
Motivation
----------
`Scalar` was a tagged enum (I64/F64/Bool/Ts), so every scalar value
physically carried its own kind tag. But the kind is already known from
the schema/port/column the value flows through (C7: the type is a
property of the column, not of the value — the hot path is already
columnar `Column<T>`, and `AnyColumn::get` *reconstructs* the tag from
the column on the way out). The per-value tag was therefore redundant
with the kind the surrounding context already holds.
That redundancy had three costs:
* It baked an implicit `match` (a branch) into every function that read
a Scalar payload — even where the caller statically knew the type.
The tag could never be exploited away.
* Size: a tagged enum is tag + payload = 16 bytes (f64/i64 alignment),
twice the 8 bytes the value needs. A `Column<Scalar>` would be double
the memory and half the cache utilisation.
* It is the shared root of several downstream papercuts we keep hitting
— the lossy f64 manifest field, the `unreachable!` panic on a
non-numeric param, the serde-tag question — all symptoms of "the type
is baked into the value".
Change
------
Introduce `Cell`: a type-erased 64-bit word (`struct Cell(u64)`) that is
not readable without external type context. It is constructed per base
type (`from_i64/from_f64/from_bool/from_ts`) and read only by naming the
type at the call site (`i64()/f64()/bool()/ts()`) — each a branch-free
bit-cast. The hot path resolves the kind once at the boundary (from the
schema) and then reads natively, with no per-value branch. `Cell` knows
nothing of `Scalar` or `ScalarKind`; the dependency is strictly one-way,
and it lives in its own `cell.rs` (more is planned on top of it).
`Scalar` becomes `struct { kind: ScalarKind, cell: Cell }` — the
self-describing form for the dynamic boundaries (builder binding,
serialization, rendering), built on top of `Cell`. Its `as_*` accessors
now `debug_assert` the kind and return the native value (free in
release); calling the wrong accessor is a caller bug, not a checked
`Option`. The variant constructors `Scalar::I64(..)` become associated
fns `Scalar::i64(..)`.
`PartialEq` is hand-written (not derived) to preserve the former enum's
value semantics: kinds must match, then native payloads compare, so f64
keeps IEEE-754 behaviour (`NaN != NaN`, `+0.0 == -0.0`) and a kind
mismatch is never equal even when the raw words coincide. A fixture
(`scalar_eq_is_value_not_bitwise`) pins exactly the cases where bit- and
value-equality diverge, so it can't silently regress. `Cell`'s own
`Eq`/`Hash` stay bitwise — correct for a raw word.
The change is behaviour-preserving: Scalar's observable behaviour is
identical to the pre-Cell enum (the value-equality fixture proves it);
only the internal representation changed. The ~440 call sites across the
workspace are a mechanical constructor rename plus ~12 destructuring
sites (match-arms / `let`-patterns) rewritten to `kind()` + `as_*`.
Verified: cargo build --workspace --all-targets, cargo clippy --workspace
--all-targets -- -D warnings, cargo test --workspace — all green.
117 lines
4.0 KiB
Rust
117 lines
4.0 KiB
Rust
//! The evaluation context (C8): the read-side window access a node sees in
|
|
//! `eval`. The engine sizes and types each input from the node's `schema` at
|
|
//! wiring, so the typed accessors below treat a kind mismatch as an engine bug
|
|
//! (panic), not a user-facing error.
|
|
|
|
use crate::{AnyColumn, Timestamp, Window};
|
|
|
|
/// Read-only, zero-copy view of a node's inputs for one `eval`, in schema
|
|
/// order, plus the cycle's timestamp (C4). `Copy` because it is just a borrow of
|
|
/// the input slice plus a `Copy` timestamp.
|
|
#[derive(Clone, Copy)]
|
|
pub struct Ctx<'a> {
|
|
inputs: &'a [AnyColumn],
|
|
now: Timestamp,
|
|
}
|
|
|
|
impl<'a> Ctx<'a> {
|
|
/// Wrap the per-input columns (in schema-declared order) and the cycle
|
|
/// timestamp for one `eval`.
|
|
pub fn new(inputs: &'a [AnyColumn], now: Timestamp) -> Self {
|
|
Self { inputs, now }
|
|
}
|
|
|
|
/// The current cycle's timestamp (C4). Causal — the present cycle's
|
|
/// timestamp, never the future (C2) — so reading it introduces no look-ahead.
|
|
pub fn now(&self) -> Timestamp {
|
|
self.now
|
|
}
|
|
|
|
/// Zero-copy `f64` window into input `i` (index 0 = newest). Panics if input
|
|
/// `i` is not an `f64` edge — a wiring bug, never reachable from a correctly
|
|
/// wired graph.
|
|
pub fn f64_in(&self, i: usize) -> Window<'a, f64> {
|
|
let inputs: &'a [AnyColumn] = self.inputs;
|
|
inputs[i]
|
|
.as_f64()
|
|
.expect("input kind mismatch (checked at wiring) — engine bug")
|
|
.window()
|
|
}
|
|
|
|
/// Zero-copy `i64` window into input `i` (index 0 = newest). See `f64_in`.
|
|
pub fn i64_in(&self, i: usize) -> Window<'a, i64> {
|
|
let inputs: &'a [AnyColumn] = self.inputs;
|
|
inputs[i]
|
|
.as_i64()
|
|
.expect("input kind mismatch (checked at wiring) — engine bug")
|
|
.window()
|
|
}
|
|
|
|
/// Zero-copy `bool` window into input `i` (index 0 = newest). See `f64_in`.
|
|
pub fn bool_in(&self, i: usize) -> Window<'a, bool> {
|
|
let inputs: &'a [AnyColumn] = self.inputs;
|
|
inputs[i]
|
|
.as_bool()
|
|
.expect("input kind mismatch (checked at wiring) — engine bug")
|
|
.window()
|
|
}
|
|
|
|
/// Zero-copy `timestamp` window into input `i` (index 0 = newest). See
|
|
/// `f64_in`.
|
|
pub fn ts_in(&self, i: usize) -> Window<'a, Timestamp> {
|
|
let inputs: &'a [AnyColumn] = self.inputs;
|
|
inputs[i]
|
|
.as_ts()
|
|
.expect("input kind mismatch (checked at wiring) — engine bug")
|
|
.window()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::{Scalar, ScalarKind, Timestamp};
|
|
|
|
#[test]
|
|
fn ctx_hands_financial_indexed_windows() {
|
|
let mut inputs = vec![AnyColumn::with_capacity(ScalarKind::F64, 4)];
|
|
for v in [10.0_f64, 20.0, 30.0] {
|
|
inputs[0].push(Scalar::f64(v)).unwrap();
|
|
}
|
|
let ctx = Ctx::new(&inputs, Timestamp(0));
|
|
let w = ctx.f64_in(0);
|
|
assert_eq!(w.len(), 3);
|
|
assert_eq!(w[0], 30.0); // newest
|
|
assert_eq!(w[2], 10.0); // oldest
|
|
}
|
|
|
|
#[test]
|
|
fn ctx_addresses_multiple_inputs() {
|
|
let mut inputs = vec![
|
|
AnyColumn::with_capacity(ScalarKind::F64, 2),
|
|
AnyColumn::with_capacity(ScalarKind::I64, 2),
|
|
];
|
|
inputs[0].push(Scalar::f64(1.5)).unwrap();
|
|
inputs[1].push(Scalar::i64(42)).unwrap();
|
|
let ctx = Ctx::new(&inputs, Timestamp(0));
|
|
assert_eq!(ctx.f64_in(0)[0], 1.5);
|
|
assert_eq!(ctx.i64_in(1)[0], 42);
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic(expected = "engine bug")]
|
|
fn ctx_panics_on_kind_mismatch() {
|
|
let mut inputs = vec![AnyColumn::with_capacity(ScalarKind::I64, 2)];
|
|
inputs[0].push(Scalar::i64(7)).unwrap();
|
|
let ctx = Ctx::new(&inputs, Timestamp(0));
|
|
let _ = ctx.f64_in(0); // wrong kind → panic
|
|
}
|
|
|
|
#[test]
|
|
fn ctx_now_returns_cycle_timestamp() {
|
|
let inputs: Vec<AnyColumn> = vec![];
|
|
let ctx = Ctx::new(&inputs, Timestamp(42));
|
|
assert_eq!(ctx.now(), Timestamp(42));
|
|
}
|
|
}
|