Add SystemIdTemplate to allow one-shot system in bsn! - #24026
Open
hxYuki wants to merge 13 commits into
Open
Conversation
Contributor
|
Welcome, new contributor! Please make sure you've read our contributing guide, as well as our policy regarding AI usage, and we look forward to reviewing your pull request shortly ✨ |
Contributor
Author
|
Not sure where to place these codes. I've searched |
hxYuki
commented
Apr 29, 2026
|
|
||
| impl Default for SystemIdTemplate { | ||
| fn default() -> Self { | ||
| Self::BoxedSystem(Arc::new(Mutex::new(Some(Box::new(IntoSystem::into_system(|| {})))))) |
Contributor
Author
There was a problem hiding this comment.
What should be the default value for a SystemId?
SkiFire13
reviewed
Apr 29, 2026
SkiFire13
reviewed
Apr 29, 2026
SkiFire13
reviewed
Apr 30, 2026
SkiFire13
reviewed
May 3, 2026
SkiFire13
left a comment
Contributor
There was a problem hiding this comment.
IMO this is becoming pretty complex and I'm kinda losing track of how everything fits together.
hxYuki
force-pushed
the
system-id-template
branch
from
May 12, 2026 10:29
7f130f0 to
83ec72e
Compare
mockersf
added a commit
to mockersf/bevy
that referenced
this pull request
May 22, 2026
# Objective bevyengine#24087 introduces scene templating for `SystemId`s, however it can result in a memory leak if a scene is re-constructed multiple times: bevyengine#24087 (comment) > This was proposed basically 1:1 in bevyengine#24026 (this was later changed though). The issue is that it's unclear who owns these systems, that is who is responsible for unregistering them once they are no longer needed. Given that recreating the template will spawn the system again this basically becomes a memory leak. bevyengine#24087 (comment) > > Hm, are you sure this is the case even tho in `build_template` it only registers the system the first time its called, switching over to storing the SystemId after the first call? > > If you recreate the template (e.g. you call `my_scene()` again) then you will create a new instance of the system. And since the system is not scoped to the scene once the scene is despawned the system entity will be leaked. Essentially, we need a way to connect the lifetime of the registered system to the lifetime of the scene. ## Solution This is a purely additive / opt-in / backwards-compatible version of bevyengine#24114 Introducing: `SystemHandle`s ```rust pub enum SystemHandle<I: SystemInput = (), O = ()> { /// A strong handle keeps the system entity alive as long as the handle /// (and any clones of it) exist. Strong(Arc<StrongSystemHandle>), /// A weak handle does not keep the system entity alive. Weak(SystemId<I, O>), } pub struct StrongSystemHandle { entity: Entity, drop_queue: Arc<ConcurrentQueue<<Entity>>, } ``` Similar to `bevy_asset::Handle`s,`SystemHandle`'s custom `Drop` implementation enqueues the registered system entity into a concurrent queue. The system `despawn_unused_registered_systems` pulls from the other end of this queue and despawns the registered system entities. `World::register_tracked_system` and `World::register_tracked_boxed_system` are the only functions that return `SystemHandle`s. ## Testing - Added a test to ensure that `despawn_unused_registered_systems` does its job - Added a test to ensure that the default app will automatically call `despawn_unused_registered_systems` ## Future work - bevyengine#24087 will use this PR as a base --------- Co-authored-by: Chris Russell <8494645+chescock@users.noreply.github.com> Co-authored-by: François Mockers <francois.mockers@vleue.com>
mockersf
added a commit
that referenced
this pull request
Jun 10, 2026
# Objective #24087 introduces scene templating for `SystemId`s, however it can result in a memory leak if a scene is re-constructed multiple times: #24087 (comment) > This was proposed basically 1:1 in #24026 (this was later changed though). The issue is that it's unclear who owns these systems, that is who is responsible for unregistering them once they are no longer needed. Given that recreating the template will spawn the system again this basically becomes a memory leak. #24087 (comment) > > Hm, are you sure this is the case even tho in `build_template` it only registers the system the first time its called, switching over to storing the SystemId after the first call? > > If you recreate the template (e.g. you call `my_scene()` again) then you will create a new instance of the system. And since the system is not scoped to the scene once the scene is despawned the system entity will be leaked. Essentially, we need a way to connect the lifetime of the registered system to the lifetime of the scene. ## Solution This is a purely additive / opt-in / backwards-compatible version of #24114 Introducing: `SystemHandle`s ```rust pub enum SystemHandle<I: SystemInput = (), O = ()> { /// A strong handle keeps the system entity alive as long as the handle /// (and any clones of it) exist. Strong(Arc<StrongSystemHandle>), /// A weak handle does not keep the system entity alive. Weak(SystemId<I, O>), } pub struct StrongSystemHandle { entity: Entity, drop_queue: Arc<ConcurrentQueue<<Entity>>, } ``` Similar to `bevy_asset::Handle`s,`SystemHandle`'s custom `Drop` implementation enqueues the registered system entity into a concurrent queue. The system `despawn_unused_registered_systems` pulls from the other end of this queue and despawns the registered system entities. `World::register_tracked_system` and `World::register_tracked_boxed_system` are the only functions that return `SystemHandle`s. ## Testing - Added a test to ensure that `despawn_unused_registered_systems` does its job - Added a test to ensure that the default app will automatically call `despawn_unused_registered_systems` ## Future work - #24087 will use this PR as a base --------- Co-authored-by: Chris Russell <8494645+chescock@users.noreply.github.com> Co-authored-by: François Mockers <francois.mockers@vleue.com>
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
Add
SystemIdTemplateAdd a helper function
system_value<M>to convert a system toSystemIdTemplateWhen a
SystemIdTemplateis built, record itsTypeIdfor theSystemIdinHashMapand spawn a "relationship pair" between the template entity and the registered system entity.Each cloned template built will get the inserted
SystemId.Each new built template refers to same system will get the
SystemIdby system type.When a template item despawns, despawn all its "relationship pairs" entities, which triggers a hook on the relationship component to check if the system entity is still referenced. If not, unregister it and remove the record.
Testing
codes
Showcase