Skip to content

Commit 8c8e771

Browse files
Benchmark remote allocation (#22659)
# Objective After #18670, we have a `RemoteAllocator`, but we don't have benchmarks for it compared to the non-remote allocator. This PR just adds those benchmarks. I don't know if we actually want these benchmarks, but it seems reasonable to have, and it took no time to make, so I figured I'd put it out there. ## Solution Add `entity_allocator_allocate_fresh_remote` and `entity_allocator_allocate_reused_remote` benchmark groups. ## Testing - CI, benchmarks --- ## Showcase ```txt entity_allocator_allocate_fresh/10000_entities 1.00 22.8±0.29µs ? ?/sec entity_allocator_allocate_fresh/100_entities 1.00 227.9±6.37ns ? ?/sec entity_allocator_allocate_fresh/1_entities 1.00 6.2±3.83ns ? ?/sec entity_allocator_allocate_fresh_bulk/10000_entities 1.00 19.9±0.25µs ? ?/sec entity_allocator_allocate_fresh_bulk/100_entities 1.00 227.5±6.95ns ? ?/sec entity_allocator_allocate_fresh_bulk/1_entities 1.00 11.5±4.69ns ? ?/sec entity_allocator_allocate_fresh_remote/10000_entities 1.00 19.4±0.32µs ? ?/sec entity_allocator_allocate_fresh_remote/100_entities 1.00 174.2±3.63ns ? ?/sec entity_allocator_allocate_fresh_remote/1_entities 1.00 3.5±3.02ns ? ?/sec entity_allocator_allocate_reused/10000_entities 1.00 21.5±0.37µs ? ?/sec entity_allocator_allocate_reused/100_entities 1.00 233.3±11.77ns ? ?/sec entity_allocator_allocate_reused/1_entities 1.00 8.3±3.70ns ? ?/sec entity_allocator_allocate_reused_bulk/10000_entities 1.00 20.4±0.64µs ? ?/sec entity_allocator_allocate_reused_bulk/100_entities 1.00 261.5±45.59ns ? ?/sec entity_allocator_allocate_reused_bulk/1_entities 1.00 19.7±10.77ns ? ?/sec entity_allocator_allocate_reused_remote/10000_entities 1.00 77.9±1.53µs ? ?/sec entity_allocator_allocate_reused_remote/100_entities 1.00 774.9±16.28ns ? ?/sec entity_allocator_allocate_reused_remote/1_entities 1.00 7.3±3.60ns ? ?/sec ``` Long story short, remote allocation is a little over 3 times slower than non-remote. All things considered, I think that's pretty good.
1 parent f929d06 commit 8c8e771

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

benches/benches/bevy_ecs/world/entity_allocator.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,57 @@ pub fn entity_allocator_benches(criterion: &mut Criterion) {
128128
}
129129

130130
group.finish();
131+
132+
let mut group = criterion.benchmark_group("entity_allocator_allocate_fresh_remote");
133+
group.warm_up_time(core::time::Duration::from_millis(500));
134+
group.measurement_time(core::time::Duration::from_secs(4));
135+
136+
for entity_count in ENTITY_COUNTS {
137+
group.bench_function(format!("{entity_count}_entities"), |bencher| {
138+
bencher.iter_batched_ref(
139+
|| {
140+
let world = World::new();
141+
world.entity_allocator().build_remote_allocator()
142+
},
143+
|remote| {
144+
for _ in 0..entity_count {
145+
let entity = remote.alloc();
146+
black_box(entity);
147+
}
148+
},
149+
BatchSize::SmallInput,
150+
);
151+
});
152+
}
153+
154+
group.finish();
155+
156+
let mut group = criterion.benchmark_group("entity_allocator_allocate_reused_remote");
157+
group.warm_up_time(core::time::Duration::from_millis(500));
158+
group.measurement_time(core::time::Duration::from_secs(4));
159+
160+
for entity_count in ENTITY_COUNTS {
161+
group.bench_function(format!("{entity_count}_entities"), |bencher| {
162+
bencher.iter_batched_ref(
163+
|| {
164+
let mut world = World::new();
165+
let mut entities =
166+
Vec::from_iter(world.entity_allocator().alloc_many(entity_count));
167+
entities
168+
.drain(..)
169+
.for_each(|e| world.entity_allocator_mut().free(e));
170+
world.entity_allocator().build_remote_allocator()
171+
},
172+
|remote| {
173+
for _ in 0..entity_count {
174+
let entity = remote.alloc();
175+
black_box(entity);
176+
}
177+
},
178+
BatchSize::SmallInput,
179+
);
180+
});
181+
}
182+
183+
group.finish();
131184
}

crates/bevy_ecs/src/entity/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ impl EntityAllocator {
715715

716716
/// Builds a new remote allocator that hooks into this [`EntityAllocator`].
717717
/// This is useful when you need to allocate entities without holding a reference to the world (like in async).
718-
pub fn build_remote_allocator(&mut self) -> RemoteAllocator {
718+
pub fn build_remote_allocator(&self) -> RemoteAllocator {
719719
RemoteAllocator::new(&self.inner)
720720
}
721721

0 commit comments

Comments
 (0)