Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/sats/src/bsatn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ codec_funcs!(val: crate::SumValue);
/// Types that can be encoded to BSATN.
///
/// Implementations of this trait may be more efficient than directly calling [`bsatn::to_vec`].
/// In particular, for [`RowRef`], this method will use a [`StaticBsatnLayout`] if one is available,
/// In particular, for [`RowRef`], this method will use a [`StaticLayout`] if one is available,
/// avoiding expensive runtime type dispatch.
pub trait ToBsatn {
/// BSATN-encode the row referred to by `self` into a freshly-allocated `Vec<u8>`.
Expand All @@ -120,7 +120,7 @@ pub trait ToBsatn {

/// Returns the static size of the type of this object.
///
/// When this returns `Some(_)` there is also a `StaticBsatnLayout`.
/// When this returns `Some(_)` there is also a `StaticLayout`.
fn static_bsatn_size(&self) -> Option<u16>;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/table/benches/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use spacetimedb_sats::algebraic_value::ser::ValueSerializer;
use spacetimedb_sats::{product, AlgebraicType, AlgebraicValue, ArrayValue, ProductType, ProductValue};
use spacetimedb_table::bflatn_from::serialize_row_from_page;
use spacetimedb_table::bflatn_to::write_row_to_page;
use spacetimedb_table::bflatn_to_bsatn_fast_path::StaticBsatnLayout;
use spacetimedb_table::blob_store::NullBlobStore;
use spacetimedb_table::eq::eq_row_in_page;
use spacetimedb_table::indexes::{Byte, Bytes, PageOffset, RowHash};
use spacetimedb_table::layout::{row_size_for_type, RowTypeLayout};
use spacetimedb_table::page::Page;
use spacetimedb_table::row_hash::hash_row_in_page;
use spacetimedb_table::row_type_visitor::{row_type_visitor, VarLenVisitorProgram};
use spacetimedb_table::static_layout::StaticLayout;
use spacetimedb_table::var_len::{AlignedVarLenOffsets, NullVarLenVisitor, VarLenGranule, VarLenMembers, VarLenRef};

fn time<R>(acc: &mut Duration, body: impl FnOnce() -> R) -> R {
Expand Down Expand Up @@ -759,7 +759,7 @@ fn eq_in_page_same(c: &mut Criterion) {
let mut group = c.benchmark_group("eq_in_page");
for (name, ty, value, _null_visitor, _aligned_offsets_visitor) in product_value_test_cases() {
let (ty, mut page, visitor) = ty_page_visitor(ty);
let static_bsatn_layout = StaticBsatnLayout::for_row_type(&ty);
let static_bsatn_layout = StaticLayout::for_row_type(&ty);

let (offset_0, _) = unsafe { write_row_to_page(&mut page, &mut NullBlobStore, &visitor, &ty, &value) }.unwrap();
let (offset_1, _) = unsafe { write_row_to_page(&mut page, &mut NullBlobStore, &visitor, &ty, &value) }.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions crates/table/src/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use super::{
bflatn_from::read_tag,
bflatn_to_bsatn_fast_path::StaticBsatnLayout,
indexes::{Bytes, PageOffset},
layout::{align_to, AlgebraicTypeLayout, HasLayout, ProductTypeLayout, RowTypeLayout},
page::Page,
row_hash::read_from_bytes,
static_layout::StaticLayout,
util::range_move,
var_len::VarLenRef,
};
Expand All @@ -32,7 +32,7 @@ pub unsafe fn eq_row_in_page(
fixed_offset_a: PageOffset,
fixed_offset_b: PageOffset,
ty: &RowTypeLayout,
static_bsatn_layout: Option<&StaticBsatnLayout>,
static_layout: Option<&StaticLayout>,
) -> bool {
// Contexts for rows `a` and `b`.
let a = BytesPage::new(page_a, fixed_offset_a, ty);
Expand All @@ -41,7 +41,7 @@ pub unsafe fn eq_row_in_page(
// If there are only fixed parts in the layout,
// there are no pointers to anywhere,
// So it is sound to simply check for byte-wise equality while ignoring padding.
match static_bsatn_layout {
match static_layout {
None => {
// Context for the whole comparison.
let mut ctx = EqCtx { a, b, curr_offset: 0 };
Expand Down
2 changes: 1 addition & 1 deletion crates/table/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

pub mod bflatn_from;
pub mod bflatn_to;
pub mod bflatn_to_bsatn_fast_path;
pub mod blob_store;
pub mod btree_index;
pub mod eq;
Expand All @@ -23,6 +22,7 @@ pub mod read_column;
pub mod row_hash;
pub mod row_type_visitor;
pub mod static_bsatn_validator;
pub mod static_layout;
pub mod table;
pub mod var_len;

Expand Down
23 changes: 11 additions & 12 deletions crates/table/src/static_bsatn_validator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! To efficiently implement a fast-path BSATN -> BFLATN,
//! we use a `StaticBsatnLayout` but in reverse of the read path.
//! we use a `StaticLayout` but in reverse of the read path.
//! This however leaves us with no way to validate
//! that the BSATN satisfies the row type of a given table.
//!
Expand All @@ -21,8 +21,8 @@
#![allow(unused)]

use super::{
bflatn_to_bsatn_fast_path::StaticBsatnLayout,
layout::{AlgebraicTypeLayout, HasLayout as _, ProductTypeLayout, RowTypeLayout},
static_layout::StaticLayout,
MemoryUsage,
};
use itertools::{repeat_n, Itertools as _};
Expand All @@ -32,7 +32,7 @@ use std::sync::Arc;

/// Constructs a validator for a row encoded in BSATN
/// that checks that the row satisfies the type `ty`
/// when `ty` has `StaticBsatnLayout`.
/// when `ty` has `StaticLayout`.
///
/// This is a potentially expensive operation,
/// so the resulting `StaticBsatnValidator` should be stored and re-used.
Expand Down Expand Up @@ -313,19 +313,19 @@ unsafe fn check_tag(bytes: &[u8], check: CheckTag) -> Result<u8, DecodeError> {

/// Validates that `bytes`, encoded in BSATN,
/// is valid according to the validation `program`
/// and a corresponding `static_bsatn_layout`,
/// and a corresponding `static_layout`,
///
/// # Safety
///
/// The caller must guarantee that
/// all offsets in `program` are `< static_bsatn_layout.bsatn_length`.
/// all offsets in `program` are `< static_layout.bsatn_length`.
pub(crate) unsafe fn validate_bsatn(
program: &StaticBsatnValidator,
static_bsatn_layout: &StaticBsatnLayout,
static_layout: &StaticLayout,
bytes: &[u8],
) -> Result<(), DecodeError> {
// Validate length of BSATN `bytes` against the expected length.
let expected = static_bsatn_layout.bsatn_length as usize;
let expected = static_layout.bsatn_length as usize;
let given = bytes.len();
if expected != given {
return Err(DecodeError::InvalidLen { expected, given });
Expand Down Expand Up @@ -388,15 +388,14 @@ pub mod test {
#[test]
fn validation_same_as_write_row_to_pages((ty, val) in generate_typed_row()) {
let ty: RowTypeLayout = ty.into();
let Some(static_bsatn_layout) = StaticBsatnLayout::for_row_type(&ty) else {
let Some(static_layout) = StaticLayout::for_row_type(&ty) else {
// `ty` has a var-len member or a sum with different payload lengths,
// so the fast path doesn't apply.
return Err(TestCaseError::reject("Var-length type"));
};
let validator = static_bsatn_validator(&ty);
let bsatn = to_vec(&val).unwrap();
let res_validate = unsafe { validate_bsatn(&validator, &static_bsatn_layout, &bsatn) };

let res_validate = unsafe { validate_bsatn(&validator, &static_layout, &bsatn) };

let mut page = Page::new(ty.size());
let visitor = row_type_visitor(&ty);
Expand All @@ -409,11 +408,11 @@ pub mod test {
#[test]
fn bad_bool_validates_to_error(byte in 2u8..) {
let ty: RowTypeLayout = ProductType::from([AlgebraicType::Bool]).into();
let static_bsatn_layout = StaticBsatnLayout::for_row_type(&ty).unwrap();
let static_layout = StaticLayout::for_row_type(&ty).unwrap();
let validator = static_bsatn_validator(&ty);

let bsatn = [byte];
let res_validate = unsafe { validate_bsatn(&validator, &static_bsatn_layout, &bsatn) };
let res_validate = unsafe { validate_bsatn(&validator, &static_layout, &bsatn) };
prop_assert_eq!(res_validate, Err(DecodeError::InvalidBool(byte)));
}
}
Expand Down
Loading
Loading