Skip to content
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

Replace all labels with interned labels #7762

Merged
merged 55 commits into from
Oct 25, 2023

Conversation

geieredgar
Copy link
Contributor

@geieredgar geieredgar commented Feb 20, 2023

Objective

First of all, this PR took heavy inspiration from #7760 and #5715. It intends to also fix #5569, but with a slightly different approach.

This also fixes #9335 by reexporting DynEq.

Solution

The advantage of this API is that we can intern a value without allocating for zero-sized-types and for enum variants that have no fields. This PR does this automatically in the SystemSet and ScheduleLabel derive macros for unit structs and fieldless enum variants. So this should cover many internal and external use cases of SystemSet and ScheduleLabel. In these optimal use cases, no memory will be allocated.

  • The interning returns a Interned<dyn SystemSet>, which is just a wrapper around a &'static dyn SystemSet.
  • Hash and Eq are implemented in terms of the pointer value of the reference, similar to my first approach of anonymous system sets in run_if for SystemConfigs via anonymous system sets #7676.
  • Therefore, Interned<T> does not implement Borrow<T>, only Deref.
  • The debug output of Interned<T> is the same as the interned value.

Edit:

  • AppLabel is now also interned and the old derive_label/define_label macros were replaced with the new interning implementation.
  • Anonymous set ids are reused for different Schedules, reducing the amount of leaked memory.

Pros

  • InternedSystemSet and InternedScheduleLabel behave very similar to the current BoxedSystemSet and BoxedScheduleLabel, but can be copied without an allocation.
  • Many use cases don't allocate at all.
  • Very fast lookups and comparisons when using InternedSystemSet and InternedScheduleLabel.
  • The intern module might be usable in other areas.
  • Interned{ScheduleLabel, SystemSet, AppLabel} does implement {ScheduleLabel, SystemSet, AppLabel}, increasing ergonomics.

Cons

  • Implementors of SystemSet and ScheduleLabel still need to implement Hash and Eq (and Clone) for it to work.

Changelog

Added

  • Added intern module to bevy_utils.
  • Added reexports of DynEq to bevy_ecs and bevy_app.

Changed

  • Replaced BoxedSystemSet and BoxedScheduleLabel with InternedSystemSet and InternedScheduleLabel.
  • Replaced impl AsRef<dyn ScheduleLabel> with impl ScheduleLabel.
  • Replaced AppLabelId with InternedAppLabel.
  • Changed AppLabel to use Debug for error messages.
  • Changed AppLabel to use interning.
  • Changed define_label/derive_label to use interning.
  • Replaced define_boxed_label/derive_boxed_label with define_label/derive_label.
  • Changed anonymous set ids to be only unique inside a schedule, not globally.
  • Made interned label types implement their label trait.

Removed

  • Removed define_boxed_label and derive_boxed_label.

Migration guide

  • Replace BoxedScheduleLabel and Box<dyn ScheduleLabel> with InternedScheduleLabel or Interned<dyn ScheduleLabel>.
  • Replace BoxedSystemSet and Box<dyn SystemSet> with InternedSystemSet or Interned<dyn SystemSet>.
  • Replace AppLabelId with InternedAppLabel or Interned<dyn AppLabel>.
  • Types manually implementing ScheduleLabel, AppLabel or SystemSet need to implement:
    • dyn_hash directly instead of implementing DynHash
    • as_dyn_eq
  • Pass labels to World::try_schedule_scope, World::schedule_scope, World::try_run_schedule. World::run_schedule, Schedules::remove, Schedules::remove_entry, Schedules::contains, Schedules::get and Schedules::get_mut by value instead of by reference.

@JoJoJet JoJoJet self-requested a review February 20, 2023 14:50
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Performance A change motivated by improving speed, memory usage or compile times C-Usability A targeted quality-of-life change that makes Bevy easier to use X-Controversial There is active debate or serious implications around merging this PR labels Feb 20, 2023
@alice-i-cecile
Copy link
Member

Neat! I've added Controversial to all three of these PRs, to make sure the ECS SMEs consciously choose between them.

Copy link
Member

@JoJoJet JoJoJet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've scanned over this quickly, and it definitely seems like a promising approach. Very similar to #7760 but potentially preferable.

Just for clarity, what are the differences between SystemSet::dyn_intern, Intern::intern, and IntoInterned::into_interned? Is it possible to simplify or combine any of these methods some way?

@geieredgar
Copy link
Contributor Author

geieredgar commented Jul 22, 2023

@JoJoJet I've replaced SystemSet::dyn_intern, Intern::intern and IntoInterned::into_interned with the standard Into<Interned<T>>::into.

The functions that took impl AsRef<dyn ScheduleLabel> as parameter now take impl Into<InternedScheduleLabel>, which is implemented by the derive-macro for ScheduleLabel for T and &T, so it almost works the same as before.

I've also introduced a new trait and method StaticRef::static_ref which can be used to provide a consistent static reference to a value equal to self. This is used to further optimize the interning process, since interning a value for which a static reference can be provided doesn't need to lock the internal hash set anymore.

Copy link
Member

@JoJoJet JoJoJet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also switch AppLabel to use interned labels? It still uses pseudo-stringly typed labels, and I'd rather not leave that relic around.

crates/bevy_utils/src/intern.rs Outdated Show resolved Hide resolved
crates/bevy_app/src/app.rs Outdated Show resolved Hide resolved
@geieredgar
Copy link
Contributor Author

@JoJoJet Regarding AppLabel, is it okay if I do the switch in a follow-up PR?

@JoJoJet
Copy link
Member

JoJoJet commented Jul 23, 2023

I guess that's fine, but is there a reason not to do it now? As far as I know, you'd just have to change a define_label! to define_interned_label!, and update some callsites.

@geieredgar geieredgar changed the title Replace boxed labels with interned labels Replace all labels with interned labels Jul 23, 2023
@geieredgar
Copy link
Contributor Author

geieredgar commented Jul 23, 2023

OK, after giving it a second thought, I've decided to do the AppLabel switch inside this PR.

I also changed AnonymousSet to not use globally unique ids, and use ids only unique inside the given Schedule. This means that we are not increasing the leaked memory indefinitely when the user creates Schedules repeatedly. Instead the amount of memory leaked by AnonymousSets depends now on the Schedule that contained the most anonymous sets.

@JoJoJet
Copy link
Member

JoJoJet commented Jul 23, 2023

Thank you for implementing my suggestions. I'll try to review this more closely today.

@JoJoJet
Copy link
Member

JoJoJet commented Jul 28, 2023

I finally got around to this. I have a couple more ideas for how the API can be refined. Rather than going back-and-forth, I opened a PR to your branch: geieredgar#1.

As always, feel free to voice any concerns you have with my suggestions.

Copy link
Member

@JoJoJet JoJoJet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR is in a good state now! I just have a minor documentation suggestion.

crates/bevy_utils/src/intern.rs Outdated Show resolved Hide resolved
@JoJoJet JoJoJet added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Jul 28, 2023
@github-actions
Copy link
Contributor

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Could you add some context on what users should update when this change get released in a new version of Bevy?
It will be used to help writing the migration guide for the version. Putting it after a ## Migration Guide will help it get automatically picked up by our tooling.

@JoJoJet
Copy link
Member

JoJoJet commented Jul 28, 2023

Interned does and probably should not implement SystemSet, I think for the same reasons that Box doesn't, but I haven't looked up all the discussions about that. Generally, Interned(x) and x should not be treated as the same thing when it comes to hash values and comparisons.

This can be removed from the cons list, and added to the pros.

@nicopap nicopap self-requested a review October 1, 2023 08:42
@alice-i-cecile alice-i-cecile removed the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Oct 1, 2023
@alice-i-cecile alice-i-cecile added the D-Complex Quite challenging from either a design or technical perspective. Ask for help! label Oct 1, 2023
@geieredgar
Copy link
Contributor Author

geieredgar commented Oct 1, 2023

@nicopap After finding rust-lang/rust#106447 I think you are correct that this approach was unsound, because two equal labels could yield two different pointers. Therefore I removed the static_ref optimization completely, so interning will always go through the internal HashMap, guaranteeing that equal labels always return equal pointers.

I also believe that two different labels cannot yield the same fat pointer when leaking, because then a.dyn_eq(b) would return true. And currently ptr::eq does compare both the address and the metadata, so it should work. However, as shown in rust-lang/rust#106447 this behavior could change in the future.

@geieredgar
Copy link
Contributor Author

I've replaced the use of std::ptr::eq with a custom equality comparison, which first checks for equal type ids and then for equal data pointers. Therefore, we don't rely on the vtable pointers anymore.

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Oct 18, 2023

@geieredgar this is in the milestone for 0.12, slated for this Saturday. I'm going to do a full review now, but I'm hopeful we can get this in and ship it. Can you resolve merge conflicts?

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really cool stuff! The changes in schedule.rs and intern.rs are the only non-trivial changes: focus your review efforts there.

The breaking changes are acceptable and well-documented, the motivation is sound, I basically understand how this works, and this is incredibly well-tested. I'm comfortable approving this!

@geieredgar
Copy link
Contributor Author

Conflicts are resolved.

Copy link
Member

@cart cart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this approach!

}

fn ref_eq(&self, other: &Self) -> bool {
self.len() == other.len() && self.as_ptr() == other.as_ptr()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an optimization, seems worth it to flip the order here to short-circuit on the ptr comparison? I think two interned strs are more likely to have different pointers than different lengths.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed as I doubt this will be controversial (and in the interest of moving forward).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave this open in case anyone wants to discuss.

@cart cart enabled auto-merge October 25, 2023 21:22
@cart cart added this pull request to the merge queue Oct 25, 2023
Merged via the queue into bevyengine:main with commit a830530 Oct 25, 2023
21 checks passed
ameknite pushed a commit to ameknite/bevy that referenced this pull request Nov 6, 2023
# Objective

First of all, this PR took heavy inspiration from bevyengine#7760 and bevyengine#5715. It
intends to also fix bevyengine#5569, but with a slightly different approach.


This also fixes bevyengine#9335 by reexporting `DynEq`.

## Solution

The advantage of this API is that we can intern a value without
allocating for zero-sized-types and for enum variants that have no
fields. This PR does this automatically in the `SystemSet` and
`ScheduleLabel` derive macros for unit structs and fieldless enum
variants. So this should cover many internal and external use cases of
`SystemSet` and `ScheduleLabel`. In these optimal use cases, no memory
will be allocated.

- The interning returns a `Interned<dyn SystemSet>`, which is just a
wrapper around a `&'static dyn SystemSet`.
- `Hash` and `Eq` are implemented in terms of the pointer value of the
reference, similar to my first approach of anonymous system sets in
bevyengine#7676.
- Therefore, `Interned<T>` does not implement `Borrow<T>`, only `Deref`.
- The debug output of `Interned<T>` is the same as the interned value.

Edit: 
- `AppLabel` is now also interned and the old
`derive_label`/`define_label` macros were replaced with the new
interning implementation.
- Anonymous set ids are reused for different `Schedule`s, reducing the
amount of leaked memory.

### Pros
- `InternedSystemSet` and `InternedScheduleLabel` behave very similar to
the current `BoxedSystemSet` and `BoxedScheduleLabel`, but can be copied
without an allocation.
- Many use cases don't allocate at all.
- Very fast lookups and comparisons when using `InternedSystemSet` and
`InternedScheduleLabel`.
- The `intern` module might be usable in other areas.
- `Interned{ScheduleLabel, SystemSet, AppLabel}` does implement
`{ScheduleLabel, SystemSet, AppLabel}`, increasing ergonomics.

### Cons
- Implementors of `SystemSet` and `ScheduleLabel` still need to
implement `Hash` and `Eq` (and `Clone`) for it to work.

## Changelog

### Added

- Added `intern` module to `bevy_utils`.
- Added reexports of `DynEq` to `bevy_ecs` and `bevy_app`.

### Changed

- Replaced `BoxedSystemSet` and `BoxedScheduleLabel` with
`InternedSystemSet` and `InternedScheduleLabel`.
- Replaced `impl AsRef<dyn ScheduleLabel>` with `impl ScheduleLabel`.
- Replaced `AppLabelId` with `InternedAppLabel`.
- Changed `AppLabel` to use `Debug` for error messages.
- Changed `AppLabel` to use interning.
- Changed `define_label`/`derive_label` to use interning. 
- Replaced `define_boxed_label`/`derive_boxed_label` with
`define_label`/`derive_label`.
- Changed anonymous set ids to be only unique inside a schedule, not
globally.
- Made interned label types implement their label trait. 

### Removed

- Removed `define_boxed_label` and `derive_boxed_label`. 

## Migration guide

- Replace `BoxedScheduleLabel` and `Box<dyn ScheduleLabel>` with
`InternedScheduleLabel` or `Interned<dyn ScheduleLabel>`.
- Replace `BoxedSystemSet` and `Box<dyn SystemSet>` with
`InternedSystemSet` or `Interned<dyn SystemSet>`.
- Replace `AppLabelId` with `InternedAppLabel` or `Interned<dyn
AppLabel>`.
- Types manually implementing `ScheduleLabel`, `AppLabel` or `SystemSet`
need to implement:
  - `dyn_hash` directly instead of implementing `DynHash`
  - `as_dyn_eq`
- Pass labels to `World::try_schedule_scope`, `World::schedule_scope`,
`World::try_run_schedule`. `World::run_schedule`, `Schedules::remove`,
`Schedules::remove_entry`, `Schedules::contains`, `Schedules::get` and
`Schedules::get_mut` by value instead of by reference.

---------

Co-authored-by: Joseph <[email protected]>
Co-authored-by: Carter Anderson <[email protected]>
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this pull request Jan 9, 2024
# Objective

First of all, this PR took heavy inspiration from bevyengine#7760 and bevyengine#5715. It
intends to also fix bevyengine#5569, but with a slightly different approach.


This also fixes bevyengine#9335 by reexporting `DynEq`.

## Solution

The advantage of this API is that we can intern a value without
allocating for zero-sized-types and for enum variants that have no
fields. This PR does this automatically in the `SystemSet` and
`ScheduleLabel` derive macros for unit structs and fieldless enum
variants. So this should cover many internal and external use cases of
`SystemSet` and `ScheduleLabel`. In these optimal use cases, no memory
will be allocated.

- The interning returns a `Interned<dyn SystemSet>`, which is just a
wrapper around a `&'static dyn SystemSet`.
- `Hash` and `Eq` are implemented in terms of the pointer value of the
reference, similar to my first approach of anonymous system sets in
bevyengine#7676.
- Therefore, `Interned<T>` does not implement `Borrow<T>`, only `Deref`.
- The debug output of `Interned<T>` is the same as the interned value.

Edit: 
- `AppLabel` is now also interned and the old
`derive_label`/`define_label` macros were replaced with the new
interning implementation.
- Anonymous set ids are reused for different `Schedule`s, reducing the
amount of leaked memory.

### Pros
- `InternedSystemSet` and `InternedScheduleLabel` behave very similar to
the current `BoxedSystemSet` and `BoxedScheduleLabel`, but can be copied
without an allocation.
- Many use cases don't allocate at all.
- Very fast lookups and comparisons when using `InternedSystemSet` and
`InternedScheduleLabel`.
- The `intern` module might be usable in other areas.
- `Interned{ScheduleLabel, SystemSet, AppLabel}` does implement
`{ScheduleLabel, SystemSet, AppLabel}`, increasing ergonomics.

### Cons
- Implementors of `SystemSet` and `ScheduleLabel` still need to
implement `Hash` and `Eq` (and `Clone`) for it to work.

## Changelog

### Added

- Added `intern` module to `bevy_utils`.
- Added reexports of `DynEq` to `bevy_ecs` and `bevy_app`.

### Changed

- Replaced `BoxedSystemSet` and `BoxedScheduleLabel` with
`InternedSystemSet` and `InternedScheduleLabel`.
- Replaced `impl AsRef<dyn ScheduleLabel>` with `impl ScheduleLabel`.
- Replaced `AppLabelId` with `InternedAppLabel`.
- Changed `AppLabel` to use `Debug` for error messages.
- Changed `AppLabel` to use interning.
- Changed `define_label`/`derive_label` to use interning. 
- Replaced `define_boxed_label`/`derive_boxed_label` with
`define_label`/`derive_label`.
- Changed anonymous set ids to be only unique inside a schedule, not
globally.
- Made interned label types implement their label trait. 

### Removed

- Removed `define_boxed_label` and `derive_boxed_label`. 

## Migration guide

- Replace `BoxedScheduleLabel` and `Box<dyn ScheduleLabel>` with
`InternedScheduleLabel` or `Interned<dyn ScheduleLabel>`.
- Replace `BoxedSystemSet` and `Box<dyn SystemSet>` with
`InternedSystemSet` or `Interned<dyn SystemSet>`.
- Replace `AppLabelId` with `InternedAppLabel` or `Interned<dyn
AppLabel>`.
- Types manually implementing `ScheduleLabel`, `AppLabel` or `SystemSet`
need to implement:
  - `dyn_hash` directly instead of implementing `DynHash`
  - `as_dyn_eq`
- Pass labels to `World::try_schedule_scope`, `World::schedule_scope`,
`World::try_run_schedule`. `World::run_schedule`, `Schedules::remove`,
`Schedules::remove_entry`, `Schedules::contains`, `Schedules::get` and
`Schedules::get_mut` by value instead of by reference.

---------

Co-authored-by: Joseph <[email protected]>
Co-authored-by: Carter Anderson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Performance A change motivated by improving speed, memory usage or compile times C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Complex Quite challenging from either a design or technical perspective. Ask for help! M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide X-Controversial There is active debate or serious implications around merging this PR
Projects
None yet
6 participants