Skip to content

Commit 2cccac4

Browse files
authored
C# module benchmarks (#1679)
1 parent df5ef6c commit 2cccac4

File tree

13 files changed

+1107
-10
lines changed

13 files changed

+1107
-10
lines changed

crates/bench/src/spacetime_module.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ use crate::{
1717

1818
lazy_static::lazy_static! {
1919
pub static ref BENCHMARKS_MODULE: CompiledModule = {
20-
// Temporarily add CARGO_TARGET_DIR override to avoid conflicts with main target dir.
21-
// Otherwise for some reason Cargo will mark all dependencies with build scripts as
22-
// fresh - but only if running benchmarks (if modules are built in release mode).
23-
// See https://github.com/clockworklabs/SpacetimeDB/issues/401.
24-
std::env::set_var("CARGO_TARGET_DIR", concat!(env!("CARGO_MANIFEST_DIR"), "/target"));
25-
let module = CompiledModule::compile("benchmarks", CompilationMode::Release);
26-
std::env::remove_var("CARGO_TARGET_DIR");
27-
module
20+
if std::env::var_os("STDB_BENCH_CS").is_some() {
21+
CompiledModule::compile("benchmarks-cs", CompilationMode::Release)
22+
} else {
23+
// Temporarily add CARGO_TARGET_DIR override to avoid conflicts with main target dir.
24+
// Otherwise for some reason Cargo will mark all dependencies with build scripts as
25+
// fresh - but only if running benchmarks (if modules are built in release mode).
26+
// See https://github.com/clockworklabs/SpacetimeDB/issues/401.
27+
std::env::set_var("CARGO_TARGET_DIR", concat!(env!("CARGO_MANIFEST_DIR"), "/target"));
28+
let module = CompiledModule::compile("benchmarks", CompilationMode::Release);
29+
std::env::remove_var("CARGO_TARGET_DIR");
30+
module
31+
}
2832
};
2933
}
3034

crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-csharp/Codegen/Module.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public static void Main() {
448448
public static void __describe_module__(SpacetimeDB.Internal.BytesSink d) => SpacetimeDB.Internal.Module.__describe_module__(d);
449449
450450
[UnmanagedCallersOnly(EntryPoint = "__call_reducer__")]
451-
public static short __call_reducer__(
451+
public static SpacetimeDB.Internal.Errno __call_reducer__(
452452
uint id,
453453
ulong sender_0,
454454
ulong sender_1,

modules/benchmarks-cs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin
2+
obj

modules/benchmarks-cs/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This C# source code is manually derived from `../benchmarks/src/` and is supposed to be functionally equivalent.
2+
3+
Do not add new types or functionality here that is not present in the Rust version, because they're compared against each other.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<!-- ignore naming conventions, we're trying to match Rust 1:1 -->
9+
<NoWarn>$(NoWarn);CS8981;IDE1006</NoWarn>
10+
</PropertyGroup>
11+
12+
<!--
13+
Use local package sources instead of published ones.
14+
This makes integration test somewhat differ from production configuration, but
15+
at least it simplifies workflow for editing and testing C# code itself.
16+
-->
17+
<ItemGroup>
18+
<ProjectReference Include="../../crates/bindings-csharp/Codegen/Codegen.csproj" OutputItemType="Analyzer" />
19+
<ProjectReference Include="../../crates/bindings-csharp/Runtime/Runtime.csproj" />
20+
</ItemGroup>
21+
22+
</Project>

modules/benchmarks-cs/circles.cs

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
2+
using SpacetimeDB;
3+
4+
namespace Benchmarks;
5+
public static partial class circles
6+
{
7+
[SpacetimeDB.Type]
8+
public partial struct Vector2(float x, float y)
9+
{
10+
public float x = x;
11+
public float y = y;
12+
}
13+
14+
[SpacetimeDB.Table]
15+
public partial struct Entity(uint id, float x, float y, uint mass)
16+
{
17+
[SpacetimeDB.Column(ColumnAttrs.PrimaryKeyAuto)]
18+
public uint id = id;
19+
public Vector2 position = new(x, y);
20+
public uint mass = mass;
21+
}
22+
23+
[SpacetimeDB.Table]
24+
public partial struct Circle(uint entity_id, uint player_id, float x, float y, float magnitude)
25+
{
26+
[SpacetimeDB.Column(ColumnAttrs.PrimaryKey)]
27+
public uint entity_id = entity_id;
28+
29+
[SpacetimeDB.Column(ColumnAttrs.Indexed)]
30+
public uint player_id = player_id;
31+
32+
public Vector2 direction = new(x, y);
33+
public float magnitude = magnitude;
34+
public ulong last_split_time = (ulong)(DateTimeOffset.UtcNow.Ticks / 10);
35+
}
36+
37+
[SpacetimeDB.Table]
38+
public partial struct Food(uint entity_id)
39+
{
40+
[SpacetimeDB.Column(ColumnAttrs.PrimaryKey)]
41+
public uint entity_id = entity_id;
42+
}
43+
44+
public static float MassToRadius(uint mass)
45+
{
46+
return (float)Math.Sqrt(mass);
47+
}
48+
49+
public static bool IsOverlapping(Entity entity1, Entity entity2)
50+
{
51+
float entity1_radius = MassToRadius(entity1.mass);
52+
float entity2_radius = MassToRadius(entity2.mass);
53+
float distance = (float)
54+
Math.Sqrt(
55+
Math.Pow(entity1.position.x - entity2.position.x, 2)
56+
+ Math.Pow(entity1.position.y - entity2.position.y, 2)
57+
);
58+
return distance < Math.Max(entity1_radius, entity2_radius);
59+
}
60+
61+
[SpacetimeDB.Reducer]
62+
public static void insert_bulk_entity(uint count)
63+
{
64+
for (uint id = 0; id < count; id++)
65+
{
66+
new Entity(0, id, id + 5, id * 5).Insert();
67+
}
68+
Runtime.Log($"INSERT ENTITY: {count}");
69+
}
70+
71+
[SpacetimeDB.Reducer]
72+
public static void insert_bulk_circle(uint count)
73+
{
74+
for (uint id = 0; id < count; id++)
75+
{
76+
new Circle(id, id, id, id + 5, id * 5).Insert();
77+
}
78+
Runtime.Log($"INSERT CIRCLE: {count}");
79+
}
80+
81+
[SpacetimeDB.Reducer]
82+
public static void insert_bulk_food(uint count)
83+
{
84+
for (uint id = 1; id <= count; id++)
85+
{
86+
new Food(id).Insert();
87+
}
88+
Runtime.Log($"INSERT FOOD: {count}");
89+
}
90+
91+
[SpacetimeDB.Reducer]
92+
public static void cross_join_all(uint expected)
93+
{
94+
uint count = 0;
95+
foreach (Circle circle in Circle.Iter())
96+
{
97+
foreach (Entity entity in Entity.Iter())
98+
{
99+
foreach (Food food in Food.Iter())
100+
{
101+
count++;
102+
}
103+
}
104+
}
105+
106+
Runtime.Log($"CROSS JOIN ALL: {expected}, processed: {count}");
107+
}
108+
109+
[SpacetimeDB.Reducer]
110+
public static void cross_join_circle_food(uint expected)
111+
{
112+
uint count = 0;
113+
foreach (Circle circle in Circle.Iter())
114+
{
115+
if (Entity.FindByid(circle.entity_id) is not { } circle_entity)
116+
{
117+
continue;
118+
}
119+
120+
foreach (Food food in Food.Iter())
121+
{
122+
count++;
123+
Entity food_entity =
124+
Entity.FindByid(food.entity_id)
125+
?? throw new Exception($"Entity not found: {food.entity_id}");
126+
Bench.BlackBox(IsOverlapping(circle_entity, food_entity));
127+
}
128+
}
129+
130+
Runtime.Log($"CROSS JOIN CIRCLE FOOD: {expected}, processed: {count}");
131+
}
132+
133+
[SpacetimeDB.Reducer]
134+
public static void init_game_circles(uint initial_load)
135+
{
136+
Load load = new(initial_load);
137+
138+
insert_bulk_food(load.initial_load);
139+
insert_bulk_entity(load.initial_load);
140+
insert_bulk_circle(load.small_table);
141+
}
142+
143+
[SpacetimeDB.Reducer]
144+
public static void run_game_circles(uint initial_load)
145+
{
146+
Load load = new(initial_load);
147+
148+
cross_join_circle_food(initial_load * load.small_table);
149+
cross_join_all(initial_load * initial_load * load.small_table);
150+
}
151+
}

0 commit comments

Comments
 (0)