-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Fix Taffy viewport node leaks #17596
base: main
Are you sure you want to change the base?
Conversation
`LayoutNode`s also have a `viewport_id: Option<taffy::NodeId` which is the id of the corresponding implicit "viewport" node if the node is a root UI node, otherwise it is `None`. When removing or parenting nodes this field is checked and the implicit viewport node is removed if present.
#[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
pub struct LayoutNode { | ||
// Implicit "viewport" node if this `LayoutNode` corresponds to a root UI node entity | ||
pub(super) viewport_id: Option<taffy::NodeId>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding it here means that it will allocate memory for each entry even though only few of them will be actually filled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's an additional u64
for every UI entity. not sure if it's a problem or not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah agree it's not ideal but we have really fat UI nodes already so it shouldn't make much difference. In contrast, each leaked leaked Taffy node is about a thousand bytes. I thought about adding another EntityHashMap
instead but the layout module already feels over complicated and I didn't want to even more synchronisation code. This extra NodeId
approach is pretty foolproof.
Add tests to check for leaked vewport nodes.
Objective
For most UI node entities there's a 1-to-1 mapping from the entity to its associated Taffy node. Root UI nodes are an exception though, their corresponding Taffy node in the Taffy tree is also given a parent that represents the viewport. These viewport Taffy nodes are not removed when a root UI node is despawned.
Parenting of an existing root UI node with an associated viewport Taffy node also results in the leak of the viewport node.
These tests fail if added to the
layout
module's tests on the main branch:Fixes #17594
Solution
Change the
UiSurface::entity_to_taffy
to map toLayoutNode
s. ALayoutNode
has aviewport_id: Option<taffy::NodeId>
field which is the id of the corresponding implicit "viewport" node if the node is a root UI node, otherwise it isNone
. When removing or parenting nodes this field is checked and the implicit viewport node is removed if present.Testing
There are two new tests in
bevy_ui::layout::tests
included with this PR:no_viewport_node_leak_on_root_despawned
no_viewport_node_leak_on_parented_root