Skip to content

Commit

Permalink
use Location instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
aevyrie committed Jun 26, 2024
1 parent 1c1af8f commit 9ac216d
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 105 deletions.
24 changes: 12 additions & 12 deletions crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ impl BundleInfo {
table_row: TableRow,
change_tick: Tick,
bundle: T,
caller: String,
caller: core::panic::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,15 +404,10 @@ 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.clone(),
);
column.initialize(table_row, component_ptr, change_tick, caller);
}
ComponentStatus::Mutated => {
column.replace(table_row, component_ptr, change_tick, caller.clone());
column.replace(table_row, component_ptr, change_tick, caller);
}
}
}
Expand All @@ -421,7 +416,7 @@ 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.clone());
sparse_set.insert(entity, component_ptr, change_tick, caller);
}
}
bundle_component += 1;
Expand Down Expand Up @@ -642,17 +637,18 @@ impl<'w> BundleInserter<'w> {
/// `entity` must currently exist in the source archetype for this inserter. `location`
/// must be `entity`'s location in the archetype. `T` must match this [`BundleInfo`]'s type
#[inline]
#[track_caller]
pub(crate) unsafe fn insert<T: DynamicBundle>(
&mut self,
entity: Entity,
location: EntityLocation,
bundle: T,
caller: String,
) -> EntityLocation {
let bundle_info = self.bundle_info.as_ref();
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();

let (new_archetype, new_location) = match &mut self.result {
InsertBundleResult::SameArchetype => {
Expand Down Expand Up @@ -891,7 +887,7 @@ impl<'w> BundleSpawner<'w> {
&mut self,
entity: Entity,
bundle: T,
caller: String,
caller: core::panic::Location<'static>,
) -> EntityLocation {
let table = self.table.as_mut();
let archetype = self.archetype.as_mut();
Expand Down Expand Up @@ -939,7 +935,11 @@ impl<'w> BundleSpawner<'w> {
/// # Safety
/// `T` must match this [`BundleInfo`]'s type
#[inline]
pub unsafe fn spawn<T: Bundle>(&mut self, bundle: T, caller: String) -> Entity {
pub unsafe fn spawn<T: Bundle>(
&mut self,
bundle: T,
caller: core::panic::Location<'static>,
) -> Entity {
let entity = self.entities().alloc();
// SAFETY: entity is allocated (but non-existent), `T` matches this BundleInfo's type
unsafe {
Expand Down
53 changes: 29 additions & 24 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub trait DetectChanges {
fn last_changed(&self) -> Tick;

/// The location that last caused this to change.
fn changed_by(&self) -> &str;
fn changed_by(&self) -> core::panic::Location<'static>;
}

/// Types that implement reliable change detection.
Expand Down Expand Up @@ -286,8 +286,8 @@ macro_rules! change_detection_impl {
}

#[inline]
fn changed_by(&self) -> &str {
self.caller
fn changed_by(&self) -> core::panic::Location<'static> {
*self.caller
}
}

Expand Down Expand Up @@ -318,14 +318,14 @@ macro_rules! change_detection_mut_impl {
#[track_caller]
fn set_changed(&mut self) {
*self.ticks.changed = self.ticks.this_run;
*self.caller = core::panic::Location::caller().to_string();
*self.caller = *core::panic::Location::caller();
}

#[inline]
#[track_caller]
fn set_last_changed(&mut self, last_changed: Tick) {
*self.ticks.changed = last_changed;
*self.caller = core::panic::Location::caller().to_string();
*self.caller = *core::panic::Location::caller();
}

#[inline]
Expand All @@ -339,7 +339,7 @@ macro_rules! change_detection_mut_impl {
#[track_caller]
fn deref_mut(&mut self) -> &mut Self::Target {
self.set_changed();
*self.caller = format!("{:?}", std::panic::Location::caller());
*self.caller = *core::panic::Location::caller();
self.value
}
}
Expand Down Expand Up @@ -518,7 +518,7 @@ impl<'w> From<TicksMut<'w>> for Ticks<'w> {
pub struct Res<'w, T: ?Sized + Resource> {
pub(crate) value: &'w T,
pub(crate) ticks: Ticks<'w>,
pub(crate) caller: &'w str,
pub(crate) caller: &'w core::panic::Location<'static>,
}

impl<'w, T: Resource> Res<'w, T> {
Expand Down Expand Up @@ -581,7 +581,7 @@ impl_debug!(Res<'w, T>, Resource);
pub struct ResMut<'w, T: ?Sized + Resource> {
pub(crate) value: &'w mut T,
pub(crate) ticks: TicksMut<'w>,
pub(crate) caller: &'w mut String,
pub(crate) caller: &'w mut core::panic::Location<'static>,
}

impl<'w, 'a, T: Resource> IntoIterator for &'a ResMut<'w, T>
Expand Down Expand Up @@ -641,7 +641,7 @@ impl<'w, T: Resource> From<ResMut<'w, T>> for Mut<'w, T> {
pub struct NonSendMut<'w, T: ?Sized + 'static> {
pub(crate) value: &'w mut T,
pub(crate) ticks: TicksMut<'w>,
pub(crate) caller: &'w mut String,
pub(crate) caller: &'w mut core::panic::Location<'static>,
}

change_detection_impl!(NonSendMut<'w, T>, T,);
Expand Down Expand Up @@ -688,7 +688,7 @@ impl<'w, T: 'static> From<NonSendMut<'w, T>> for Mut<'w, T> {
pub struct Ref<'w, T: ?Sized> {
pub(crate) value: &'w T,
pub(crate) ticks: Ticks<'w>,
pub(crate) caller: &'w str,
pub(crate) caller: &'w core::panic::Location<'static>,
}

impl<'w, T: ?Sized> Ref<'w, T> {
Expand Down Expand Up @@ -726,7 +726,7 @@ impl<'w, T: ?Sized> Ref<'w, T> {
changed: &'w Tick,
last_run: Tick,
this_run: Tick,
caller: &'w str,
caller: &'w core::panic::Location<'static>,
) -> Ref<'w, T> {
Ref {
value,
Expand Down Expand Up @@ -818,7 +818,7 @@ impl_debug!(Ref<'w, T>,);
pub struct Mut<'w, T: ?Sized> {
pub(crate) value: &'w mut T,
pub(crate) ticks: TicksMut<'w>,
pub(crate) caller: &'w mut String,
pub(crate) caller: &'w mut core::panic::Location<'static>,
}

impl<'w, T: ?Sized> Mut<'w, T> {
Expand All @@ -843,7 +843,7 @@ impl<'w, T: ?Sized> Mut<'w, T> {
last_changed: &'w mut Tick,
last_run: Tick,
this_run: Tick,
caller: &'w mut String,
caller: &'w mut core::panic::Location<'static>,
) -> Self {
Self {
value,
Expand Down Expand Up @@ -909,7 +909,7 @@ impl_debug!(Mut<'w, T>,);
pub struct MutUntyped<'w> {
pub(crate) value: PtrMut<'w>,
pub(crate) ticks: TicksMut<'w>,
pub(crate) caller: &'w mut String,
pub(crate) caller: &'w mut core::panic::Location<'static>,
}

impl<'w> MutUntyped<'w> {
Expand Down Expand Up @@ -1024,8 +1024,8 @@ impl<'w> DetectChanges for MutUntyped<'w> {
}

#[inline]
fn changed_by(&self) -> &str {
self.caller
fn changed_by(&self) -> core::panic::Location<'static> {
*self.caller
}
}

Expand All @@ -1036,14 +1036,14 @@ impl<'w> DetectChangesMut for MutUntyped<'w> {
#[track_caller]
fn set_changed(&mut self) {
*self.ticks.changed = self.ticks.this_run;
*self.caller = core::panic::Location::caller().to_string();
*self.caller = *core::panic::Location::caller();
}

#[inline]
#[track_caller]
fn set_last_changed(&mut self, last_changed: Tick) {
*self.ticks.changed = last_changed;
*self.caller = core::panic::Location::caller().to_string();
*self.caller = *core::panic::Location::caller();
}

#[inline]
Expand Down Expand Up @@ -1206,10 +1206,11 @@ mod tests {
this_run: Tick::new(4),
};
let mut res = R {};
let mut caller = *core::panic::Location::caller();
let res_mut = ResMut {
value: &mut res,
ticks,
caller: &mut "".into(),
caller: &mut caller,
};

let into_mut: Mut<R> = res_mut.into();
Expand All @@ -1226,7 +1227,7 @@ mod tests {
changed: Tick::new(3),
};
let mut res = R {};
let mut caller = String::new();
let mut caller = *core::panic::Location::caller();

let val = Mut::new(
&mut res,
Expand Down Expand Up @@ -1254,10 +1255,11 @@ mod tests {
this_run: Tick::new(4),
};
let mut res = R {};
let mut caller = *core::panic::Location::caller();
let non_send_mut = NonSendMut {
value: &mut res,
ticks,
caller: &mut "".into(),
caller: &mut caller,
};

let into_mut: Mut<R> = non_send_mut.into();
Expand Down Expand Up @@ -1286,10 +1288,11 @@ mod tests {
};

let mut outer = Outer(0);
let mut caller = *core::panic::Location::caller();
let ptr = Mut {
value: &mut outer,
ticks,
caller: &mut "".into(),
caller: &mut caller,
};
assert!(!ptr.is_changed());

Expand Down Expand Up @@ -1372,10 +1375,11 @@ mod tests {
};

let mut value: i32 = 5;
let mut caller = *core::panic::Location::caller();
let value = MutUntyped {
value: PtrMut::from(&mut value),
ticks,
caller: &mut "".into(),
caller: &mut caller,
};

let reflect_from_ptr = <ReflectFromPtr as FromType<i32>>::from_type();
Expand Down Expand Up @@ -1406,10 +1410,11 @@ mod tests {
this_run: Tick::new(4),
};
let mut c = C {};
let mut caller = *core::panic::Location::caller();
let mut_typed = Mut {
value: &mut c,
ticks,
caller: &mut "".into(),
caller: &mut caller,
};

let into_mut: MutUntyped = mut_typed.into();
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};
use bevy_ptr::{ThinSlicePtr, UnsafeCellDeref};
use bevy_utils::all_tuples;
use std::{cell::UnsafeCell, marker::PhantomData, ops::Deref};
use std::{cell::UnsafeCell, marker::PhantomData};

/// Types that can be fetched from a [`World`] using a [`Query`].
///
Expand Down Expand Up @@ -1026,7 +1026,7 @@ pub struct RefFetch<'w, T> {
ThinSlicePtr<'w, UnsafeCell<T>>,
ThinSlicePtr<'w, UnsafeCell<Tick>>,
ThinSlicePtr<'w, UnsafeCell<Tick>>,
ThinSlicePtr<'w, UnsafeCell<String>>,
ThinSlicePtr<'w, UnsafeCell<core::panic::Location<'static>>>,
)>,
// T::STORAGE_TYPE = StorageType::SparseSet
sparse_set: Option<&'w ComponentSparseSet>,
Expand Down Expand Up @@ -1215,7 +1215,7 @@ pub struct WriteFetch<'w, T> {
ThinSlicePtr<'w, UnsafeCell<T>>,
ThinSlicePtr<'w, UnsafeCell<Tick>>,
ThinSlicePtr<'w, UnsafeCell<Tick>>,
ThinSlicePtr<'w, UnsafeCell<String>>,
ThinSlicePtr<'w, UnsafeCell<core::panic::Location<'static>>>,
)>,
// T::STORAGE_TYPE = StorageType::SparseSet
sparse_set: Option<&'w ComponentSparseSet>,
Expand Down Expand Up @@ -1326,7 +1326,8 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
// SAFETY: The caller ensures `table_row` is in range.
let added = unsafe { added_ticks.get(table_row.as_usize()) };
// SAFETY: The caller ensures `table_row` is in range.
let changed = unsafe { changed_ticks.get(table_row.as_usize()) }; // SAFETY: The caller ensures `table_row` is in range.
let changed = unsafe { changed_ticks.get(table_row.as_usize()) };
// SAFETY: The caller ensures `table_row` is in range.
let caller = unsafe { callers.get(table_row.as_usize()) };

Mut {
Expand Down
22 changes: 17 additions & 5 deletions crates/bevy_ecs/src/storage/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct ResourceData<const SEND: bool> {
type_name: String,
id: ArchetypeComponentId,
origin_thread_id: Option<ThreadId>,
caller: UnsafeCell<String>,
caller: UnsafeCell<core::panic::Location<'static>>,
}

impl<const SEND: bool> Drop for ResourceData<SEND> {
Expand Down Expand Up @@ -110,7 +110,13 @@ impl<const SEND: bool> ResourceData<SEND> {
/// If `SEND` is false, this will panic if a value is present and is not accessed from the
/// original thread it was inserted in.
#[inline]
pub(crate) fn get_with_ticks(&self) -> Option<(Ptr<'_>, TickCells<'_>, &UnsafeCell<String>)> {
pub(crate) fn get_with_ticks(
&self,
) -> Option<(
Ptr<'_>,
TickCells<'_>,
&UnsafeCell<core::panic::Location<'static>>,
)> {
self.is_present().then(|| {
self.validate_access();
(
Expand Down Expand Up @@ -211,7 +217,13 @@ impl<const SEND: bool> ResourceData<SEND> {
/// original thread it was inserted from.
#[inline]
#[must_use = "The returned pointer to the removed component should be used or dropped"]
pub(crate) fn remove(&mut self) -> Option<(OwningPtr<'_>, ComponentTicks, String)> {
pub(crate) fn remove(
&mut self,
) -> Option<(
OwningPtr<'_>,
ComponentTicks,
core::panic::Location<'static>,
)> {
if !self.is_present() {
return None;
}
Expand All @@ -222,7 +234,7 @@ impl<const SEND: bool> ResourceData<SEND> {
let res = unsafe { self.data.swap_remove_and_forget_unchecked(Self::ROW) };

// SAFETY: This function is being called through an exclusive mutable reference to Self
let caller = unsafe { std::mem::take(self.caller.deref_mut()) };
let caller = unsafe { *self.caller.deref_mut() };

// SAFETY: This function is being called through an exclusive mutable reference to Self, which
// makes it sound to read these ticks.
Expand Down Expand Up @@ -342,7 +354,7 @@ impl<const SEND: bool> Resources<SEND> {
type_name: String::from(component_info.name()),
id: f(),
origin_thread_id: None,
caller: UnsafeCell::new("init".into())
caller: UnsafeCell::new(*core::panic::Location::caller())
}
})
}
Expand Down
Loading

0 comments on commit 9ac216d

Please sign in to comment.