Skip to content

Commit

Permalink
sats: misc refactoring (#382)
Browse files Browse the repository at this point in the history
- more conversions
- prepare for getting rid of BuiltinValue & BuiltinType
- prepare for slim slices
  • Loading branch information
Centril authored Oct 8, 2023
1 parent 77065a8 commit a45c30c
Show file tree
Hide file tree
Showing 39 changed files with 479 additions and 618 deletions.
8 changes: 4 additions & 4 deletions crates/bench/benches/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use spacetimedb_bench::{
schemas::{create_sequential, BenchTable, IndexStrategy, Location, Person, RandomTable, BENCH_PKEY_INDEX},
spacetime_module, spacetime_raw, sqlite, ResultBench,
};
use spacetimedb_lib::{sats::BuiltinType, AlgebraicType};
use spacetimedb_lib::sats::AlgebraicType;
fn criterion_benchmark(c: &mut Criterion) {
bench_suite::<sqlite::SQLite>(c, true).unwrap();
bench_suite::<spacetime_raw::SpacetimeRaw>(c, true).unwrap();
Expand Down Expand Up @@ -294,9 +294,9 @@ fn _filter_setup<DB: BenchDatabase, T: BenchTable + RandomTable>(
buckets: u32,
) -> ResultBench<(String, TableSchema, Vec<T>)> {
let filter_column_type = match &T::product_type().elements[column_index as usize].algebraic_type {
AlgebraicType::Builtin(BuiltinType::String) => "string",
AlgebraicType::Builtin(BuiltinType::U32) => "u32",
AlgebraicType::Builtin(BuiltinType::U64) => "u64",
AlgebraicType::String => "string",
AlgebraicType::U32 => "u32",
AlgebraicType::U64 => "u64",
_ => unimplemented!(),
};
let mean_result_count = load / buckets;
Expand Down
56 changes: 14 additions & 42 deletions crates/bench/src/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,15 @@ impl BenchTable for Person {
}

fn product_type() -> sats::ProductType {
sats::ProductType::new(vec![
sats::ProductTypeElement {
name: Some("id".to_string()),
algebraic_type: sats::AlgebraicType::Builtin(sats::BuiltinType::U32),
},
sats::ProductTypeElement {
name: Some("name".to_string()),
algebraic_type: sats::AlgebraicType::Builtin(sats::BuiltinType::String),
},
sats::ProductTypeElement {
name: Some("age".to_string()),
algebraic_type: sats::AlgebraicType::Builtin(sats::BuiltinType::U64),
},
])
[
("id", sats::AlgebraicType::U32),
("name", sats::AlgebraicType::String),
("age", sats::AlgebraicType::U64),
]
.into()
}
fn into_product_value(self) -> sats::ProductValue {
sats::ProductValue {
elements: vec![
sats::AlgebraicValue::Builtin(sats::BuiltinValue::U32(self.id)),
sats::AlgebraicValue::Builtin(sats::BuiltinValue::String(self.name)),
sats::AlgebraicValue::Builtin(sats::BuiltinValue::U64(self.age)),
],
}
sats::product![self.id, self.name, self.age]
}

type SqliteParams = (u32, String, u64);
Expand All @@ -92,29 +78,15 @@ impl BenchTable for Location {
}

fn product_type() -> sats::ProductType {
sats::ProductType::new(vec![
sats::ProductTypeElement {
name: Some("id".to_string()),
algebraic_type: sats::AlgebraicType::Builtin(sats::BuiltinType::U32),
},
sats::ProductTypeElement {
name: Some("x".to_string()),
algebraic_type: sats::AlgebraicType::Builtin(sats::BuiltinType::U64),
},
sats::ProductTypeElement {
name: Some("y".to_string()),
algebraic_type: sats::AlgebraicType::Builtin(sats::BuiltinType::U64),
},
])
[
("id", sats::AlgebraicType::U32),
("x", sats::AlgebraicType::U64),
("y", sats::AlgebraicType::U64),
]
.into()
}
fn into_product_value(self) -> sats::ProductValue {
sats::ProductValue {
elements: vec![
sats::AlgebraicValue::Builtin(sats::BuiltinValue::U32(self.id)),
sats::AlgebraicValue::Builtin(sats::BuiltinValue::U64(self.x)),
sats::AlgebraicValue::Builtin(sats::BuiltinValue::U64(self.y)),
],
}
sats::product![self.id, self.x, self.y]
}

type SqliteParams = (u32, u64, u64);
Expand Down
18 changes: 8 additions & 10 deletions crates/bench/src/spacetime_module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use spacetimedb::db::datastore::traits::TableSchema;
use spacetimedb::db::{Config, FsyncPolicy, Storage};
use spacetimedb_lib::sats::BuiltinValue;
use spacetimedb_lib::sats::{product, BuiltinValue};
use spacetimedb_lib::{sats::ArrayValue, AlgebraicValue, ProductValue};
use spacetimedb_testing::modules::{start_runtime, CompiledModule, ModuleHandle};
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -150,11 +150,9 @@ impl BenchDatabase for SpacetimeModule {
}

fn insert_bulk<T: BenchTable>(&mut self, table_id: &Self::TableId, rows: Vec<T>) -> ResultBench<()> {
let args = ProductValue {
elements: vec![AlgebraicValue::Builtin(spacetimedb_lib::sats::BuiltinValue::Array {
val: ArrayValue::Product(rows.into_iter().map(|row| row.into_product_value()).collect()),
})],
};
let args = product![AlgebraicValue::ArrayOf(ArrayValue::Product(
rows.into_iter().map(|row| row.into_product_value()).collect(),
))];
let SpacetimeModule { runtime, module } = self;
let module = module.as_mut().unwrap();
let reducer_name = format!("insert_bulk_{}", table_id.snake_case);
Expand Down Expand Up @@ -218,10 +216,10 @@ impl BenchDatabase for SpacetimeModule {
) -> ResultBench<()> {
let column = &table.columns[column_index as usize].col_name;

let value = match value.as_builtin().unwrap() {
BuiltinValue::U32(x) => x.to_string(),
BuiltinValue::U64(x) => x.to_string(),
BuiltinValue::String(x) => format!("'{}'", x),
let value = match value {
AlgebraicValue::Builtin(BuiltinValue::U32(x)) => x.to_string(),
AlgebraicValue::Builtin(BuiltinValue::U64(x)) => x.to_string(),
AlgebraicValue::Builtin(BuiltinValue::String(x)) => format!("'{}'", x),
_ => {
unreachable!()
}
Expand Down
11 changes: 5 additions & 6 deletions crates/bench/src/spacetime_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use spacetimedb::db::datastore::traits::{IndexDef, TableDef, TableSchema};
use spacetimedb::db::relational_db::{open_db, RelationalDB};
use spacetimedb::error::DBError;
use spacetimedb::sql::execute::run;
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_lib::sats::BuiltinValue;
use spacetimedb_lib::AlgebraicValue;
use spacetimedb_lib::{identity::AuthCtx, sats::BuiltinValue};
use std::hint::black_box;
use tempdir::TempDir;

Expand Down Expand Up @@ -150,10 +149,10 @@ impl BenchDatabase for SpacetimeRaw {

let table_name = &table.table_name;

let value = match value.as_builtin().unwrap() {
BuiltinValue::U32(x) => x.to_string(),
BuiltinValue::U64(x) => x.to_string(),
BuiltinValue::String(x) => format!("'{}'", x),
let value = match value {
AlgebraicValue::Builtin(BuiltinValue::U32(x)) => x.to_string(),
AlgebraicValue::Builtin(BuiltinValue::U64(x)) => x.to_string(),
AlgebraicValue::Builtin(BuiltinValue::String(x)) => format!("'{}'", x),
_ => {
unreachable!()
}
Expand Down
18 changes: 7 additions & 11 deletions crates/bench/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use ahash::AHashMap;
use lazy_static::lazy_static;
use rusqlite::Connection;
use spacetimedb::db::datastore::traits::TableSchema;
use spacetimedb_lib::{
sats::{self},
AlgebraicType, AlgebraicValue, ProductType,
};
use spacetimedb_lib::{sats::BuiltinValue, AlgebraicType, AlgebraicValue, ProductType};
use std::{
fmt::Write,
hint::black_box,
Expand Down Expand Up @@ -68,9 +65,8 @@ impl BenchDatabase for SQLite {
for (i, column) in T::product_type().elements.iter().enumerate() {
let column_name = column.name.clone().unwrap();
let type_ = match column.algebraic_type {
AlgebraicType::Builtin(sats::BuiltinType::U32) => "INTEGER",
AlgebraicType::Builtin(sats::BuiltinType::U64) => "INTEGER",
AlgebraicType::Builtin(sats::BuiltinType::String) => "TEXT",
AlgebraicType::U32 | AlgebraicType::U64 => "INTEGER",
AlgebraicType::String => "TEXT",
_ => unimplemented!(),
};
let extra = if index_strategy == IndexStrategy::Unique && i == 0 {
Expand Down Expand Up @@ -192,19 +188,19 @@ impl BenchDatabase for SQLite {

begin.execute(())?;
match value {
AlgebraicValue::Builtin(sats::BuiltinValue::String(value)) => {
for _ in stmt.query_map((value,), |row| {
AlgebraicValue::Builtin(BuiltinValue::String(value)) => {
for _ in stmt.query_map((&*value,), |row| {
black_box(row);
Ok(())
})? {}
}
AlgebraicValue::Builtin(sats::BuiltinValue::U32(value)) => {
AlgebraicValue::Builtin(BuiltinValue::U32(value)) => {
for _ in stmt.query_map((value,), |row| {
black_box(row);
Ok(())
})? {}
}
AlgebraicValue::Builtin(sats::BuiltinValue::U64(value)) => {
AlgebraicValue::Builtin(BuiltinValue::U64(value)) => {
for _ in stmt.query_map((value,), |row| {
black_box(row);
Ok(())
Expand Down
36 changes: 25 additions & 11 deletions crates/bindings-macro/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,23 @@ pub(crate) fn derive_satstype(ty: &SatsType<'_>, gen_type_alias: bool) -> TokenS
SatsTypeData::Product(fields) => {
let fields = fields.iter().map(|field| {
let field_name = match &field.name {
Some(name) => quote!(Some(#name.to_owned())),
Some(name) => quote!(Some(#name)),
None => quote!(None),
};
let ty = field.ty;
quote!(spacetimedb::sats::ProductTypeElement {
name: #field_name,
algebraic_type: <#ty as spacetimedb::SpacetimeType>::make_type(__typespace),
})
quote!((
#field_name,
<#ty as spacetimedb::SpacetimeType>::make_type(__typespace),
))
});
quote!(spacetimedb::sats::AlgebraicType::product(vec![#(#fields),*]))
let len = fields.len();
quote!(
spacetimedb::sats::AlgebraicType::product::<
[(Option<&str>, spacetimedb::sats::AlgebraicType); #len]
>(
[#(#fields),*]
)
)
}
SatsTypeData::Sum(variants) => {
let unit = syn::Type::Tuple(syn::TypeTuple {
Expand All @@ -145,12 +152,19 @@ pub(crate) fn derive_satstype(ty: &SatsType<'_>, gen_type_alias: bool) -> TokenS
let variants = variants.iter().map(|var| {
let variant_name = &var.name;
let ty = var.ty.unwrap_or(&unit);
quote!(spacetimedb::sats::SumTypeVariant {
name: Some(#variant_name.to_owned()),
algebraic_type: <#ty as spacetimedb::SpacetimeType>::make_type(__typespace),
})
quote!((
#variant_name,
<#ty as spacetimedb::SpacetimeType>::make_type(__typespace),
))
});
quote!(spacetimedb::sats::AlgebraicType::sum(vec![#(#variants),*]))
let len = variants.len();
quote!(
spacetimedb::sats::AlgebraicType::sum::<
[(&str, spacetimedb::sats::AlgebraicType); #len]
>(
[#(#variants),*]
)
)
// todo!()
} // syn::Data::Union(u) => return Err(syn::Error::new(u.union_token.span, "unions not supported")),
};
Expand Down
2 changes: 1 addition & 1 deletion crates/bindings/src/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl TypespaceBuilder for ModuleBuilder {
btree_map::Entry::Occupied(o) => *o.get(),
btree_map::Entry::Vacant(v) => {
// Bind a fresh alias to the unit type.
let slot_ref = self.module.typespace.add(AlgebraicType::UNIT_TYPE);
let slot_ref = self.module.typespace.add(AlgebraicType::unit());
// Relate `typeid -> fresh alias`.
v.insert(slot_ref);

Expand Down
Loading

0 comments on commit a45c30c

Please sign in to comment.