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.
61 lines
2.4 KiB
Rust
61 lines
2.4 KiB
Rust
//! `Cell` — a type-erased 64-bit word holding one scalar value with its kind
|
|
//! stripped away (C7). The type lives in the schema / column / port, never in
|
|
//! the value, so a cell is read back only by naming its type via a typed
|
|
//! accessor. `Scalar` is built on top of this; `Cell` itself knows nothing of
|
|
//! `Scalar` or `ScalarKind`.
|
|
|
|
use crate::scalar::Timestamp;
|
|
|
|
/// A type-erased 64-bit cell: the raw storage of one scalar value with its kind
|
|
/// stripped away. The bits are meaningless on their own — the type lives in the
|
|
/// schema/column/port, not in the value (C7), so the word is recovered **only**
|
|
/// by naming the type at the call site via a typed accessor ([`Cell::i64`],
|
|
/// [`Cell::f64`], [`Cell::bool`], [`Cell::ts`]). Each reinterprets the word with
|
|
/// no tag to check and therefore no branch — the caller's choice of accessor
|
|
/// *is* the type context the hot path already holds. The inner word is private;
|
|
/// there is no kind-free way to read it.
|
|
///
|
|
/// All four base types fit one 64-bit word: `i64`/`Timestamp`/`bool` reuse the
|
|
/// same integer word, `f64` via its IEEE-754 bit pattern. `Eq`/`Hash` are
|
|
/// bit-exact (so `+0.0`/`-0.0` differ and a `NaN` bit pattern equals itself) —
|
|
/// the semantics of a raw word, not of a number.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
pub struct Cell(u64);
|
|
|
|
impl Cell {
|
|
/// Store an `i64` (identity bit-cast).
|
|
pub fn from_i64(v: i64) -> Self {
|
|
Cell(v as u64)
|
|
}
|
|
/// Store an `f64` as its IEEE-754 bit pattern.
|
|
pub fn from_f64(v: f64) -> Self {
|
|
Cell(v.to_bits())
|
|
}
|
|
/// Store a `bool` as `0`/`1`.
|
|
pub fn from_bool(v: bool) -> Self {
|
|
Cell(v as u64)
|
|
}
|
|
/// Store a [`Timestamp`] (its `i64` epoch-ns, bit-cast).
|
|
pub fn from_ts(v: Timestamp) -> Self {
|
|
Cell(v.0 as u64)
|
|
}
|
|
|
|
/// Read the word as `i64` (identity bit-cast). The caller asserts the type
|
|
/// by choosing this accessor; there is no tag to check, hence no branch.
|
|
pub fn i64(self) -> i64 {
|
|
self.0 as i64
|
|
}
|
|
/// Read the word as `f64` from its IEEE-754 bit pattern. Branch-free.
|
|
pub fn f64(self) -> f64 {
|
|
f64::from_bits(self.0)
|
|
}
|
|
/// Read the word as `bool` (any non-zero word is `true`). Branch-free.
|
|
pub fn bool(self) -> bool {
|
|
self.0 != 0
|
|
}
|
|
/// Read the word as a [`Timestamp`] (identity bit-cast). Branch-free.
|
|
pub fn ts(self) -> Timestamp {
|
|
Timestamp(self.0 as i64)
|
|
}
|
|
}
|