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

Remove WorldCell #3939

Closed
wants to merge 7 commits into from
Closed

Conversation

alice-i-cecile
Copy link
Member

@alice-i-cecile alice-i-cecile commented Feb 13, 2022

Objective

  • WorldCell is not fully featured: see Expand WorldCell to support queries #1555.
  • As a result, it does not fully behave like a Cell (a runtime borrow-checked API to split borrows arbitrarily).
  • Making it fully-featured is a questionable idea: see discussion in World cell queries rfcs#42 on how this promotes very panic-prone, fragile and hard-to-reason about code that is easy to write.

Solution

  • Remove WorldCell completely, rather than keeping a half-working implementation around.
  • Users should use SystemState or resource_scope as needed.
  • As other clear use cases arise that cannot be comfortably tackled with the existing API, add additional, less error-prone methods on World.

Notes

In the places where this was used:

  • several places were exclusive systems that did not need to be exclusive
  • a couple usages were trivially replaced with a resource_scope
  • other usages were trivially replaced with a SystemState
  • some were apparently completely unnecessary (assorted pipeline FromWorld impls)
  • one (winit_runner_with) was straightforward but rather verbose, and reflective of generally not-great code quality (it should be reusable but isn't, and is rather unstructured)

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Feb 13, 2022
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide C-Code-Quality A section of code that is hard to understand or change C-Usability A targeted quality-of-life change that makes Bevy easier to use and removed S-Needs-Triage This issue needs to be labelled labels Feb 13, 2022
@alice-i-cecile
Copy link
Member Author

alice-i-cecile commented Feb 13, 2022

@bevyengine/ecs-team; this is fundamentally a design decision. RFC #42 seems fairly doomed (a decision I support), but if it's doomed we should close it and remove the half-working WorldCell API.

As a point in favor of this removal, this API was never used internally.

@cart
Copy link
Member

cart commented Feb 13, 2022

As a point in favor of this removal, this API was never used internally.

This isn't true :)

image

@alice-i-cecile
Copy link
Member Author

alice-i-cecile commented Feb 13, 2022

Ah, my search for WorldCell missed it. Let me try fixing those...

@alice-i-cecile alice-i-cecile marked this pull request as draft February 13, 2022 23:20
@cart
Copy link
Member

cart commented Feb 13, 2022

Pretty sure they can all be replaced with SystemState or resource_scope.
What WorldCell really enables is "arbitrary nesting of non-conflicting contexts with arbitrary ad-hoc borrows". Bevy doesn't use that pattern internally except for the RenderGraph resource_scope, but I can see why some users might want it. We might want it if we want to support Entitas-style event handlers.

@alice-i-cecile
Copy link
Member Author

alice-i-cecile commented Feb 13, 2022

What WorldCell really enables is "arbitrary nesting of non-conflicting contexts with arbitrary ad-hoc borrows"

I agree.

This is a risky pattern, and non-trivial uses of this are likely to lead to complex runtime panics whenever surrounding code is changed. IMO the ideas discussed in #2192 are a much better way to accomplish the same goals.

@alice-i-cecile
Copy link
Member Author

alice-i-cecile commented Feb 14, 2022

It's a bit frustrating not to have a better solution for #2192 yet; the extremely complex system state constructed in winit_runner_with would particularly benefit from the caching and complexity management. Looking at the code, I actually think it may be cleanest to just create / store / run a little schedule on the App to push the information from winit into the various event resources. This would also make winit_runner much more reusable / hackable.

Overall, that refactor was quite painless, even for extremely complex uses of WorldCell. And now the system state can be cached and the borrow checker rules are upheld at compile time.

@alice-i-cecile alice-i-cecile marked this pull request as ready for review February 14, 2022 00:35
@TheRawMeatball
Copy link
Member

We might want it if we want to support Entitas-style event handlers.

I personally think requiring &mut World and adding hooks would be a better way to support this.

Copy link
Member

@TheRawMeatball TheRawMeatball left a comment

Choose a reason for hiding this comment

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

I agree with the idea, but the winit runner can be done better imo.

crates/bevy_winit/src/lib.rs Show resolved Hide resolved
bors bot pushed a commit that referenced this pull request Feb 27, 2022
# Objective

- In the large majority of cases, users were calling `.unwrap()` immediately after `.get_resource`.
- Attempting to add more helpful error messages here resulted in endless manual boilerplate (see #3899 and the linked PRs).

## Solution

- Add an infallible variant named `.resource` and so on.
- Use these infallible variants over `.get_resource().unwrap()` across the code base.

## Notes

I did not provide equivalent methods on `WorldCell`, in favor of removing it entirely in #3939.

## Migration Guide

Infallible variants of `.get_resource` have been added that implicitly panic, rather than needing to be unwrapped.

Replace `world.get_resource::<Foo>().unwrap()` with `world.resource::<Foo>()`.

## Impact

- `.unwrap` search results before: 1084
- `.unwrap` search results after: 942
- internal `unwrap_or_else` calls added: 4
- trivial unwrap calls removed from tests and code: 146
- uses of the new `try_get_resource` API: 11
- percentage of the time the unwrapping API was used internally: 93%
@aevyrie
Copy link
Member

aevyrie commented Mar 4, 2022

Please ping me when this is ready to merge, assuming it happens after #3974. I want to double check that everything still behaves as expected. 🙂

crates/bevy_pbr/src/render/light.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/lib.rs Outdated Show resolved Hide resolved
Co-authored-by: Aevyrie <aevyrie@gmail.com>
@alice-i-cecile
Copy link
Member Author

Please ping me when this is ready to merge

Will do! From my perspective, this is ready-to-merge; it's just waiting on a final decision from Cart.

@aevyrie
Copy link
Member

aevyrie commented Mar 5, 2022

Got it! We'll just have to be mindful if/when this and #3974 are merged that we don't accidentally break anything in the merging process. We don't have an effective way to automatically test the window updating just yet.

kurtkuehnert pushed a commit to kurtkuehnert/bevy that referenced this pull request Mar 6, 2022
- In the large majority of cases, users were calling `.unwrap()` immediately after `.get_resource`.
- Attempting to add more helpful error messages here resulted in endless manual boilerplate (see bevyengine#3899 and the linked PRs).

- Add an infallible variant named `.resource` and so on.
- Use these infallible variants over `.get_resource().unwrap()` across the code base.

I did not provide equivalent methods on `WorldCell`, in favor of removing it entirely in bevyengine#3939.

Infallible variants of `.get_resource` have been added that implicitly panic, rather than needing to be unwrapped.

Replace `world.get_resource::<Foo>().unwrap()` with `world.resource::<Foo>()`.

- `.unwrap` search results before: 1084
- `.unwrap` search results after: 942
- internal `unwrap_or_else` calls added: 4
- trivial unwrap calls removed from tests and code: 146
- uses of the new `try_get_resource` API: 11
- percentage of the time the unwrapping API was used internally: 93%
@alice-i-cecile alice-i-cecile added the X-Controversial There is active debate or serious implications around merging this PR label Apr 22, 2022
@alice-i-cecile alice-i-cecile mentioned this pull request Jun 7, 2022
9 tasks
@alice-i-cecile
Copy link
Member Author

Closing in favor of the dramatically less controversial #5109. I still think this should be done, but the code-quality changes should be ported over ASAP anyways.

@alice-i-cecile alice-i-cecile deleted the worldcell-yeet branch June 26, 2022 23:26
@james7132
Copy link
Member

Our internal uses of World::cell outside of tests is now at 0. It might be a good time to revisit this now that we have UnsafeWorldCell and DeferredWorld.

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-Code-Quality A section of code that is hard to understand or change C-Usability A targeted quality-of-life change that makes Bevy easier to use 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
Development

Successfully merging this pull request may close these issues.

5 participants