Skip to content

Use new run_without_applying_deferred method in SingleThreadedExecutor #18684

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

Conversation

chescock
Copy link
Contributor

@chescock chescock commented Apr 2, 2025

Objective

Simplify code in the SingleThreadedExecutor by removing a special case for exclusive systems.

The SingleThreadedExecutor runs systems without immediately applying deferred buffers. That required calling run_unsafe() instead of run(), but that would panic for exclusive systems, so the code also needed a special case for those. Following #18076 and #18406, we have a run_without_applying_deferred method that has the exact behavior we want and works on exclusive systems.

Solution

Replace the code in SingleThreadedExecutor that runs systems with a single call to run_without_applying_deferred(). Also add this as a wrapper in the __rust_begin_short_backtrace module to preserve the special behavior for backtraces.

@chescock chescock added A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 2, 2025
@alice-i-cecile alice-i-cecile added the D-Straightforward Simple bug fixes and API improvements, docs, test and examples label Apr 2, 2025
@alice-i-cecile
Copy link
Member

Less unsafe is more good!

world: &mut World,
) -> Result {
let result = system.run_without_applying_deferred((), world);
black_box(());
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the first time I've seen black_box used like this, and after some searching I still don't know why this would be beneficial. Can you explain or link me to somewhere this is explained?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not actually sure! I just copied it from the other methods.

... Okay, I think I found the source. It comes from the __rust_begin_short_backtrace method in std, which has a comment "prevent this frame from being tail-call optimised away".

https://github.com/rust-lang/rust/blob/ae9173d7dd4a31806c950c90dcc331f1508b4d17/library/std/src/sys/backtrace.rs#L154-L155

I'm going to add that as a comment to the black_box calls here, even though it's not strictly related to this PR.

Copy link
Contributor

@SpecificProtagonist SpecificProtagonist left a comment

Choose a reason for hiding this comment

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

Scheduler simplifications are nice. And yep, I should have commented the black_box.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 2, 2025
@alice-i-cecile alice-i-cecile added this to the 0.17 milestone Apr 2, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue May 6, 2025
Merged via the queue into bevyengine:main with commit 3442e25 May 6, 2025
34 checks passed
andrewzhurov pushed a commit to andrewzhurov/bevy that referenced this pull request May 17, 2025
…utor` (bevyengine#18684)

# Objective

Simplify code in the `SingleThreadedExecutor` by removing a special case
for exclusive systems.

The `SingleThreadedExecutor` runs systems without immediately applying
deferred buffers. That required calling `run_unsafe()` instead of
`run()`, but that would `panic` for exclusive systems, so the code also
needed a special case for those. Following bevyengine#18076 and bevyengine#18406, we have a
`run_without_applying_deferred` method that has the exact behavior we
want and works on exclusive systems.

## Solution

Replace the code in `SingleThreadedExecutor` that runs systems with a
single call to `run_without_applying_deferred()`. Also add this as a
wrapper in the `__rust_begin_short_backtrace` module to preserve the
special behavior for backtraces.
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 D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants