Skip to content
Closed
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
5 changes: 5 additions & 0 deletions ixa-bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,8 @@ harness = false
name = "sample_entity_scaling"
path = "criterion/sample_entity_scaling.rs"
harness = false

[[bench]]
name = "bulk_add_people"
path = "criterion/bulk_add_people.rs"
harness = false
90 changes: 90 additions & 0 deletions ixa-bench/criterion/bulk_add_people.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::hint::black_box;

use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use ixa::context::Context;
use ixa::prelude::*;

const N_PEOPLE: usize = 10_000;

define_entity!(Person);
define_property!(struct Age(u8), Person);
define_property!(struct Prop1(u32), Person, default_const = Prop1(0));
define_property!(struct Prop2(u32), Person, default_const = Prop2(0));
define_property!(struct Prop3(u32), Person, default_const = Prop3(0));
define_property!(struct Prop4(u32), Person, default_const = Prop4(0));
define_property!(struct Prop5(u32), Person, default_const = Prop5(0));
define_property!(struct Prop6(u32), Person, default_const = Prop6(0));
define_property!(struct Prop7(u32), Person, default_const = Prop7(0));
define_property!(struct Prop8(u32), Person, default_const = Prop8(0));
define_property!(struct Prop9(u32), Person, default_const = Prop9(0));

pub fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("bulk_add_people");

group.bench_function("add_10000_one_by_one", |bencher| {
bencher.iter_batched(
Context::new,
|mut context| {
let mut ids = Vec::with_capacity(N_PEOPLE);
for i in 0..N_PEOPLE {
let i_u32 = i as u32;
ids.push(
context
.add_entity((
Age((i % 100) as u8),
Prop1(i_u32 + 1),
Prop2((i_u32 % 10_000) + 1),
Prop3((i_u32 % 100_000) + 1),
Prop4((i_u32 % 1_000_000) + 1),
Prop5((i_u32.wrapping_mul(3)) + 1),
Prop6((i_u32.wrapping_mul(5)) + 1),
Prop7((i_u32.wrapping_mul(7)) + 1),
Prop8((i_u32.wrapping_mul(11)) + 1),
Prop9((i_u32.wrapping_mul(13)) + 1),
))
.unwrap(),
);
}
black_box(context.get_entity_count::<Person>());
black_box(ids.len());
},
BatchSize::SmallInput,
);
});

group.bench_function("add_10000_in_bulk", |bencher| {
bencher.iter_batched(
Context::new,
|mut context| {
let ids = context
.add_entities::<Person, _>(
(0..N_PEOPLE)
.map(|i| {
let i_u32 = i as u32;
(
Age((i % 100) as u8),
Prop1(i_u32 + 1),
Prop2((i_u32 % 10_000) + 1),
Prop3((i_u32 % 100_000) + 1),
Prop4((i_u32 % 1_000_000) + 1),
Prop5((i_u32.wrapping_mul(3)) + 1),
Prop6((i_u32.wrapping_mul(5)) + 1),
Prop7((i_u32.wrapping_mul(7)) + 1),
Prop8((i_u32.wrapping_mul(11)) + 1),
Prop9((i_u32.wrapping_mul(13)) + 1),
)
})
.collect(),
)
.unwrap();
black_box(ids.try_len().unwrap());
},
BatchSize::SmallInput,
);
});

group.finish();
}

criterion_group!(bulk_add_people_benches, criterion_benchmark);
criterion_main!(bulk_add_people_benches);
1 change: 1 addition & 0 deletions mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ depends = "install-nightly"
run = [
# cargo check compiles everything without the final step of code generation
# --all-targets ensures we include --lib --bins --tests --benches --examples
"rustc +nightly --version",
"cargo check --all-targets --workspace --all-features",
"cargo +nightly fmt --check",
"cargo +nightly clippy --workspace --all-targets --all-features -- -D warnings",
Expand Down
19 changes: 19 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ impl Context {
}
}

pub(crate) fn has_event_handlers<E: IxaEvent + Copy + 'static>(&self) -> bool {
self.event_handlers.contains_key(&TypeId::of::<E>())
}

/// Add a plan to the future event list at the specified time in the normal
/// phase
///
Expand Down Expand Up @@ -945,6 +949,21 @@ mod tests {
assert_eq!(*obs_data2.borrow(), 2);
}

#[test]
fn has_event_handlers_reflects_subscriptions() {
let mut context = Context::new();
assert!(!context.has_event_handlers::<Event1>());
assert!(!context.has_event_handlers::<Event2>());

context.subscribe_to_event::<Event1>(|_, _| {});
assert!(context.has_event_handlers::<Event1>());
assert!(!context.has_event_handlers::<Event2>());

context.subscribe_to_event::<Event2>(|_, _| {});
assert!(context.has_event_handlers::<Event1>());
assert!(context.has_event_handlers::<Event2>());
}

#[test]
fn subscribe_after_event() {
let mut context = Context::new();
Expand Down
Loading
Loading