Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centril/flatten sats #381

Draft
wants to merge 2 commits into
base: centril/flatten-algebraic
Choose a base branch
from
Draft
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
22 changes: 9 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ arrayvec = "0.7.2"
backtrace = "0.3.66"
base64 = "0.21.2"
bitflags = "2.3.3"
bit-set = "0.5.3"
byte-unit = "4.0.18"
bytes = "1.2.1"
bytestring = { version = "1.2.0", features = ["serde"] }
Expand Down Expand Up @@ -115,7 +116,6 @@ jsonwebtoken = { version = "8.1.0" }
lazy_static = "1.4.0"
log = "0.4.17"
mimalloc = "0.1.39"
nonempty = "0.8.1"
once_cell = "1.16"
parking_lot = { version = "0.12.1", features = ["send_guard", "arc_lock"] }
paste = "1.0"
Expand Down
6 changes: 3 additions & 3 deletions crates/bench/src/schemas.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use spacetimedb_lib::sats;
use spacetimedb_lib::sats::{self, product, SatsString};
use std::fmt::Debug;
use std::hash::Hash;

Expand Down Expand Up @@ -60,7 +60,7 @@ impl BenchTable for Person {
.into()
}
fn into_product_value(self) -> sats::ProductValue {
sats::product![self.id, self.name, self.age]
sats::product![self.id, SatsString::from_string(self.name), self.age]
}

type SqliteParams = (u32, String, u64);
Expand All @@ -86,7 +86,7 @@ impl BenchTable for Location {
.into()
}
fn into_product_value(self) -> sats::ProductValue {
sats::product![self.id, self.x, self.y]
product![self.id, self.x, self.y]
}

type SqliteParams = (u32, u64, u64);
Expand Down
22 changes: 17 additions & 5 deletions crates/bench/src/spacetime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl BenchDatabase for SpacetimeModule {

let count = runtime.block_on(async move {
let name = format!("count_{}", table_id.snake_case);
module.call_reducer_binary(&name, ProductValue::new(&[])).await?;
module.call_reducer_binary(&name, ProductValue::new([].into())).await?;
let logs = module.read_log(Some(1)).await;
let message = serde_json::from_str::<LoggerRecord>(&logs)?;
if !message.message.starts_with("COUNT: ") {
Expand All @@ -135,7 +135,9 @@ impl BenchDatabase for SpacetimeModule {
let module = module.as_mut().unwrap();

runtime.block_on(async move {
module.call_reducer_binary("empty", ProductValue::new(&[])).await?;
module
.call_reducer_binary("empty", ProductValue::new([].into()))
.await?;
Ok(())
})
}
Expand All @@ -154,7 +156,12 @@ impl BenchDatabase for SpacetimeModule {
}

fn insert_bulk<T: BenchTable>(&mut self, table_id: &Self::TableId, rows: Vec<T>) -> ResultBench<()> {
let rows = rows.into_iter().map(|row| row.into_product_value()).collect();
let rows = rows
.into_iter()
.map(|row| row.into_product_value())
.collect::<Box<[_]>>()
.try_into()
.unwrap();
let args = product![ArrayValue::Product(rows)];
let SpacetimeModule { runtime, module } = self;
let module = module.as_mut().unwrap();
Expand All @@ -173,7 +180,7 @@ impl BenchDatabase for SpacetimeModule {

runtime.block_on(async move {
module
.call_reducer_binary(&reducer_name, ProductValue::new(&[]))
.call_reducer_binary(&reducer_name, ProductValue::new([].into()))
.await?;
Ok(())
})
Expand All @@ -194,7 +201,12 @@ impl BenchDatabase for SpacetimeModule {

runtime.block_on(async move {
module
.call_reducer_binary(&reducer_name, ProductValue { elements: vec![value] })
.call_reducer_binary(
&reducer_name,
ProductValue {
elements: [value].into(),
},
)
.await?;
Ok(())
})
Expand Down
9 changes: 5 additions & 4 deletions crates/bench/src/spacetime_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use crate::{
use spacetimedb::db::relational_db::{open_db, RelationalDB};
use spacetimedb::execution_context::ExecutionContext;
use spacetimedb_lib::sats::db::def::{IndexDef, TableDef};
use spacetimedb_lib::sats::AlgebraicValue;
use spacetimedb_lib::sats::{AlgebraicValue, SatsString};
use spacetimedb_primitives::{ColId, TableId};
use spacetimedb_sats::nstr;
use std::hint::black_box;
use tempdir::TempDir;

Expand Down Expand Up @@ -38,15 +39,15 @@ impl BenchDatabase for SpacetimeRaw {
}

fn create_table<T: BenchTable>(&mut self, index_strategy: IndexStrategy) -> ResultBench<Self::TableId> {
let name = table_name::<T>(index_strategy);
let name = SatsString::from_string(table_name::<T>(index_strategy));
self.db.with_auto_commit(&ExecutionContext::default(), |tx| {
let table_def = TableDef::from(T::product_type());
let table_id = self.db.create_table(tx, table_def)?;
self.db.rename_table(tx, table_id, &name)?;
self.db.rename_table(tx, table_id, name)?;
match index_strategy {
IndexStrategy::Unique => {
self.db
.create_index(tx, IndexDef::new("id".to_string(), table_id, 0.into(), true))?;
.create_index(tx, IndexDef::new(nstr!("id"), table_id, 0.into(), true))?;
}
IndexStrategy::NonUnique => (),
IndexStrategy::MultiIndex => {
Expand Down
11 changes: 5 additions & 6 deletions crates/bench/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use ahash::AHashMap;
use lazy_static::lazy_static;
use rusqlite::Connection;
use spacetimedb_lib::sats::{AlgebraicType, AlgebraicValue, ProductType};
use spacetimedb_lib::sats::{AlgebraicType, AlgebraicValue, ProductType, SatsString};
use std::{
fmt::Write,
hint::black_box,
Expand All @@ -26,6 +26,8 @@ impl BenchDatabase for SQLite {
"sqlite"
}

type TableId = SatsString;

fn build(in_memory: bool, fsync: bool) -> ResultBench<Self>
where
Self: Sized,
Expand All @@ -50,8 +52,6 @@ impl BenchDatabase for SQLite {
})
}

type TableId = String;

/// We derive the SQLite schema from the AlgebraicType of the table.
fn create_table<T: BenchTable>(
&mut self,
Expand Down Expand Up @@ -91,7 +91,7 @@ impl BenchDatabase for SQLite {
log::info!("SQLITE: `{statement}`");
self.db.execute_batch(&statement)?;

Ok(table_name)
Ok(SatsString::from_string(table_name))
}

fn clear_table(&mut self, table_id: &Self::TableId) -> ResultBench<()> {
Expand Down Expand Up @@ -172,8 +172,7 @@ impl BenchDatabase for SQLite {
value: AlgebraicValue,
) -> ResultBench<()> {
let statement = memo_query(BenchName::Filter, table_id, || {
let column = T::product_type()
.elements
let column = Vec::from(T::product_type().elements)
.swap_remove(column_index as usize)
.name
.unwrap();
Expand Down
17 changes: 10 additions & 7 deletions crates/bindings-macro/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ pub(crate) fn derive_satstype(ty: &SatsType<'_>, gen_type_alias: bool) -> TokenS

let (impl_generics, ty_generics, where_clause) = ty.generics.split_for_impl();
let ty_name = if gen_type_alias {
quote!(Some(#ty_name))
quote!(Some({
const NAME: &spacetimedb::sats::SatsStr<'_> = &spacetimedb::sats::from_str(#ty_name);
NAME
}))
} else {
quote!(None)
};
Expand Down Expand Up @@ -283,10 +286,10 @@ pub(crate) fn derive_deserialize(ty: &SatsType<'_>) -> TokenStream {
names.extend::<&[&str]>(&[#(#field_strings),*])
}

fn visit<__E: #spacetimedb_lib::de::Error>(self, name: &str) -> Result<Self::Output, __E> {
match name {
fn visit<__E: #spacetimedb_lib::de::Error>(self, name: #spacetimedb_lib::SatsStr<'_>) -> Result<Self::Output, __E> {
match &*name {
#(#field_strings => Ok(__ProductFieldIdent::#field_names),)*
_ => Err(#spacetimedb_lib::de::Error::unknown_field_name(name, &self)),
name => Err(#spacetimedb_lib::de::Error::unknown_field_name(name, &self)),
}
}
}
Expand Down Expand Up @@ -366,9 +369,9 @@ pub(crate) fn derive_deserialize(ty: &SatsType<'_>) -> TokenStream {
}
}
fn visit_name<E: #spacetimedb_lib::de::Error>(self, __name: &str) -> Result<Self::Output, E> {
match __name {
match &*__name {
#(#variant_names => Ok(__Variant::#variant_idents),)*
_ => Err(#spacetimedb_lib::de::Error::unknown_variant_name(__name, &self)),
__name => Err(#spacetimedb_lib::de::Error::unknown_variant_name(__name, &self)),
}
}
}
Expand All @@ -390,7 +393,7 @@ pub(crate) fn derive_serialize(ty: &SatsType) -> TokenStream {
let nfields = fields.len();
quote! {
let mut __prod = __serializer.serialize_named_product(#nfields)?;
#(#spacetimedb_lib::ser::SerializeNamedProduct::serialize_element::<#tys>(&mut __prod, Some(#fieldnamestrings), &self.#fieldnames)?;)*
#(#spacetimedb_lib::ser::SerializeNamedProduct::serialize_element::<#tys>(&mut __prod, Some(#spacetimedb_lib::from_str(#fieldnamestrings)), &self.#fieldnames)?;)*
#spacetimedb_lib::ser::SerializeNamedProduct::end(__prod)
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ bench = false
getrandom = ["spacetimedb-bindings-sys/getrandom"]

[dependencies]
spacetimedb-bindings-macro = { path = "../bindings-macro", version = "0.7.3"}
spacetimedb-bindings-sys = { path = "../bindings-sys", version = "0.7.3" }
spacetimedb-data-structures = { path = "../data-structures", version = "0.7.3" }
spacetimedb-lib = { path = "../lib", default-features = false, version = "0.7.3"}
spacetimedb-bindings-macro = { path = "../bindings-macro", version = "0.7.3"}
spacetimedb-primitives = { path = "../primitives", version = "0.7.3" }
spacetimedb-sats = { path = "../sats", version = "0.7.3" }

derive_more.workspace = true
log.workspace = true
nonempty.workspace = true
once_cell.workspace = true
scoped-tls.workspace = true

Expand Down
Loading
Loading