-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Entity ranges #24102
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
Open
Trashtalk217
wants to merge
21
commits into
bevyengine:main
Choose a base branch
from
Trashtalk217:entity-ranges
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+96
−37
Open
Entity ranges #24102
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
91067aa
add try_alloc and constructors with range
Trashtalk217 5847dbd
fixed clippy
Trashtalk217 676bff0
fix test
Trashtalk217 3f9bd6b
CompressedImageSaver revamp (#23567)
JMS55 0ff7c35
Gate LTC LUTs behind a feature and merge them to a texture array (#24…
beicause 68e5a15
fix: `Handle<T>::from_reflect` incorrectly instantiating regardless o…
makspll 1a21cbe
Revert Screen Space Transmission Gate for Mesh Bind Groups (#24089)
kfc35 739b1e5
Push window resize and scale factor messages to `bevy_window_events` …
ProffDea 71ad427
Make 1-to-1 Relationship clearer in docs (#24116)
amtep bd3d304
perf changes
Trashtalk217 ed98b31
fix
Trashtalk217 be503ac
Merge branch 'main' into entity-ranges
Trashtalk217 3207ed6
Merge branch 'main' into entity-ranges
cart 416a12b
Update crates/bevy_ecs/src/entity/remote_allocator.rs
Trashtalk217 4d1b5dc
Merge branch 'main' of https://github.com/bevyengine/bevy into entity…
Trashtalk217 8194a0d
remove MAX_ENTITIES
Trashtalk217 15d2ba2
Revert "CompressedImageSaver revamp (#23567)"
Trashtalk217 049f13b
addressed review comments, removed Default
Trashtalk217 a6f8d69
Default is back
Trashtalk217 4339a1c
dead code warning
Trashtalk217 38e5677
pls fix
Trashtalk217 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,7 +39,7 @@ use bevy_platform::{ | |
| Arc, | ||
| }, | ||
| }; | ||
| use core::mem::ManuallyDrop; | ||
| use core::{mem::ManuallyDrop, ops::Range}; | ||
| use log::warn; | ||
| use nonmax::NonMaxU32; | ||
|
|
||
|
|
@@ -296,7 +296,7 @@ impl FreeBuffer { | |
| /// making safety for other operations afterward need careful justification. | ||
| /// Otherwise, the compiler will make unsound optimizations. | ||
| #[inline] | ||
| unsafe fn iter(&self, indices: core::ops::Range<u32>) -> FreeBufferIterator<'_> { | ||
| unsafe fn iter(&self, indices: Range<u32>) -> FreeBufferIterator<'_> { | ||
| FreeBufferIterator { | ||
| buffer: self, | ||
| future_buffer_indices: indices, | ||
|
|
@@ -325,7 +325,7 @@ struct FreeBufferIterator<'a> { | |
| /// The part of the buffer we are iterating at the moment. | ||
| current_chunk_slice: core::slice::Iter<'a, Slot>, | ||
| /// The indices in the buffer that are not yet in `current_chunk_slice`. | ||
| future_buffer_indices: core::ops::Range<u32>, | ||
| future_buffer_indices: Range<u32>, | ||
| } | ||
|
|
||
| impl<'a> Iterator for FreeBufferIterator<'a> { | ||
|
|
@@ -737,12 +737,16 @@ impl FreeList { | |
| struct FreshAllocator { | ||
| /// The next value of [`Entity::index`] to give out if needed. | ||
| next_entity_index: AtomicU32, | ||
| max_index: u32, | ||
| } | ||
|
|
||
| impl FreshAllocator { | ||
| /// This exists because it may possibly change depending on platform. | ||
| /// Ex: We may want this to be smaller on 32 bit platforms at some point. | ||
| const MAX_ENTITIES: u32 = u32::MAX; | ||
| pub(crate) fn new(range: Range<u32>) -> Self { | ||
| Self { | ||
| next_entity_index: AtomicU32::new(range.start), | ||
| max_index: range.end, | ||
| } | ||
| } | ||
|
|
||
| /// The total number of indices given out. | ||
| #[inline] | ||
|
|
@@ -759,15 +763,24 @@ impl FreshAllocator { | |
| } | ||
|
|
||
| /// Allocates a fresh [`EntityIndex`]. | ||
| /// This row has never been given out before. | ||
| /// This index has never been given out before. | ||
| /// If no index is available (out of range), than it returns None | ||
| #[inline] | ||
| fn alloc(&self) -> Entity { | ||
| fn alloc(&self) -> Option<Entity> { | ||
| let index = self.next_entity_index.fetch_add(1, Ordering::Relaxed); | ||
| if index == Self::MAX_ENTITIES { | ||
| Self::on_overflow(); | ||
| if index >= self.max_index { | ||
| self.next_entity_index | ||
| .store(self.max_index, Ordering::Relaxed); | ||
| if index == u32::MAX { | ||
| Self::on_overflow(); | ||
| } | ||
| return None; | ||
|
Trashtalk217 marked this conversation as resolved.
|
||
| } | ||
|
Trashtalk217 marked this conversation as resolved.
|
||
|
|
||
| // SAFETY: We just checked that this was not max and we only added 1, so we can't have missed it. | ||
| Entity::from_index(unsafe { EntityIndex::new(NonMaxU32::new_unchecked(index)) }) | ||
| Some(Entity::from_index(unsafe { | ||
| EntityIndex::new(NonMaxU32::new_unchecked(index)) | ||
| })) | ||
| } | ||
|
|
||
| /// Allocates `count` [`EntityIndex`]s. | ||
|
|
@@ -777,7 +790,7 @@ impl FreshAllocator { | |
| let start_new = self.next_entity_index.fetch_add(count, Ordering::Relaxed); | ||
| let new = match start_new | ||
| .checked_add(count) | ||
| .filter(|new| *new < Self::MAX_ENTITIES) | ||
| .filter(|new| *new < self.max_index) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if we could instead say, "Here's the id range you could allocate," so it never panics; it just sometimes doesn't contain all |
||
| { | ||
| Some(new_next_entity_index) => start_new..new_next_entity_index, | ||
| None => Self::on_overflow(), | ||
|
|
@@ -790,7 +803,7 @@ impl FreshAllocator { | |
| /// These rows have never been given out before. | ||
| /// | ||
| /// **NOTE:** Dropping will leak the remaining entity rows! | ||
| pub(super) struct AllocUniqueEntityIndexIterator(core::ops::Range<u32>); | ||
| pub(super) struct AllocUniqueEntityIndexIterator(Range<u32>); | ||
|
|
||
| impl Iterator for AllocUniqueEntityIndexIterator { | ||
| type Item = Entity; | ||
|
|
@@ -824,25 +837,24 @@ struct SharedAllocator { | |
|
|
||
| impl SharedAllocator { | ||
| /// Constructs a [`SharedAllocator`] | ||
| fn new() -> Self { | ||
| fn new(range: Range<u32>) -> Self { | ||
| Self { | ||
| free: FreeList::new(), | ||
| fresh: FreshAllocator { | ||
| next_entity_index: AtomicU32::new(0), | ||
| }, | ||
| fresh: FreshAllocator::new(range), | ||
| is_closed: AtomicBool::new(false), | ||
| } | ||
| } | ||
|
|
||
| /// Allocates a new [`Entity`], reusing a freed index if one exists. | ||
| /// If no more entities can be allocated, this returns None | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// This must not conflict with [`FreeList::free`] calls. | ||
| #[inline] | ||
| unsafe fn alloc(&self) -> Entity { | ||
| unsafe fn try_alloc(&self) -> Option<Entity> { | ||
| // SAFETY: assured by caller | ||
| unsafe { self.free.alloc() }.unwrap_or_else(|| self.fresh.alloc()) | ||
| unsafe { self.free.alloc() }.or_else(|| self.fresh.alloc()) | ||
| } | ||
|
|
||
| /// Allocates a `count` [`Entity`]s, reusing freed indices if they exist. | ||
|
|
@@ -862,10 +874,8 @@ impl SharedAllocator { | |
| /// Allocates a new [`Entity`]. | ||
| /// This will only try to reuse a freed index if it is safe to do so. | ||
| #[inline] | ||
| fn remote_alloc(&self) -> Entity { | ||
| self.free | ||
| .remote_alloc() | ||
| .unwrap_or_else(|| self.fresh.alloc()) | ||
| fn try_remote_alloc(&self) -> Option<Entity> { | ||
| self.free.remote_alloc().or_else(|| self.fresh.alloc()) | ||
| } | ||
|
|
||
| /// Marks the allocator as closed, but it will still function normally. | ||
|
|
@@ -891,28 +901,43 @@ pub(crate) struct Allocator { | |
| /// The local free list. | ||
| /// We use this to amortize the cost of freeing to the shared allocator since that is expensive. | ||
| local_free: Box<ArrayVec<Entity, 128>>, | ||
| /// The index range this allocator operates on | ||
| range: Range<u32>, | ||
| } | ||
|
|
||
| impl Default for Allocator { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| Self::new(0..u32::MAX) | ||
| } | ||
| } | ||
|
|
||
| impl Allocator { | ||
| /// Constructs a new [`Allocator`] | ||
| pub(super) fn new() -> Self { | ||
| pub(super) fn new(range: Range<u32>) -> Self { | ||
| Self { | ||
| shared: Arc::new(SharedAllocator::new()), | ||
| shared: Arc::new(SharedAllocator::new(range.clone())), | ||
| local_free: Box::new(ArrayVec::new()), | ||
| range, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the range this allocator operates on | ||
| pub(super) fn range(&self) -> &Range<u32> { | ||
| &self.range | ||
| } | ||
|
|
||
| /// Allocates a new [`Entity`], reusing a freed index if one exists. | ||
| #[cfg(test)] | ||
| fn alloc(&self) -> Entity { | ||
| self.try_alloc().expect("out of entities") | ||
| } | ||
|
|
||
| /// Allocates a new [`Entity`], reusing a freed index if one exists. | ||
| /// Returns None if no entities are available within the range | ||
| #[inline] | ||
| pub(super) fn alloc(&self) -> Entity { | ||
| pub(super) fn try_alloc(&self) -> Option<Entity> { | ||
| // SAFETY: violating safety requires a `&mut self` to exist, but rust does not allow that. | ||
| unsafe { self.shared.alloc() } | ||
| unsafe { self.shared.try_alloc() } | ||
| } | ||
|
|
||
| /// The total number of indices given out. | ||
|
|
@@ -1049,7 +1074,7 @@ impl RemoteAllocator { | |
| Arc::ptr_eq(&self.shared, &source.shared) | ||
| } | ||
|
|
||
| /// Allocates an entity remotely. | ||
| /// Allocates an entity remotely, panicking if no more entities are available. | ||
| /// | ||
| /// This comes with a major downside: | ||
| /// Because this does not hold reference to the world, the world may be cleared or destroyed before you get a chance to use the result. | ||
|
|
@@ -1058,7 +1083,19 @@ impl RemoteAllocator { | |
| /// Before using the returned values in the world, first check that it is ok with [`EntityAllocator::has_remote_allocator`](super::EntityAllocator::has_remote_allocator). | ||
| #[inline] | ||
| pub fn alloc(&self) -> Entity { | ||
| self.shared.remote_alloc() | ||
| self.shared.try_remote_alloc().expect("out of entities") | ||
| } | ||
|
|
||
| /// Allocates an entity remotely, returning `None` if no more entities are available. | ||
| /// | ||
| /// This comes with a major downside: | ||
| /// Because this does not hold reference to the world, the world may be cleared or destroyed before you get a chance to use the result. | ||
| /// If that happens, these entities will be garbage! | ||
| /// They will not be unique in the world anymore and you should not spawn them! | ||
| /// Before using the returned values in the world, first check that it is ok with [`EntityAllocator::has_remote_allocator`](super::EntityAllocator::has_remote_allocator). | ||
| #[inline] | ||
| pub fn try_alloc(&self) -> Option<Entity> { | ||
| self.shared.try_remote_alloc() | ||
| } | ||
|
|
||
| /// Returns whether or not this [`RemoteAllocator`] is still connected to its source [`EntityAllocator`](super::EntityAllocator). | ||
|
|
@@ -1133,7 +1170,7 @@ mod tests { | |
| #[test] | ||
| fn uniqueness() { | ||
| let mut entities = Vec::with_capacity(2000); | ||
| let mut allocator = Allocator::new(); | ||
| let mut allocator = Allocator::default(); | ||
| entities.extend(allocator.alloc_many(1000)); | ||
|
|
||
| let pre_len = entities.len(); | ||
|
|
@@ -1159,7 +1196,7 @@ mod tests { | |
| /// This test just exists to make sure allocations don't step on each other's toes. | ||
| #[test] | ||
| fn allocation_order_correctness() { | ||
| let mut allocator = Allocator::new(); | ||
| let mut allocator = Allocator::default(); | ||
| let e0 = allocator.alloc(); | ||
| let e1 = allocator.alloc(); | ||
| let e2 = allocator.alloc(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.