Skip to content

Commit daaaed9

Browse files
committed
simplify code, don't need to store UniqueValue since &'static str is already unique
1 parent 39ca65d commit daaaed9

File tree

1 file changed

+33
-36
lines changed
  • crates/bevy_ecs/src/schedule

1 file changed

+33
-36
lines changed

crates/bevy_ecs/src/schedule/set.rs

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,56 @@ use crate::system::{
1515
ExclusiveSystemParamFunction, IsExclusiveFunctionSystem, IsFunctionSystem, SystemParamFunction,
1616
};
1717

18-
type Interner = RwLock<HashMap<UniqueValueHash, &'static str>>;
18+
type Interner = RwLock<HashMap<UniqueValue, &'static str>>;
1919
static INTERNER: OnceCell<Interner> = OnceCell::new();
2020

2121
/// The [`TypeId`](std::any::TypeId) of a value and its [`DynHash`] output when hashed with the [`DefaultHasher`].
2222
///
2323
/// This should be different for each value.
2424
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
25-
struct UniqueValueHash(TypeId, u64);
25+
struct UniqueValue(TypeId, u64);
26+
27+
/// Returns a reference to the interned `Debug` string of `value`.
28+
///
29+
/// If the string has not been interned, it will be allocated in a [`Box`] and leaked, but
30+
/// subsequent calls with the same `value` will return the same reference.
31+
fn intern_debug<T>(value: &T) -> &'static str
32+
where
33+
T: Debug + DynHash + ?Sized,
34+
{
35+
let mut hasher = DefaultHasher::new();
36+
value.dyn_hash(&mut hasher);
37+
let hash = hasher.finish();
38+
39+
let key = UniqueValue(TypeId::of::<T>(), hash);
40+
let mut map = INTERNER.get_or_init(default).write().unwrap();
41+
let str = *map.entry(key).or_insert_with(|| {
42+
let string = format!("{:?}", value);
43+
let str: &'static str = Box::leak(string.into_boxed_str());
44+
str
45+
});
46+
47+
str
48+
}
2649

2750
/// Identifies a [`Schedule`](super::Schedule).
2851
pub trait ScheduleLabel: DynHash + Debug + Send + Sync + 'static {}
2952

3053
/// A lightweight and printable identifier for a [`Schedule`](super::Schedule).
3154
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
32-
pub struct ScheduleId(UniqueValueHash, &'static str);
55+
pub struct ScheduleId(&'static str);
3356

3457
impl ScheduleId {
3558
/// Returns the [`ScheduleId`] of the schedule.
3659
pub fn of<S: ScheduleLabel + ?Sized>(label: &S) -> ScheduleId {
37-
let mut hasher = DefaultHasher::new();
38-
label.dyn_hash(&mut hasher);
39-
let hash = hasher.finish();
40-
41-
let type_id = TypeId::of::<S>();
42-
43-
let key = UniqueValueHash(type_id, hash);
44-
let mut map = INTERNER.get_or_init(default).write().unwrap();
45-
let str = *map.entry(key).or_insert_with(|| {
46-
let string = format!("{:?}", label);
47-
let str: &'static str = Box::leak(string.into_boxed_str());
48-
str
49-
});
50-
51-
ScheduleId(key, str)
60+
let str = intern_debug(label);
61+
ScheduleId(str)
5262
}
5363
}
5464

5565
impl Debug for ScheduleId {
5666
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57-
f.pad(self.1)
67+
self.0.fmt(f)
5868
}
5969
}
6070

@@ -89,32 +99,19 @@ pub trait SystemSet: DynHash + Debug + Send + Sync + 'static {
8999

90100
/// A lightweight and printable identifier for a [`SystemSet`].
91101
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
92-
pub struct SystemSetId(UniqueValueHash, &'static str);
102+
pub struct SystemSetId(&'static str);
93103

94104
impl SystemSetId {
95105
/// Returns the [`SystemSetId`] of the set.
96106
pub fn of<S: SystemSet + ?Sized>(set: &S) -> SystemSetId {
97-
let mut hasher = DefaultHasher::new();
98-
set.dyn_hash(&mut hasher);
99-
let hash = hasher.finish();
100-
101-
let type_id = TypeId::of::<S>();
102-
103-
let key = UniqueValueHash(type_id, hash);
104-
let mut map = INTERNER.get_or_init(default).write().unwrap();
105-
let str = *map.entry(key).or_insert_with(|| {
106-
let string = format!("{:?}", set);
107-
let str: &'static str = Box::leak(string.into_boxed_str());
108-
str
109-
});
110-
111-
SystemSetId(key, str)
107+
let str = intern_debug(set);
108+
SystemSetId(str)
112109
}
113110
}
114111

115112
impl Debug for SystemSetId {
116113
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117-
f.pad(self.1)
114+
self.0.fmt(f)
118115
}
119116
}
120117

0 commit comments

Comments
 (0)