Skip to content

chore: add query and reflection benchmarks #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions assets/benchmarks/function/call.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function bench()
noop()
end
3 changes: 3 additions & 0 deletions assets/benchmarks/function/call.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn bench(){
noop.call();
}
3 changes: 3 additions & 0 deletions assets/benchmarks/function/call_4_args.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function bench()
noop_4_args(1,"asd",{1,2,3}, {asd = 1})
end
3 changes: 3 additions & 0 deletions assets/benchmarks/function/call_4_args.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn bench(){
noop_4_args.call(1,"asd",[1,2,3],#{ asd: 1});
}
20 changes: 20 additions & 0 deletions assets/benchmarks/math/vec_mat_ops.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

reseed()

local matrix = nil
local vector = nil
function pre_bench()
-- generate random 3x3 matrix and 3x1 vec
vector = Vec3.new(random(1,999), random(1,999), random(1,999))
matrix = Mat3.from_cols(
Vec3.new(random(1,999), random(1,999), random(1,999)),
Vec3.new(random(1,999), random(1,999), random(1,999)),
Vec3.new(random(1,999), random(1,999), random(1,999))
)
end

function bench()
local mul = matrix * vector
local add = matrix + vector
local div = vector / matrix
end
19 changes: 19 additions & 0 deletions assets/benchmarks/math/vec_mat_ops.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

reseed.call();

let matrix = ();
let vector = ();
fn pre_bench(){
vector = Vec3.new_.call(random.call(1,999), random.call(1,999), random.call(1,999));
matrix = Mat3.from_cols.call(
Vec3.new_.call(random.call(1,999), random.call(1,999), random.call(1,999)),
Vec3.new_.call(random.call(1,999), random.call(1,999), random.call(1,999)),
Vec3.new_.call(random.call(1,999), random.call(1,999), random.call(1,999))
);
}

fn bench() {
let mul = matrix * vector;
let add = matrix + vector;
let div = vector / matrix;
}
35 changes: 35 additions & 0 deletions assets/benchmarks/query/1000_entities.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
local entity_a = world.spawn()
local entity_b = world.spawn()
local entity_c = world.spawn()

local components = {
types.CompWithDefaultAndComponentData,
types.CompWithFromWorldAndComponentData,
types.SimpleTupleStruct,
types.SimpleEnum,
}

reseed()

for i = 1, 1000 do
local entity = world.spawn()
-- spawn 1000 entities with random components
local left_to_pick = {1,2,3,4}
for j = 1, 3 do
local index = random_int(1, #left_to_pick)
local component = components[left_to_pick[index]]
table.remove(left_to_pick, index)
world.add_default_component(entity, component)
end
end

function bench()
for i,result in pairs(world.query()
:component(types.CompWithFromWorldAndComponentData)
:component(types.SimpleTupleStruct)
:with(types.SimpleEnum)
:without(types.CompWithDefaultAndComponentData)
:build()) do

end
end
34 changes: 34 additions & 0 deletions assets/benchmarks/query/1000_entities.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
let entity_a = world.spawn_.call();
let entity_b = world.spawn_.call();
let entity_c = world.spawn_.call();

let components = [
types.CompWithDefaultAndComponentData,
types.CompWithFromWorldAndComponentData,
types.SimpleTupleStruct,
types.SimpleEnum,
];

reseed.call();

for i in 1..=1000 {
let entity = world.spawn_.call();
// spawn 1000 entities with random components
let left_to_pick = [0, 1, 2, 3];
for j in 1..=3 {
let index = random_int.call(1, left_to_pick.len()) - 1;
let component = components[left_to_pick[index]];
left_to_pick.remove(index);
world.add_default_component.call(entity, component);
};
};

fn bench() {
for (result, i) in world.query.call()
.component.call(types.CompWithFromWorldAndComponentData)
.component.call(types.SimpleTupleStruct)
.with_.call(types.SimpleEnum)
.without.call(types.CompWithDefaultAndComponentData)
.build.call() {
};
}
34 changes: 34 additions & 0 deletions assets/benchmarks/query/100_entities.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local entity_a = world.spawn()
local entity_b = world.spawn()
local entity_c = world.spawn()

local components = {
types.CompWithDefaultAndComponentData,
types.CompWithFromWorldAndComponentData,
types.SimpleTupleStruct,
types.SimpleEnum,
}

reseed()

for i = 1, 100 do
local entity = world.spawn()
local left_to_pick = {1,2,3,4}
for j = 1, 3 do
local index = random_int(1, #left_to_pick)
local component = components[left_to_pick[index]]
table.remove(left_to_pick, index)
world.add_default_component(entity, component)
end
end

function bench()
for i,result in pairs(world.query()
:component(types.CompWithFromWorldAndComponentData)
:component(types.SimpleTupleStruct)
:with(types.SimpleEnum)
:without(types.CompWithDefaultAndComponentData)
:build()) do

end
end
33 changes: 33 additions & 0 deletions assets/benchmarks/query/100_entities.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
let entity_a = world.spawn_.call();
let entity_b = world.spawn_.call();
let entity_c = world.spawn_.call();

let components = [
types.CompWithDefaultAndComponentData,
types.CompWithFromWorldAndComponentData,
types.SimpleTupleStruct,
types.SimpleEnum,
];

reseed.call();

for i in 1..=100 {
let entity = world.spawn_.call();
let left_to_pick = [0, 1, 2, 3];
for j in 1..=3 {
let index = random_int.call(1, left_to_pick.len()) - 1;
let component = components[left_to_pick[index]];
left_to_pick.remove(index);
world.add_default_component.call(entity, component);
};
};

fn bench() {
for (result, i) in world.query.call()
.component.call(types.CompWithFromWorldAndComponentData)
.component.call(types.SimpleTupleStruct)
.with_.call(types.SimpleEnum)
.without.call(types.CompWithDefaultAndComponentData)
.build.call() {
};
}
34 changes: 34 additions & 0 deletions assets/benchmarks/query/10_entities.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local entity_a = world.spawn()
local entity_b = world.spawn()
local entity_c = world.spawn()

local components = {
types.CompWithDefaultAndComponentData,
types.CompWithFromWorldAndComponentData,
types.SimpleTupleStruct,
types.SimpleEnum,
}

reseed()

for i = 1, 10 do
local entity = world.spawn()
local left_to_pick = {1,2,3,4}
for j = 1, 3 do
local index = random_int(1, #left_to_pick)
local component = components[left_to_pick[index]]
table.remove(left_to_pick, index)
world.add_default_component(entity, component)
end
end

function bench()
for i,result in pairs(world.query()
:component(types.CompWithFromWorldAndComponentData)
:component(types.SimpleTupleStruct)
:with(types.SimpleEnum)
:without(types.CompWithDefaultAndComponentData)
:build()) do

end
end
33 changes: 33 additions & 0 deletions assets/benchmarks/query/10_entities.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
let entity_a = world.spawn_.call();
let entity_b = world.spawn_.call();
let entity_c = world.spawn_.call();

let components = [
types.CompWithDefaultAndComponentData,
types.CompWithFromWorldAndComponentData,
types.SimpleTupleStruct,
types.SimpleEnum,
];

reseed.call();

for i in 1..=10 {
let entity = world.spawn_.call();
let left_to_pick = [0, 1, 2, 3];
for j in 1..=3 {
let index = random_int.call(1, left_to_pick.len()) - 1;
let component = components[left_to_pick[index]];
left_to_pick.remove(index);
world.add_default_component.call(entity, component);
};
};

fn bench() {
for (result, i) in world.query.call()
.component.call(types.CompWithFromWorldAndComponentData)
.component.call(types.SimpleTupleStruct)
.with_.call(types.SimpleEnum)
.without.call(types.CompWithDefaultAndComponentData)
.build.call() {
};
}
52 changes: 52 additions & 0 deletions assets/benchmarks/reflection/10.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-- create a dynamic component with n depth of fields
local Component = world.register_new_component("DeepComponent");
local new_entity = world.spawn();

reseed()

local keys = {
"_0",
"my_key",
"longish_key"
}

-- Recursively build a single nested table using the pre-selected step_keys.
local function make_path(step_keys, index)
if index > #step_keys then
return {}
end
local key = step_keys[index]
return { [key] = make_path(step_keys, index + 1) }
end


local current_reference = nil
local steps = 10
local step_keys = {}
function pre_bench()
step_keys = {}
current_reference = nil
-- Choose keys for every step.
for i = 1, steps do
local key = keys[random_int(1, #keys)]
step_keys[i] = key
end

-- Build the nested table.
local instance = make_path(step_keys, 1)
world.remove_component(new_entity, Component)
world.insert_component(new_entity, Component, construct(types.DynamicComponent, {
data = instance
}))
current_reference = world.get_component(new_entity, Component)
end

function bench()
-- reference into random key into current_reference steps times
local reference = current_reference.data
local current_step = 1
while current_step <= steps do
reference = reference[step_keys[current_step]]
current_step = current_step + 1
end
end
52 changes: 52 additions & 0 deletions assets/benchmarks/reflection/10.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
let Component = world.register_new_component.call("DeepComponent");
let new_entity = world.spawn_.call();

reseed.call();

let keys = [
"_0",
"my_key",
"longish_key"
];

fn make_path(step_keys, index) {
if index >= step_keys.len() {
return #{};
};
let key = step_keys[index];
let nested = make_path(step_keys, index + 1);
let obj = #{};
obj.set(key,nested);
return obj;
};

let current_reference = ();
let steps = 10;
let step_keys = [];

fn pre_bench() {
step_keys = [];
current_reference = ();

// Choose keys for every step.
for i in 0 .. steps {
let key = keys[random_int.call(0, keys.len() - 1)];
step_keys.push(key);
};

// Build the nested table.
let instance = make_path(step_keys, 0);
world.remove_component.call(new_entity, Component);
world.insert_component.call(new_entity, Component, construct.call(types.DynamicComponent, #{ "data": instance }));
current_reference = world.get_component.call(new_entity, Component);
};

fn bench() {
// reference into random key into current_reference steps times
let reference = current_reference.data;
let current_step = 0;
while current_step < steps {
reference = reference[step_keys[current_step]];
current_step += 1;
};
};
Loading
Loading