Optimize label comparisons and remove restrictions on types that can be labels#5377
Closed
joseph-gio wants to merge 22 commits intobevyengine:mainfrom
Closed
Optimize label comparisons and remove restrictions on types that can be labels#5377joseph-gio wants to merge 22 commits intobevyengine:mainfrom
joseph-gio wants to merge 22 commits intobevyengine:mainfrom
Conversation
92ea11e to
6b57afc
Compare
2605865 to
1118a6b
Compare
joseph-gio
commented
Jul 26, 2022
7 tasks
Closed
042899d to
b5d7611
Compare
aa3a462 to
3df37e4
Compare
Member
Author
|
This PR is now at a point where I'm happy with it. I'll get CI passing once we yeet stringly-typed labels. |
255a88c to
4fa714d
Compare
Member
Author
|
On second thought, I realized that I can implement string-labels in this design. This PR is no longer blocked, not sure what that last CI failure is about though. |
7bd4448 to
239f8dc
Compare
MIRI is buggy so we can't use fn pointers for comparisons
f584e12 to
07c7b1c
Compare
8864321 to
989b916
Compare
Member
Author
|
I've iterated a lot on this PR. In that time, the design and precise motivations have changed considerably and the history has gotten quite messy. I'm going to close this out and open a new PR. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Objective
Solution
Labelto return au64value that distinguishes between labels of the same type.&mut Formatterfn pointer.TypeIdand formatter behind the same pointer, shrinking the stack size of labels to 16 bytes (down from 24).u64can get stored on the stack for free.Examples
Basic labels.
Complex labels require boxing and interning.
Notes
Labeltraits are no longer object safe, but this is probably a good thing since it makes it easier to catch obsolete instances ofBox<dyn Label>.LabelIdshould always be used instead.parking_lotandindexmapas dependencies forbevy_utils.indexmaponly has one dependency (hashbrown), which is already a dependency ofbevy_utils.Benchmarks
If we unnecessarily intern most of the labels in the benchmark, we see that it's still significantly faster than before #4957, which is the PR that originally removed boxed labels.
Changelog
as_str()andtype_id()from label traits.data()andfmt()to label traits.Labelderive macros.Migration Guide
The
Labelfamily of traits (SystemLabel,StageLabel, etc) is no longer object safe. Any use ofBox<dyn *Label>should be replaced with*LabelId- this family of types serves the same purpose but isCopyand does not usually require allocations. Any use of&dyn *Labelshould be replaced withimpl *Labelif possible, or*LabelIdif you cannot use generics.For manual implementers of this family of traits, the implementation has been changed entirely. Now, comparisons are done using the method
.data(), which returns au64used to distinguish labels of the same type. Formatting is done through the associated fnfmt(), which writes to an&mut Formatterand is allowed to depend on runtime values.