Skip to content

Commit b46fc85

Browse files
re0312alice-i-cecile
authored andcommitted
Fix regression on the get/get_mut/get_not_found (#19505)
# Objective - Partial fix #19504 - As more features were added to Bevy ECS, certain core hot-path function calls exceeded LLVM's automatic inlining threshold, leading to significant performance regressions in some cases. ## Solution - inline more functions. ## Performance This brought nearly 3x improvement in Windows bench (using Sander's testing code) --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent 383b351 commit b46fc85

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

crates/bevy_ecs/src/world/entity_fetch.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ unsafe impl WorldEntityFetch for Entity {
200200
type Mut<'w> = EntityWorldMut<'w>;
201201
type DeferredMut<'w> = EntityMut<'w>;
202202

203+
#[inline]
203204
unsafe fn fetch_ref(
204205
self,
205206
cell: UnsafeWorldCell<'_>,
@@ -209,6 +210,7 @@ unsafe impl WorldEntityFetch for Entity {
209210
Ok(unsafe { EntityRef::new(ecell) })
210211
}
211212

213+
#[inline]
212214
unsafe fn fetch_mut(
213215
self,
214216
cell: UnsafeWorldCell<'_>,
@@ -223,6 +225,7 @@ unsafe impl WorldEntityFetch for Entity {
223225
Ok(unsafe { EntityWorldMut::new(world, self, location) })
224226
}
225227

228+
#[inline]
226229
unsafe fn fetch_deferred_mut(
227230
self,
228231
cell: UnsafeWorldCell<'_>,
@@ -242,20 +245,23 @@ unsafe impl<const N: usize> WorldEntityFetch for [Entity; N] {
242245
type Mut<'w> = [EntityMut<'w>; N];
243246
type DeferredMut<'w> = [EntityMut<'w>; N];
244247

248+
#[inline]
245249
unsafe fn fetch_ref(
246250
self,
247251
cell: UnsafeWorldCell<'_>,
248252
) -> Result<Self::Ref<'_>, EntityDoesNotExistError> {
249253
<&Self>::fetch_ref(&self, cell)
250254
}
251255

256+
#[inline]
252257
unsafe fn fetch_mut(
253258
self,
254259
cell: UnsafeWorldCell<'_>,
255260
) -> Result<Self::Mut<'_>, EntityMutableFetchError> {
256261
<&Self>::fetch_mut(&self, cell)
257262
}
258263

264+
#[inline]
259265
unsafe fn fetch_deferred_mut(
260266
self,
261267
cell: UnsafeWorldCell<'_>,
@@ -273,6 +279,7 @@ unsafe impl<const N: usize> WorldEntityFetch for &'_ [Entity; N] {
273279
type Mut<'w> = [EntityMut<'w>; N];
274280
type DeferredMut<'w> = [EntityMut<'w>; N];
275281

282+
#[inline]
276283
unsafe fn fetch_ref(
277284
self,
278285
cell: UnsafeWorldCell<'_>,
@@ -290,6 +297,7 @@ unsafe impl<const N: usize> WorldEntityFetch for &'_ [Entity; N] {
290297
Ok(refs)
291298
}
292299

300+
#[inline]
293301
unsafe fn fetch_mut(
294302
self,
295303
cell: UnsafeWorldCell<'_>,
@@ -316,6 +324,7 @@ unsafe impl<const N: usize> WorldEntityFetch for &'_ [Entity; N] {
316324
Ok(refs)
317325
}
318326

327+
#[inline]
319328
unsafe fn fetch_deferred_mut(
320329
self,
321330
cell: UnsafeWorldCell<'_>,
@@ -335,6 +344,7 @@ unsafe impl WorldEntityFetch for &'_ [Entity] {
335344
type Mut<'w> = Vec<EntityMut<'w>>;
336345
type DeferredMut<'w> = Vec<EntityMut<'w>>;
337346

347+
#[inline]
338348
unsafe fn fetch_ref(
339349
self,
340350
cell: UnsafeWorldCell<'_>,
@@ -349,6 +359,7 @@ unsafe impl WorldEntityFetch for &'_ [Entity] {
349359
Ok(refs)
350360
}
351361

362+
#[inline]
352363
unsafe fn fetch_mut(
353364
self,
354365
cell: UnsafeWorldCell<'_>,
@@ -372,6 +383,7 @@ unsafe impl WorldEntityFetch for &'_ [Entity] {
372383
Ok(refs)
373384
}
374385

386+
#[inline]
375387
unsafe fn fetch_deferred_mut(
376388
self,
377389
cell: UnsafeWorldCell<'_>,
@@ -391,6 +403,7 @@ unsafe impl WorldEntityFetch for &'_ EntityHashSet {
391403
type Mut<'w> = EntityHashMap<EntityMut<'w>>;
392404
type DeferredMut<'w> = EntityHashMap<EntityMut<'w>>;
393405

406+
#[inline]
394407
unsafe fn fetch_ref(
395408
self,
396409
cell: UnsafeWorldCell<'_>,
@@ -404,6 +417,7 @@ unsafe impl WorldEntityFetch for &'_ EntityHashSet {
404417
Ok(refs)
405418
}
406419

420+
#[inline]
407421
unsafe fn fetch_mut(
408422
self,
409423
cell: UnsafeWorldCell<'_>,
@@ -417,6 +431,7 @@ unsafe impl WorldEntityFetch for &'_ EntityHashSet {
417431
Ok(refs)
418432
}
419433

434+
#[inline]
420435
unsafe fn fetch_deferred_mut(
421436
self,
422437
cell: UnsafeWorldCell<'_>,

crates/bevy_ecs/src/world/entity_ref.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ impl<'w> EntityMut<'w> {
454454
/// - `cell` must have permission to mutate every component of the entity.
455455
/// - No accesses to any of the entity's components may exist
456456
/// at the same time as the returned [`EntityMut`].
457+
#[inline]
457458
pub(crate) unsafe fn new(cell: UnsafeEntityCell<'w>) -> Self {
458459
Self { cell }
459460
}
@@ -1002,6 +1003,7 @@ impl<'w> From<EntityWorldMut<'w>> for EntityMut<'w> {
10021003
}
10031004

10041005
impl<'a> From<&'a mut EntityWorldMut<'_>> for EntityMut<'a> {
1006+
#[inline]
10051007
fn from(entity: &'a mut EntityWorldMut<'_>) -> Self {
10061008
// SAFETY: `EntityWorldMut` guarantees exclusive access to the entire world.
10071009
unsafe { EntityMut::new(entity.as_unsafe_entity_cell()) }
@@ -1114,6 +1116,7 @@ impl<'w> EntityWorldMut<'w> {
11141116
}
11151117
}
11161118

1119+
#[inline(always)]
11171120
fn as_unsafe_entity_cell_readonly(&self) -> UnsafeEntityCell<'_> {
11181121
self.assert_not_despawned();
11191122
UnsafeEntityCell::new(
@@ -1122,6 +1125,8 @@ impl<'w> EntityWorldMut<'w> {
11221125
self.location,
11231126
)
11241127
}
1128+
1129+
#[inline(always)]
11251130
fn as_unsafe_entity_cell(&mut self) -> UnsafeEntityCell<'_> {
11261131
self.assert_not_despawned();
11271132
UnsafeEntityCell::new(
@@ -1130,6 +1135,8 @@ impl<'w> EntityWorldMut<'w> {
11301135
self.location,
11311136
)
11321137
}
1138+
1139+
#[inline(always)]
11331140
fn into_unsafe_entity_cell(self) -> UnsafeEntityCell<'w> {
11341141
self.assert_not_despawned();
11351142
UnsafeEntityCell::new(
@@ -1168,6 +1175,7 @@ impl<'w> EntityWorldMut<'w> {
11681175
}
11691176

11701177
/// Gets read-only access to all of the entity's components.
1178+
#[inline]
11711179
pub fn as_readonly(&self) -> EntityRef<'_> {
11721180
EntityRef::from(self)
11731181
}
@@ -1179,6 +1187,7 @@ impl<'w> EntityWorldMut<'w> {
11791187
}
11801188

11811189
/// Gets non-structural mutable access to all of the entity's components.
1190+
#[inline]
11821191
pub fn as_mutable(&mut self) -> EntityMut<'_> {
11831192
EntityMut::from(self)
11841193
}

0 commit comments

Comments
 (0)