-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathcircles.rs
More file actions
174 lines (152 loc) · 4.45 KB
/
circles.rs
File metadata and controls
174 lines (152 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! STDB module used for benchmarks based on "realistic" workloads we are focusing in improving.
use crate::Load;
use spacetimedb::{log, ReducerContext, SpacetimeType, Table, Timestamp};
use std::hint::black_box;
#[derive(SpacetimeType, Debug, Clone, Copy)]
pub struct Vector2 {
pub x: f32,
pub y: f32,
}
// ---------- schemas ----------
#[spacetimedb::table(name = entity)]
pub struct Entity {
#[auto_inc]
#[primary_key]
pub id: u32,
pub position: Vector2,
pub mass: u32,
}
impl Entity {
pub fn new(id: u32, x: f32, y: f32, mass: u32) -> Self {
Self {
id,
mass,
position: Vector2 { x, y },
}
}
}
#[spacetimedb::table(name = circle)]
pub struct Circle {
#[primary_key]
pub entity_id: u32,
#[index(btree)]
pub player_id: u32,
pub direction: Vector2,
pub magnitude: f32,
pub last_split_time: Timestamp,
}
impl Circle {
pub fn new(entity_id: u32, player_id: u32, x: f32, y: f32, magnitude: f32, last_split_time: Timestamp) -> Self {
Self {
entity_id,
player_id,
direction: Vector2 { x, y },
magnitude,
last_split_time,
}
}
}
#[spacetimedb::table(name = food)]
pub struct Food {
#[primary_key]
pub entity_id: u32,
}
impl Food {
pub fn new(entity_id: u32) -> Self {
Self { entity_id }
}
}
fn mass_to_radius(mass: u32) -> f32 {
(mass as f32).sqrt()
}
fn is_overlapping(entity1: &Entity, entity2: &Entity) -> bool {
let entity1_radius = mass_to_radius(entity1.mass);
let entity2_radius = mass_to_radius(entity2.mass);
let distance =
((entity1.position.x - entity2.position.x).powi(2) + (entity1.position.y - entity2.position.y).powi(2)).sqrt();
distance < entity1_radius.max(entity2_radius)
}
// ---------- insert bulk ----------
#[spacetimedb::reducer]
pub fn insert_bulk_entity(ctx: &ReducerContext, count: u32) {
for id in 0..count {
ctx.db
.entity()
.insert(Entity::new(0, id as f32, (id + 5) as f32, id * 5));
}
log::info!("INSERT ENTITY: {count}");
}
#[spacetimedb::reducer]
pub fn insert_bulk_circle(ctx: &ReducerContext, count: u32) {
for id in 0..count {
ctx.db.circle().insert(Circle::new(
id,
id,
id as f32,
(id + 5) as f32,
(id * 5) as f32,
ctx.timestamp,
));
}
log::info!("INSERT CIRCLE: {count}");
}
#[spacetimedb::reducer]
pub fn insert_bulk_food(ctx: &ReducerContext, count: u32) {
for id in 1..=count {
ctx.db.food().insert(Food::new(id));
}
log::info!("INSERT FOOD: {count}");
}
// Simulate
// ```
// SELECT * FROM Circle, Entity, Food
// ```
#[spacetimedb::reducer]
pub fn cross_join_all(ctx: &ReducerContext, expected: u32) {
let mut count = 0;
for _circle in ctx.db.circle().iter() {
for _entity in ctx.db.entity().iter() {
for _food in ctx.db.food().iter() {
count += 1;
}
}
}
log::info!("CROSS JOIN ALL: {expected}, processed: {count}");
}
// Simulate
// ```
// SELECT * FROM Circle JOIN ENTITY USING(entity_id), Food JOIN ENTITY USING(entity_id)
// ```
#[spacetimedb::reducer]
pub fn cross_join_circle_food(ctx: &ReducerContext, expected: u32) {
let mut count = 0;
for circle in ctx.db.circle().iter() {
let Some(circle_entity) = ctx.db.entity().id().find(circle.entity_id) else {
continue;
};
for food in ctx.db.food().iter() {
count += 1;
let food_entity = ctx
.db
.entity()
.id()
.find(food.entity_id)
.unwrap_or_else(|| panic!("Entity not found: {})", food.entity_id));
black_box(is_overlapping(&circle_entity, &food_entity));
}
}
log::info!("CROSS JOIN CIRCLE FOOD: {expected}, processed: {count}");
}
#[spacetimedb::reducer]
pub fn init_game_circles(ctx: &ReducerContext, initial_load: u32) {
let load = Load::new(initial_load);
insert_bulk_food(ctx, load.initial_load);
insert_bulk_entity(ctx, load.initial_load);
insert_bulk_circle(ctx, load.small_table);
}
#[spacetimedb::reducer]
pub fn run_game_circles(ctx: &ReducerContext, initial_load: u32) {
let load = Load::new(initial_load);
cross_join_circle_food(ctx, initial_load * load.small_table);
cross_join_all(ctx, initial_load * initial_load * load.small_table);
}