Skip to content

Conversation

ItsDoot
Copy link
Contributor

@ItsDoot ItsDoot commented May 24, 2025

Objective

Solution

Makes the necessary internal data structure changes in order to allow system removal to be added in a future PR: Vecs storing systems and system sets in ScheduleGraph have been replaced with SlotMaps.

See the included migration guide for the required changes.

Testing

Internal changes only and no new features should mean no new tests are requried.

@ItsDoot ItsDoot marked this pull request as draft May 24, 2025 03:05
@ItsDoot ItsDoot force-pushed the schedule/slotmap branch from 1f9ed64 to a91e7de Compare May 24, 2025 03:13
@ItsDoot ItsDoot added A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes labels May 24, 2025
@ItsDoot ItsDoot force-pushed the schedule/slotmap branch from a91e7de to bfed18a Compare May 24, 2025 03:23
@ItsDoot ItsDoot force-pushed the schedule/slotmap branch from bfed18a to 69e0f0a Compare July 2, 2025 03:42
Copy link
Contributor

github-actions bot commented Jul 2, 2025

Your PR increases Bevy Minimum Supported Rust Version. Please update the rust-version field in the root Cargo.toml file.

@ItsDoot ItsDoot marked this pull request as ready for review July 2, 2025 06:15
@ItsDoot ItsDoot added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jul 2, 2025
@ItsDoot ItsDoot added this to the 0.17 milestone Jul 2, 2025
Copy link
Contributor

@urben1680 urben1680 left a comment

Choose a reason for hiding this comment

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

Well done! I like how this improved type safety here and there!

Copy link
Contributor

@chescock chescock left a comment

Choose a reason for hiding this comment

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

Yeah, the extra type safety is really nice!

I left some thoughts, but nothing blocking.

@ItsDoot ItsDoot 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 Jul 3, 2025
@alice-i-cecile
Copy link
Member

Good stuff :) I'm looking forward to progress on dynamic system scheduling after all these years <3

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jul 3, 2025
Merged via the queue into bevyengine:main with commit ebf87f5 Jul 3, 2025
38 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Jul 8, 2025
# Objective

Make the schedule graph code more understandable, and replace some
panics with `Result`s.

I found the `check_edges` and `check_hierarchy` functions [a little
confusing](#19352 (comment)),
as they combined two concerns: Initializing graph nodes for system sets,
and checking for self-edges on system sets. It was hard to understand
the self-edge checks because it wasn't clear what `NodeId` was being
checked against! So, let's separate those concerns, and move them to
more appropriate locations.

Fix a bug where `schedule.configure_sets((SameSet, SameSet).chain());`
would panic with an unhelpful message: `assertion failed: index_a <
index_b`.

## Solution

Remove the `check_edges` and `check_hierarchy` functions, separating the
initialization behavior and the checking behavior and moving them where
they are easier to understand.

For initializing graph nodes, do this on-demand using the `entry` API by
replacing later `self.system_set_ids[&set]` calls with a
`self.system_sets.get_or_add_set(set)` method. This should avoid the
need for an extra pass over the graph and an extra lookup.

Unfortunately, implementing that method directly on `ScheduleGraph`
leads to borrowing errors as it borrows the entire `struct`. So, split
out the collections managing system sets into a separate `struct`.

For checking self-edges, move this check later so that it can be
reported by returning a `Result` from `Schedule::initialize` instead of
having to panic in `configure_set_inner`. The issue was that `iter_sccs`
does not report self-edges as cycles, since the resulting components
only have one node, but that later code assumes all edges point forward.
So, check for self-edges directly, immediately before calling
`iter_sccs`.

This also ensures we catch *every* way that self-edges can be added. The
previous code missed an edge case where `chain()`ing a set to itself
would create a self-edge and would trigger a `debug_assert`.
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-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide 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