Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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: 0 additions & 3 deletions benches/benches/bevy_ecs/empty_archetypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ fn empty_archetypes(criterion: &mut Criterion) {
schedule.add_systems(iter);
});
add_archetypes(&mut world, archetype_count);
world.clear_entities();
let mut e = world.spawn_empty();
e.insert(A::<0>(1.0));
e.insert(A::<1>(1.0));
Expand Down Expand Up @@ -197,7 +196,6 @@ fn empty_archetypes(criterion: &mut Criterion) {
schedule.add_systems(for_each);
});
add_archetypes(&mut world, archetype_count);
world.clear_entities();
let mut e = world.spawn_empty();
e.insert(A::<0>(1.0));
e.insert(A::<1>(1.0));
Expand Down Expand Up @@ -228,7 +226,6 @@ fn empty_archetypes(criterion: &mut Criterion) {
schedule.add_systems(par_for_each);
});
add_archetypes(&mut world, archetype_count);
world.clear_entities();
let mut e = world.spawn_empty();
e.insert(A::<0>(1.0));
e.insert(A::<1>(1.0));
Expand Down
2 changes: 2 additions & 0 deletions benches/benches/bevy_ecs/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ mod fragmentation;
mod iteration;
mod observers;
mod param;
mod resources;
mod scheduling;
mod world;

criterion_main!(
bundles::benches,
change_detection::benches,
components::benches,
resources::benches,
empty_archetypes::benches,
entity_cloning::benches,
events::benches,
Expand Down
69 changes: 69 additions & 0 deletions benches/benches/bevy_ecs/resources.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::{alloc::Layout, hint::black_box, ptr::NonNull};

use benches::bench;
use bevy_ecs::{
change_detection::MaybeLocation,
component::{ComponentCloneBehavior, ComponentDescriptor, StorageType},
prelude::*,
ptr::OwningPtr,
};
use criterion::{criterion_group, Criterion};

criterion_group!(benches, get, get_mut, insert_remove);

fn create_world() -> World {
let mut world = World::new();
for _ in 0..500 {
// SAFETY: Uses zero-sized value, never drops
unsafe {
let resource_id =
world.register_component_with_descriptor(ComponentDescriptor::new_with_layout(
"",
StorageType::SparseSet,
Layout::new::<()>(),
None,
true,
ComponentCloneBehavior::Default,
None,
));
world.insert_resource_by_id(
resource_id,
OwningPtr::new(NonNull::dangling()),
MaybeLocation::caller(),
);
}
}
world
}

#[derive(Resource)]
struct R;

pub fn get(criterion: &mut Criterion) {
let mut world = create_world();
world.insert_resource(R);
criterion.bench_function(bench!("get"), |bencher| {
bencher.iter(|| world.get_resource::<R>());
});
}

pub fn get_mut(criterion: &mut Criterion) {
let mut world = create_world();
world.insert_resource(R);
criterion.bench_function(bench!("get_mut"), |bencher| {
bencher.iter(|| {
black_box(world.get_resource_mut::<R>());
});
});
}

pub fn insert_remove(criterion: &mut Criterion) {
let mut world = create_world();
criterion.bench_function(bench!("insert_remove"), |bencher| {
bencher.iter(|| {
world.insert_resource(R);
black_box(&mut world);
world.remove_resource::<R>()
});
});
}
2 changes: 1 addition & 1 deletion crates/bevy_ecs/macros/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn derive_resource(input: TokenStream) -> TokenStream {
}
});

let storage = storage_path(&bevy_ecs_path, StorageTy::Table);
let storage = storage_path(&bevy_ecs_path, StorageTy::SparseSet);

let on_add_path = None;
let on_remove_path = None;
Expand Down
24 changes: 12 additions & 12 deletions crates/bevy_ecs/src/component/constants.rs
Comment thread
Trashtalk217 marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Constant components included in every world.

/// `usize` for the [`Add`](crate::lifecycle::Add) component used in lifecycle observers.
pub const ADD: usize = 0;
/// `usize` for the [`Insert`](crate::lifecycle::Insert) component used in lifecycle observers.
pub const INSERT: usize = 1;
/// `usize` for the [`Discard`](crate::lifecycle::Discard) component used in lifecycle observers.
pub const DISCARD: usize = 2;
/// `usize` for the [`Remove`](crate::lifecycle::Remove) component used in lifecycle observers.
pub const REMOVE: usize = 3;
/// `usize` for [`Despawn`](crate::lifecycle::Despawn) component used in lifecycle observers.
pub const DESPAWN: usize = 4;
/// `usize` of the [`IsResource`](crate::resource::IsResource) component used to mark entities with resources.
pub const IS_RESOURCE: usize = 5;
/// `u32` for the [`Add`](crate::lifecycle::Add) component used in lifecycle observers.
pub const ADD: u32 = 0;
/// `u32` for the [`Insert`](crate::lifecycle::Insert) component used in lifecycle observers.
pub const INSERT: u32 = 1;
/// `u32` for the [`Discard`](crate::lifecycle::Discard) component used in lifecycle observers.
pub const DISCARD: u32 = 2;
/// `u32` for the [`Remove`](crate::lifecycle::Remove) component used in lifecycle observers.
pub const REMOVE: u32 = 3;
/// `u32` for [`Despawn`](crate::lifecycle::Despawn) component used in lifecycle observers.
pub const DESPAWN: u32 = 4;
/// `u32` of the [`IsResource`](crate::resource::IsResource) component used to mark entities with resources.
pub const IS_RESOURCE: u32 = 5;
Loading