-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
debug/ | ||
target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
# MSVC Windows builds of rustc generate these, which store debugging information | ||
*.pdb | ||
|
||
# Spacetime ignore | ||
/.spacetime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "keynote-benchmarks" | ||
version = "0.1.0" | ||
edition.workspace = true | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
spacetimedb = { path = "../../crates/bindings" } | ||
log = "0.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use core::ops::AddAssign; | ||
use spacetimedb::{log_stopwatch::LogStopwatch, rand::Rng, reducer, table, DbContext, ReducerContext, Table}; | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
#[table(name = position, public)] | ||
#[repr(C)] | ||
pub struct Position { | ||
#[primary_key] | ||
#[index(direct)] | ||
id: u32, | ||
x: f32, | ||
y: f32, | ||
z: f32, | ||
} | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
#[table(name = velocity, public)] | ||
#[repr(C)] | ||
pub struct Velocity { | ||
#[primary_key] | ||
#[index(direct)] | ||
id: u32, | ||
dx: f32, | ||
dy: f32, | ||
dz: f32, | ||
} | ||
|
||
impl AddAssign<Velocity> for Position { | ||
fn add_assign(&mut self, vel: Velocity) { | ||
self.x += vel.dx; | ||
self.y += vel.dy; | ||
self.z += vel.dz; | ||
} | ||
} | ||
|
||
#[reducer(init)] | ||
fn init(ctx: &ReducerContext) { | ||
let _stopwatch = LogStopwatch::new("init"); | ||
|
||
// Insert 10^6 randomized positions and velocities, | ||
// but with incrementing and corresponding ids. | ||
let db = ctx.db(); | ||
let mut rng = ctx.rng(); | ||
for id in 0..1_000_000 { | ||
let (x, y, z) = rng.gen(); | ||
let (dx, dy, dz) = rng.gen(); | ||
db.position().insert(Position { id, x, y, z }); | ||
db.velocity().insert(Velocity { id, dx, dy, dz }); | ||
} | ||
} | ||
|
||
#[reducer] | ||
fn update_positions_by_collect(ctx: &ReducerContext) { | ||
let _stopwatch = LogStopwatch::new("update_positions_by_collect"); | ||
|
||
let mut pos_vec = ctx.db.position().iter().collect::<Vec<_>>(); | ||
let mut vel_vec = ctx.db.velocity().iter().collect::<Vec<_>>(); | ||
|
||
pos_vec.sort_unstable_by_key(|pos| pos.id); | ||
vel_vec.sort_unstable_by_key(|vel| vel.id); | ||
|
||
for (pos, vel) in pos_vec.iter_mut().zip(&vel_vec) { | ||
*pos += *vel; | ||
} | ||
|
||
for pos in pos_vec { | ||
ctx.db.position().id().update(pos); | ||
} | ||
} | ||
|
||
#[reducer] | ||
fn roundtrip(ctx: &ReducerContext) { | ||
let _stopwatch = LogStopwatch::new("index_roundtrip"); | ||
ctx.db().velocity().id().find(333_333); | ||
} |