203 lines
4.7 KiB
Plaintext
203 lines
4.7 KiB
Plaintext
#! Times what `ecs` does most, over a million entities: spawning them, passing
|
|||
|
|
#! over them by `each`, by chunks, and by `par_each`, against a loop over plain
|
||
|
|
#! arrays, which is as fast as a pass can be; getting and setting a component;
|
||
|
|
#! moving entities between tables; and despawning them.
|
||
|
|
#!
|
||
|
|
#! `blossom build`, then run `ecs.bench`, as an executable: the VM's times
|
||
|
|
#! aren't the program's.
|
||
|
|
|
||
|
|
import std::io;
|
||
|
|
import std::time;
|
||
|
|
import ecs;
|
||
|
|
|
||
|
|
struct pos3 {
|
||
|
|
var x: f32;
|
||
|
|
var y: f32;
|
||
|
|
var z: f32;
|
||
|
|
}
|
||
|
|
|
||
|
|
struct vel3 {
|
||
|
|
var x: f32;
|
||
|
|
var y: f32;
|
||
|
|
var z: f32;
|
||
|
|
}
|
||
|
|
|
||
|
|
struct life {
|
||
|
|
var points: i64;
|
||
|
|
}
|
||
|
|
|
||
|
|
struct marked {}
|
||
|
|
struct tag1 {}
|
||
|
|
struct tag2 {}
|
||
|
|
struct tag3 {}
|
||
|
|
struct tag4 {}
|
||
|
|
struct tag5 {}
|
||
|
|
struct tag6 {}
|
||
|
|
|
||
|
|
# What `each` is given as a type of its own, rather than a lambda, whose calls
|
||
|
|
# go through its address.
|
||
|
|
struct mover : invokable<def(&mut pos3, &vel3): void> {}
|
||
|
|
|
||
|
|
def mover::invoke(&mut this, p: &mut pos3, v: &vel3): void {
|
||
|
|
p.x += v.x;
|
||
|
|
p.y += v.y;
|
||
|
|
p.z += v.z;
|
||
|
|
}
|
||
|
|
|
||
|
|
# How long something took, for each of the things it did.
|
||
|
|
def report(what: strview, started: &instant, things: u64): void {
|
||
|
|
val ns := started.elapsed().in_nanoseconds as f64;
|
||
|
|
println($"{what}: {ns / things as f64} ns each, {ns / 1000000} ms in all");
|
||
|
|
}
|
||
|
|
|
||
|
|
def main(): void {
|
||
|
|
val n: u64 = 1000000;
|
||
|
|
val passes: u64 = 20;
|
||
|
|
var w := world::new();
|
||
|
|
var all := vector::<entity>::new();
|
||
|
|
all.reserve(n);
|
||
|
|
|
||
|
|
var at := instant::now();
|
||
|
|
for i in 0..n {
|
||
|
|
all.append(w.spawn_with(pos3{ x = i as f32 }, vel3{ x = 1, y = 2, z = 3 }));
|
||
|
|
}
|
||
|
|
report("spawn_with of 2 components", &at, n);
|
||
|
|
# Half have another component, so passes go over two tables.
|
||
|
|
for i in 0..n {
|
||
|
|
if i % 2 == 0 {
|
||
|
|
w.set(all[i], life{ points = 1 });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var xs := vector::<pos3>::new();
|
||
|
|
var vs := vector::<vel3>::new();
|
||
|
|
for i in 0..n {
|
||
|
|
xs.append(pos3{ x = i as f32 });
|
||
|
|
vs.append(vel3{ x = 1, y = 2, z = 3 });
|
||
|
|
}
|
||
|
|
at = instant::now();
|
||
|
|
for _ in 0..passes {
|
||
|
|
for i in 0..n {
|
||
|
|
xs[i].x += vs[i].x;
|
||
|
|
xs[i].y += vs[i].y;
|
||
|
|
xs[i].z += vs[i].z;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
report("plain arrays, a pass", &at, n * passes);
|
||
|
|
|
||
|
|
at = instant::now();
|
||
|
|
for _ in 0..passes {
|
||
|
|
w.each(def(p: &mut pos3, v: &vel3) {
|
||
|
|
p.x += v.x;
|
||
|
|
p.y += v.y;
|
||
|
|
p.z += v.z;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
report("each, given a lambda", &at, n * passes);
|
||
|
|
|
||
|
|
at = instant::now();
|
||
|
|
for _ in 0..passes {
|
||
|
|
w.each(mover{});
|
||
|
|
}
|
||
|
|
report("each, given a functor", &at, n * passes);
|
||
|
|
|
||
|
|
at = instant::now();
|
||
|
|
var q := w.query(with::<pos3>().with::<vel3>());
|
||
|
|
for _ in 0..passes {
|
||
|
|
for c in q.chunks() {
|
||
|
|
val ps := c.write::<pos3>();
|
||
|
|
val vels := c.read::<vel3>();
|
||
|
|
for i in 0..c.length {
|
||
|
|
ps[i].x += vels[i].x;
|
||
|
|
ps[i].y += vels[i].y;
|
||
|
|
ps[i].z += vels[i].z;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
report("chunks", &at, n * passes);
|
||
|
|
|
||
|
|
w.par_each(def(p: &mut pos3, v: &vel3) { p.x += 0; });
|
||
|
|
at = instant::now();
|
||
|
|
for _ in 0..passes {
|
||
|
|
w.par_each(def(p: &mut pos3, v: &vel3) {
|
||
|
|
p.x += v.x;
|
||
|
|
p.y += v.y;
|
||
|
|
p.z += v.z;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
report("par_each", &at, n * passes);
|
||
|
|
|
||
|
|
# Each pass moved every pos3 on by 1.
|
||
|
|
var wrong := 0u;
|
||
|
|
for i in 0..n {
|
||
|
|
if w.get::<pos3>(all[i]) is some(&mut p) {
|
||
|
|
if p.x != (i + passes * 4) as f32 {
|
||
|
|
wrong++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if wrong > 0 {
|
||
|
|
println($"{wrong} positions are wrong");
|
||
|
|
}
|
||
|
|
|
||
|
|
var sum: f64 = 0;
|
||
|
|
at = instant::now();
|
||
|
|
for i in 0..n {
|
||
|
|
if w.get::<pos3>(all[i]) is some(&mut p) {
|
||
|
|
sum += p.x as f64;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
report("get, in order", &at, n);
|
||
|
|
|
||
|
|
at = instant::now();
|
||
|
|
for i in 0..n {
|
||
|
|
if w.get::<pos3>(all[(i * 7919) % n]) is some(&mut p) {
|
||
|
|
sum += p.x as f64;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
report("get, in no order", &at, n);
|
||
|
|
|
||
|
|
at = instant::now();
|
||
|
|
for i in 0..n {
|
||
|
|
w.set(all[i], vel3{ x = 2 });
|
||
|
|
}
|
||
|
|
report("set of a component an entity has", &at, n);
|
||
|
|
|
||
|
|
at = instant::now();
|
||
|
|
for i in 0..n / 10 {
|
||
|
|
w.add::<marked>(all[i * 10]);
|
||
|
|
}
|
||
|
|
for i in 0..n / 10 {
|
||
|
|
_ = w.remove::<marked>(all[i * 10]);
|
||
|
|
}
|
||
|
|
report("add and remove of a tag, each a move", &at, n / 10 * 2);
|
||
|
|
|
||
|
|
# Six tags, given by the bits of each entity's index, spread the entities
|
||
|
|
# over 128 tables.
|
||
|
|
for i in 0..n {
|
||
|
|
val e := all[i];
|
||
|
|
if (i & 1) != 0 { w.add::<tag1>(e); }
|
||
|
|
if (i & 2) != 0 { w.add::<tag2>(e); }
|
||
|
|
if (i & 4) != 0 { w.add::<tag3>(e); }
|
||
|
|
if (i & 8) != 0 { w.add::<tag4>(e); }
|
||
|
|
if (i & 16) != 0 { w.add::<tag5>(e); }
|
||
|
|
if (i & 32) != 0 { w.add::<tag6>(e); }
|
||
|
|
}
|
||
|
|
at = instant::now();
|
||
|
|
for _ in 0..passes {
|
||
|
|
w.each(def(p: &mut pos3, v: &vel3) {
|
||
|
|
p.x += v.x;
|
||
|
|
p.y += v.y;
|
||
|
|
p.z += v.z;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
report($"each, over {w.archetypes} tables", &at, n * passes);
|
||
|
|
|
||
|
|
at = instant::now();
|
||
|
|
for i in 0..n {
|
||
|
|
_ = w.despawn(all[i]);
|
||
|
|
}
|
||
|
|
report("despawn", &at, n);
|
||
|
|
|
||
|
|
println($"(sums {sum} {xs[n - 1].x}, {w.count} left)");
|
||
|
|
}
|