Skip to content

Commit b27314a

Browse files
committed
Shrink AV and AT to 24 & 16 bytes respectively, and also friends.
1 parent 4cd17d7 commit b27314a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+657
-631
lines changed

crates/bench/benches/callgrind.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,10 @@ mod callgrind_benches {
528528
assert_eq!(self.format, "bsatn", "provided metadata has incorrect format");
529529

530530
let buckets = 64;
531-
let data = create_sequential::<u32_u64_str>(0xdeadbeef, self.count, buckets);
532-
let data = ProductValue {
533-
elements: data
534-
.into_iter()
535-
.map(|row| spacetimedb_lib::AlgebraicValue::Product(row.into_product_value()))
536-
.collect::<Vec<_>>(),
537-
};
531+
let data = create_sequential::<u32_u64_str>(0xdeadbeef, self.count, buckets)
532+
.into_iter()
533+
.map(|row| spacetimedb_lib::AlgebraicValue::Product(row.into_product_value()))
534+
.collect::<ProductValue>();
538535

539536
spacetimedb::callgrind_flag::enable_callgrind_globally(|| {
540537
// don't time deallocation: return this!
@@ -567,13 +564,10 @@ mod callgrind_benches {
567564
assert_eq!(self.format, "json", "provided metadata has incorrect format");
568565

569566
let buckets = 64;
570-
let data = create_sequential::<u32_u64_str>(0xdeadbeef, self.count, buckets);
571-
let data = ProductValue {
572-
elements: data
573-
.into_iter()
574-
.map(|row| spacetimedb_lib::AlgebraicValue::Product(row.into_product_value()))
575-
.collect::<Vec<_>>(),
576-
};
567+
let data = create_sequential::<u32_u64_str>(0xdeadbeef, self.count, buckets)
568+
.into_iter()
569+
.map(|row| spacetimedb_lib::AlgebraicValue::Product(row.into_product_value()))
570+
.collect::<ProductValue>();
577571

578572
spacetimedb::callgrind_flag::enable_callgrind_globally(|| {
579573
// don't time deallocation: return this!

crates/bench/benches/special.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn custom_module_benchmarks(c: &mut Criterion) {
2929
};
3030
let module = runtime.block_on(async { BENCHMARKS_MODULE.load_module(config, None).await });
3131

32-
let args = sats::product!["0".repeat(65536)];
32+
let args = sats::product!["0".repeat(65536).into_boxed_str()];
3333
c.bench_function("special/stdb_module/large_arguments/64KiB", |b| {
3434
b.iter_batched(
3535
|| args.clone(),
@@ -66,12 +66,10 @@ fn serialize_benchmarks<T: BenchTable + RandomTable>(c: &mut Criterion) {
6666
);
6767
});
6868
// this measures serialization from a ProductValue, not directly (as in, from generated code in the Rust SDK.)
69-
let data_pv = ProductValue {
70-
elements: data
71-
.into_iter()
72-
.map(|row| spacetimedb_lib::AlgebraicValue::Product(row.into_product_value()))
73-
.collect::<Vec<_>>(),
74-
};
69+
let data_pv = data
70+
.into_iter()
71+
.map(|row| spacetimedb_lib::AlgebraicValue::Product(row.into_product_value()))
72+
.collect::<ProductValue>();
7573

7674
group.bench_function(&format!("{name}/bsatn/count={count}"), |b| {
7775
b.iter_batched_ref(

crates/bench/benches/subscription.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn create_table_footprint(db: &RelationalDB) -> Result<TableId, DBError> {
4040
fn insert_op(table_id: TableId, table_name: &str, row: ProductValue) -> DatabaseTableUpdate {
4141
DatabaseTableUpdate {
4242
table_id,
43-
table_name: table_name.to_string(),
43+
table_name: table_name.into(),
4444
inserts: [row].into(),
4545
deletes: [].into(),
4646
}

crates/bench/src/schemas.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct u32_u64_str {
1919
// column 1
2020
age: u64,
2121
// column 2
22-
name: String,
22+
name: Box<str>,
2323
}
2424

2525
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -82,7 +82,7 @@ impl BenchTable for u32_u64_str {
8282
sats::product![self.id, self.age, self.name,]
8383
}
8484

85-
type SqliteParams = (u32, u64, String);
85+
type SqliteParams = (u32, u64, Box<str>);
8686
fn into_sqlite_params(self) -> Self::SqliteParams {
8787
(self.id, self.age, self.name)
8888
}
@@ -192,7 +192,7 @@ pub trait RandomTable {
192192

193193
impl RandomTable for u32_u64_str {
194194
fn gen(id: u32, rng: &mut XorShiftLite, buckets: u64) -> Self {
195-
let name = nth_name(rng.gen() % buckets);
195+
let name = nth_name(rng.gen() % buckets).into();
196196
let age = rng.gen() % buckets;
197197
u32_u64_str { id, name, age }
198198
}

crates/bench/src/spacetime_module.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::Path;
33
use spacetimedb::db::{Config, Storage};
44
use spacetimedb_lib::{
55
sats::{product, ArrayValue},
6-
AlgebraicValue, ProductValue,
6+
AlgebraicValue,
77
};
88
use spacetimedb_testing::modules::{start_runtime, CompilationMode, CompiledModule, LoggerRecord, ModuleHandle};
99
use tokio::runtime::Runtime;
@@ -107,7 +107,7 @@ impl BenchDatabase for SpacetimeModule {
107107
module.call_reducer_binary(&name, ProductValue::new(&[])).await?;
108108
*/
109109
// workaround for now
110-
module.client.module.clear_table(table_id.pascal_case.clone()).await?;
110+
module.client.module.clear_table(&table_id.pascal_case).await?;
111111
Ok(())
112112
})
113113
}
@@ -194,9 +194,7 @@ impl BenchDatabase for SpacetimeModule {
194194
let reducer_name = format!("filter_{}_by_{}", table_id.snake_case, column_name);
195195

196196
runtime.block_on(async move {
197-
module
198-
.call_reducer_binary(&reducer_name, ProductValue { elements: vec![value] })
199-
.await?;
197+
module.call_reducer_binary(&reducer_name, [value].into()).await?;
200198
Ok(())
201199
})
202200
}

crates/bench/src/sqlite.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ impl BenchDatabase for SQLite {
135135

136136
fn update_bulk<T: BenchTable>(&mut self, table_id: &Self::TableId, row_count: u32) -> ResultBench<()> {
137137
let mut product = T::product_type();
138-
let id_column = product.elements.swap_remove(0).name.unwrap();
139-
let update_column = product.elements.swap_remove(1).name.unwrap();
138+
let id_column = product.elements[0].name.take().unwrap();
139+
let update_column = product.elements[1].name.take().unwrap();
140140
// this relies on IDs having been generated in order...
141141
let statement =
142142
format!("UPDATE {table_id} SET {update_column} = {update_column} + 1 WHERE {id_column} < {row_count}");
@@ -175,11 +175,7 @@ impl BenchDatabase for SQLite {
175175
value: AlgebraicValue,
176176
) -> ResultBench<()> {
177177
let statement = memo_query(BenchName::Filter, table_id, || {
178-
let column: String = T::product_type()
179-
.elements
180-
.swap_remove(column_index as usize)
181-
.name
182-
.unwrap();
178+
let column: Box<str> = T::product_type().elements[column_index as usize].name.take().unwrap();
183179
format!("SELECT * FROM {table_id} WHERE {column} = ?")
184180
});
185181

crates/bindings/src/rt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ macro_rules! impl_reducer {
270270
name: Info::NAME.into(),
271271
args: vec![
272272
$(ProductTypeElement {
273-
name: $T.map(str::to_owned),
273+
name: $T.map(Into::into),
274274
algebraic_type: <$T>::make_type(_typespace),
275275
}),*
276276
],
@@ -470,7 +470,7 @@ impl From<crate::IndexDesc<'_>> for IndexDef {
470470
};
471471

472472
IndexDef {
473-
index_name: index.name.to_string(),
473+
index_name: index.name.into(),
474474
is_unique: false,
475475
index_type: index.ty,
476476
columns,

crates/cli/src/subcommands/generate/csharp.rs

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::util::fmt_fn;
22

33
use std::fmt::{self, Write};
4+
use std::ops::Deref;
45

56
use convert_case::{Case, Casing};
67
use spacetimedb_lib::sats::db::def::TableSchema;
@@ -110,7 +111,7 @@ fn ty_fmt<'a>(ctx: &'a GenCtx, ty: &'a AlgebraicType, namespace: &'a str) -> imp
110111
let name = csharp_typename(ctx, *r);
111112
match &ctx.typespace.types[r.idx()] {
112113
AlgebraicType::Sum(sum_type) => {
113-
if is_enum(sum_type) {
114+
if sum_type.is_simple_enum() {
114115
let parts: Vec<&str> = name.split('.').collect();
115116
if parts.len() >= 2 {
116117
let enum_namespace = parts[0];
@@ -253,7 +254,7 @@ fn convert_type<'a>(
253254
let algebraic_type = &ctx.typespace.types[r.idx()];
254255
match algebraic_type {
255256
AlgebraicType::Sum(sum) => {
256-
if is_enum(sum) {
257+
if sum.is_simple_enum() {
257258
let split: Vec<&str> = name.split('.').collect();
258259
if split.len() >= 2 {
259260
assert_eq!(
@@ -317,7 +318,7 @@ fn convert_algebraic_type<'a>(ctx: &'a GenCtx, ty: &'a AlgebraicType, namespace:
317318
let name = csharp_typename(ctx, *r);
318319
match &ctx.typespace.types[r.idx()] {
319320
AlgebraicType::Sum(sum_type) => {
320-
if is_enum(sum_type) {
321+
if sum_type.is_simple_enum() {
321322
let parts: Vec<&str> = name.split('.').collect();
322323
if parts.len() >= 2 {
323324
let enum_namespace = parts[0];
@@ -349,7 +350,7 @@ fn convert_product_type<'a>(
349350
"SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]"
350351
)?;
351352
writeln!(f, "{{")?;
352-
for elem in &product_type.elements {
353+
for elem in &*product_type.elements {
353354
writeln!(
354355
f,
355356
"{INDENT}new SpacetimeDB.SATS.ProductTypeElement({}, {}),",
@@ -371,7 +372,7 @@ fn convert_sum_type<'a>(ctx: &'a GenCtx, sum_type: &'a SumType, namespace: &'a s
371372
"SpacetimeDB.SATS.AlgebraicType.CreateSumType(new System.Collections.Generic.List<SpacetimeDB.SATS.SumTypeVariant>"
372373
)?;
373374
writeln!(f, "{{")?;
374-
for elem in &sum_type.variants {
375+
for elem in &*sum_type.variants {
375376
writeln!(
376377
f,
377378
"\tnew SpacetimeDB.SATS.SumTypeVariant({}, {}),",
@@ -386,23 +387,8 @@ fn convert_sum_type<'a>(ctx: &'a GenCtx, sum_type: &'a SumType, namespace: &'a s
386387
})
387388
}
388389

389-
pub fn is_enum(sum_type: &SumType) -> bool {
390-
for variant in sum_type.clone().variants {
391-
match variant.algebraic_type {
392-
AlgebraicType::Product(product) => {
393-
if product.elements.is_empty() {
394-
continue;
395-
}
396-
}
397-
_ => return false,
398-
}
399-
}
400-
401-
true
402-
}
403-
404390
pub fn autogen_csharp_sum(ctx: &GenCtx, name: &str, sum_type: &SumType, namespace: &str) -> String {
405-
if is_enum(sum_type) {
391+
if sum_type.is_simple_enum() {
406392
autogen_csharp_enum(ctx, name, sum_type, namespace)
407393
} else {
408394
unimplemented!();
@@ -467,7 +453,7 @@ pub fn autogen_csharp_enum(ctx: &GenCtx, name: &str, sum_type: &SumType, namespa
467453
writeln!(output, "{{").unwrap();
468454
{
469455
indent_scope!(output);
470-
for variant in &sum_type.variants {
456+
for variant in &*sum_type.variants {
471457
let variant_name = variant
472458
.name
473459
.as_ref()
@@ -500,7 +486,7 @@ pub fn autogen_csharp_enum(ctx: &GenCtx, name: &str, sum_type: &SumType, namespa
500486
.unwrap();
501487
}
502488
None => {
503-
for variant in &sum_type.variants {
489+
for variant in &*sum_type.variants {
504490
let variant_name = variant
505491
.name
506492
.as_ref()
@@ -614,7 +600,7 @@ fn autogen_csharp_product_table_common(
614600
{
615601
indent_scope!(output);
616602

617-
for field in &product_type.elements {
603+
for field in &*product_type.elements {
618604
let field_name = field
619605
.name
620606
.as_ref()
@@ -641,7 +627,7 @@ fn autogen_csharp_product_table_common(
641627
AlgebraicType::Ref(type_ref) => {
642628
let ref_type = &ctx.typespace.types[type_ref.idx()];
643629
if let AlgebraicType::Sum(sum_type) = ref_type {
644-
if is_enum(sum_type) {
630+
if sum_type.is_simple_enum() {
645631
writeln!(output, "[SpacetimeDB.Enum]").unwrap();
646632
} else {
647633
unimplemented!()
@@ -1307,7 +1293,7 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
13071293
AlgebraicType::Ref(type_ref) => {
13081294
let ref_type = &ctx.typespace.types[type_ref.idx()];
13091295
if let AlgebraicType::Sum(sum_type) = ref_type {
1310-
if is_enum(sum_type) {
1296+
if sum_type.is_simple_enum() {
13111297
json_args.push_str(
13121298
format!("new SpacetimeDB.EnumWrapper<{}>({})", arg_type_str, arg_name).as_str(),
13131299
);
@@ -1403,7 +1389,8 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
14031389
let arg_name = arg
14041390
.name
14051391
.clone()
1406-
.unwrap_or_else(|| format!("arg_{}", i))
1392+
.unwrap_or_else(|| format!("arg_{}", i).into())
1393+
.deref()
14071394
.to_case(Case::Pascal);
14081395
let arg_type_str = ty_fmt(ctx, &arg.algebraic_type, namespace);
14091396
writeln!(output, ",({arg_type_str})args.{arg_name}").unwrap();
@@ -1441,7 +1428,8 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
14411428
let arg_name = arg
14421429
.name
14431430
.clone()
1444-
.unwrap_or_else(|| format!("arg_{}", i))
1431+
.unwrap_or_else(|| format!("arg_{}", i).into())
1432+
.deref()
14451433
.to_case(Case::Pascal);
14461434
let algebraic_type = convert_algebraic_type(ctx, &arg.algebraic_type, namespace);
14471435
writeln!(
@@ -1472,7 +1460,8 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
14721460
let arg_name = arg
14731461
.name
14741462
.clone()
1475-
.unwrap_or_else(|| format!("arg_{}", i))
1463+
.unwrap_or_else(|| format!("arg_{}", i).into())
1464+
.deref()
14761465
.to_case(Case::Pascal);
14771466
let cs_type = ty_fmt(ctx, &arg.algebraic_type, namespace);
14781467
writeln!(output, "public {cs_type} {arg_name};").unwrap();
@@ -1503,7 +1492,7 @@ pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<Vec<(St
15031492
.collect();
15041493
let reducer_names: Vec<String> = reducers
15051494
.iter()
1506-
.map(|reducer| reducer.name.to_case(Case::Pascal))
1495+
.map(|reducer| reducer.name.deref().to_case(Case::Pascal))
15071496
.collect();
15081497

15091498
let use_namespace = true;
@@ -1570,7 +1559,7 @@ pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<Vec<(St
15701559
writeln!(output).unwrap();
15711560
// Properties for reducer args
15721561
for reducer in &reducers {
1573-
let reducer_name = reducer.name.to_case(Case::Pascal);
1562+
let reducer_name = reducer.name.deref().to_case(Case::Pascal);
15741563
writeln!(output, "public {reducer_name}ArgsStruct {reducer_name}Args").unwrap();
15751564
writeln!(output, "{{").unwrap();
15761565
{
@@ -1598,7 +1587,7 @@ pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<Vec<(St
15981587
{
15991588
indent_scope!(output);
16001589
for reducer in &reducers {
1601-
let reducer_name = reducer.name.to_case(Case::Pascal);
1590+
let reducer_name = reducer.name.deref().to_case(Case::Pascal);
16021591
writeln!(output, "case ReducerType.{reducer_name}:").unwrap();
16031592
writeln!(output, "{{").unwrap();
16041593
{
@@ -1611,7 +1600,8 @@ pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<Vec<(St
16111600
let arg_name = arg
16121601
.name
16131602
.clone()
1614-
.unwrap_or_else(|| format!("arg_{}", i))
1603+
.unwrap_or_else(|| format!("arg_{}", i).into())
1604+
.deref()
16151605
.to_case(Case::Pascal);
16161606
writeln!(output, "args.{arg_name},").unwrap();
16171607
}

0 commit comments

Comments
 (0)