Skip to content
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

#14034 improvements #2

Merged
merged 7 commits into from
Jul 27, 2024
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ ios_simulator = ["bevy_internal/ios_simulator"]
# Enable built in global state machines
bevy_state = ["bevy_internal/bevy_state"]

# Enables source location tracking for change detection, which can assist with debugging
track_change_detection = ["bevy_internal/track_change_detection"]

[dependencies]
bevy_internal = { path = "crates/bevy_internal", version = "0.14.0-dev", default-features = false }

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ categories = ["game-engines", "data-structures"]
rust-version = "1.77.0"

[features]
default = ["bevy_reflect"]
trace = []
multi_threaded = ["bevy_tasks/multi_threaded", "arrayvec"]
bevy_debug_stepping = []
default = ["bevy_reflect"]
serialize = ["dep:serde"]
track_change_detection = []

[dependencies]
bevy_ptr = { path = "../bevy_ptr", version = "0.14.0-dev" }
Expand Down
49 changes: 39 additions & 10 deletions crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ use crate::{
};
use bevy_ptr::{ConstNonNull, OwningPtr};
use bevy_utils::all_tuples;
use std::any::TypeId;
use std::ptr::NonNull;
#[cfg(feature = "track_change_detection")]
use std::panic::Location;
use std::{any::TypeId, ptr::NonNull};

/// The `Bundle` trait enables insertion and removal of [`Component`]s from an entity.
///
Expand Down Expand Up @@ -387,7 +388,7 @@ impl BundleInfo {
table_row: TableRow,
change_tick: Tick,
bundle: T,
caller: core::panic::Location<'static>,
#[cfg(feature = "track_change_detection")] caller: &'static Location<'static>,
) {
// NOTE: get_components calls this closure on each component in "bundle order".
// bundle_info.component_ids are also in "bundle order"
Expand All @@ -404,10 +405,22 @@ impl BundleInfo {
let status = unsafe { bundle_component_status.get_status(bundle_component) };
match status {
ComponentStatus::Added => {
column.initialize(table_row, component_ptr, change_tick, caller);
column.initialize(
table_row,
component_ptr,
change_tick,
#[cfg(feature = "track_change_detection")]
caller,
);
}
ComponentStatus::Mutated => {
column.replace(table_row, component_ptr, change_tick, caller);
column.replace(
table_row,
component_ptr,
change_tick,
#[cfg(feature = "track_change_detection")]
caller,
);
}
}
}
Expand All @@ -416,7 +429,13 @@ impl BundleInfo {
// SAFETY: If component_id is in self.component_ids, BundleInfo::new requires that
// a sparse set exists for the component.
unsafe { sparse_sets.get_mut(component_id).debug_checked_unwrap() };
sparse_set.insert(entity, component_ptr, change_tick, caller);
sparse_set.insert(
entity,
component_ptr,
change_tick,
#[cfg(feature = "track_change_detection")]
caller,
);
}
}
bundle_component += 1;
Expand Down Expand Up @@ -648,7 +667,8 @@ impl<'w> BundleInserter<'w> {
let add_bundle = self.add_bundle.as_ref();
let table = self.table.as_mut();
let archetype = self.archetype.as_mut();
let caller = *core::panic::Location::caller();
#[cfg(feature = "track_change_detection")]
let caller = Location::caller();

let (new_archetype, new_location) = match &mut self.result {
InsertBundleResult::SameArchetype => {
Expand All @@ -666,6 +686,7 @@ impl<'w> BundleInserter<'w> {
location.table_row,
self.change_tick,
bundle,
#[cfg(feature = "track_change_detection")]
caller,
);

Expand Down Expand Up @@ -705,6 +726,7 @@ impl<'w> BundleInserter<'w> {
result.table_row,
self.change_tick,
bundle,
#[cfg(feature = "track_change_detection")]
caller,
);

Expand Down Expand Up @@ -785,6 +807,7 @@ impl<'w> BundleInserter<'w> {
move_result.new_row,
self.change_tick,
bundle,
#[cfg(feature = "track_change_detection")]
caller,
);

Expand Down Expand Up @@ -887,7 +910,7 @@ impl<'w> BundleSpawner<'w> {
&mut self,
entity: Entity,
bundle: T,
caller: core::panic::Location<'static>,
#[cfg(feature = "track_change_detection")] caller: &'static Location<'static>,
) -> EntityLocation {
let table = self.table.as_mut();
let archetype = self.archetype.as_mut();
Expand All @@ -910,6 +933,7 @@ impl<'w> BundleSpawner<'w> {
table_row,
self.change_tick,
bundle,
#[cfg(feature = "track_change_detection")]
caller,
);
entities.set(entity.index(), location);
Expand Down Expand Up @@ -938,12 +962,17 @@ impl<'w> BundleSpawner<'w> {
pub unsafe fn spawn<T: Bundle>(
&mut self,
bundle: T,
caller: core::panic::Location<'static>,
#[cfg(feature = "track_change_detection")] caller: &'static Location<'static>,
) -> Entity {
let entity = self.entities().alloc();
// SAFETY: entity is allocated (but non-existent), `T` matches this BundleInfo's type
unsafe {
self.spawn_non_existent(entity, bundle, caller);
self.spawn_non_existent(
entity,
bundle,
#[cfg(feature = "track_change_detection")]
caller,
);
}
entity
}
Expand Down
Loading
Loading