Add debug cycle detection for insert_recursive and remove_recursive#19293
Add debug cycle detection for insert_recursive and remove_recursive#19293theotherphil wants to merge 2 commits intobevyengine:mainfrom
Conversation
|
Do you have benchmarks for these checks? |
I don't - how are these typically added in Bevy? |
|
People usually post a screenshot of Tracy for the function in question :) I don't think it's too important for this PR, so feel free to skip it if it sounds like a hassle. |
|
Ah, thanks! I would be surprised if this was a material overhead in debug builds but I'm happy to write something synthetic to check. Before doing so I'll wait until I have an implementation that @bushrat011899 is happy with. |
|
I feel like it would be more broadly useful to create a cycle detector for arbitrary relations, and then document that in the relations docs. This isn't specific to insert / remove recursive, and the user can then just stick it in wherever they want. |
|
What would the API for that look like - is this something you want to check per traversal, or something to optionally check for within the Relationship component hooks? (Or something else?) |
Register a error-returning lifecycle observer with a generic type on the App, which users can then cfg(debug_assertions) themselves. |
Objective
Partially fixes #17465.
Solution
Add a
CyclicTraversalDetectorstruct and manually thread it through calls toinsert_recursive(_impl)andremove_recursive(_impl).All of the
cfg(debug_assertion)s are a bit ugly, and this approach would require more wiring anywhere else we wanted to add cycle detection. We could instead use a scoped resource that gets inserted intoworldat the start of the traversal and removed at the end, but before adding complexity I wanted to check with @bushrat011899 and/or @alice-i-cecile where else they want cycle detection to be supported (and whether this "scoped resource" approach would even be sensible and/or has prior art).Testing
Added
should_panictests for a cyclical hierarchy.