Module spacetimedb_table::bflatn_to_bsatn_fast_path

source ·
Expand description

This module implements a fast path for serializing certain types from BFLATN to BSATN.

The key insight is that a majority of row types will have a known fixed length, with no variable-length members. BFLATN is designed with this in mind, storing fixed-length portions of rows inline, at the expense of an indirection to reach var-length columns like strings. A majority of these types will also have a fixed BSATN length, but note that BSATN stores sum values (enums) without padding, so row types which contain sums may not have a fixed BSATN length if the sum’s variants have different “live” unpadded lengths.

For row types with fixed BSATN lengths, we can reduce the BFLATN -> BSATN conversion to a series of memcpys, skipping over padding sequences. This is potentially much faster than the more general crate::bflatn_from::serialize_row_from_page, which traverses a RowTypeLayout and dispatches on the type of each column.

For example, to serialize a row of type (u64, u64, u32, u64), [bflatn_from] will do four dispatches, three calls to serialize_u64 and one to serialize_u32. This module will make 2 memcpys (or actually, <[u8]>::copy_from_slices): one of 20 bytes to copy the leading (u64, u64, u32), which contains no padding, and then one of 8 bytes to copy the trailing u64, skipping over 4 bytes of padding in between.

Structs§

  • A precomputed BSATN layout for a type whose encoded length is a known constant, enabling fast BFLATN -> BSATN conversion.