From dceb64147f08efcd6a4f90e7923729a2a0b43e86 Mon Sep 17 00:00:00 2001 From: Andrea Mancuso Date: Sat, 21 Nov 2026 15:19:12 +0100 Subject: [PATCH] Re-link only parents whose child list changed Every publication removed the Yoga children of every owned node and inserted them again. That marks the whole layout dirty, so the frame after a commit laid out the whole tree again, even when one row changed. A parent is now re-linked only when its child list changed, it is new to the publication, or it is being destroyed. Moved children still leave a parent whose list changed or a destroyed one before being attached. Measured through xframes-core (xframes-project/xframes-core#21), one changed row per commit, hidden window: the frame after a commit at 2,000 rows (8,001 nodes) went from 168.5 ms to 12.5 ms, which is what a frame without a commit costs. The protocol, validation and receipts are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0186WhMU3KjnmjR7Y4r49GrT --- .../dear-imgui/cpp/app/src/xframes_commit.cpp | 22 ++++++++++++++----- .../dear-imgui/cpp/tests/xframes_test.cpp | 19 ++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/packages/dear-imgui/cpp/app/src/xframes_commit.cpp b/packages/dear-imgui/cpp/app/src/xframes_commit.cpp index aa4c5f21..4006ee4d 100644 --- a/packages/dear-imgui/cpp/app/src/xframes_commit.cpp +++ b/packages/dear-imgui/cpp/app/src/xframes_commit.cpp @@ -105,14 +105,26 @@ CommitResult XFrames::ApplyCommitOperations(CommitBatch& batch) { } } operationIndex.reset(); - // Disconnect every previous Yoga owner before attaching any - // candidate child. Moving out of a removed ancestor retains the - // exact Element, Yoga node, internal subject and populated data. + // Only parents whose child list changed are re-linked. Re-linking an + // unchanged parent marks its Yoga subtree dirty and makes the next + // frame lay it out again. A child that moves leaves a parent whose + // list changes too, or one that is destroyed; both are disconnected + // before any candidate child is attached. Moving out of a removed + // ancestor retains the exact Element, Yoga node, internal subject and + // populated data. + std::unordered_set relink; + for (const auto& [parent, children] : plan.children) { + if (parent == 0) continue; + const auto previous = m_hierarchy.find(parent); + if (!m_publicationOwnedIds.contains(parent) || previous == m_hierarchy.end() || previous->second != children) + relink.insert(parent); + } for (int id : m_publicationOwnedIds) - YGNodeRemoveAllChildren(m_elements.at(id)->m_layoutNode->m_node); + if (relink.contains(id) || !plan.ownedIds.contains(id)) + YGNodeRemoveAllChildren(m_elements.at(id)->m_layoutNode->m_node); for (const auto& [parent, children] : plan.children) { m_hierarchy[parent] = children; - if (parent == 0) continue; + if (parent == 0 || !relink.contains(parent)) continue; auto* element = m_elements.at(parent).get(); for (size_t index = 0; index < children.size(); ++index) element->m_layoutNode->InsertChild(m_elements.at(children[index])->m_layoutNode.get(), index); diff --git a/packages/dear-imgui/cpp/tests/xframes_test.cpp b/packages/dear-imgui/cpp/tests/xframes_test.cpp index ba640316..122cff86 100644 --- a/packages/dear-imgui/cpp/tests/xframes_test.cpp +++ b/packages/dear-imgui/cpp/tests/xframes_test.cpp @@ -162,6 +162,25 @@ TEST_F(XFramesTest, PublicationReorderAndInsertionKeepHierarchyAndYogaConsistent EXPECT_EQ(Node(state, 1)["yogaChildren"], json::array({3, 4, 2})); EXPECT_EQ(Node(state, 4)["yogaParent"], 1); } +// Re-linking a parent marks its Yoga subtree dirty, so the next frame lays it out +// again. A publication re-links only parents whose child list changed: a sibling +// subtree that did not change keeps its layout. +TEST_F(XFramesTest, PublicationRelinksOnlyParentsWhoseChildrenChanged) { + Publish({{0, {1}}, {1, {2, 3}}, {2, {4}}, {3, {5}}, {4, {}}, {5, {}}}, + {Create(1), Create(2), Create(3), Create(4), Create(5)}); + const auto yoga = [&](int id) { return ElementAt(id)->m_layoutNode->m_node; }; + YGNodeCalculateLayout(yoga(1), 100, 100, YGDirectionLTR); + ASSERT_FALSE(YGNodeIsDirty(yoga(3))); + EXPECT_EQ(Publish({{0, {1}}, {1, {2, 3}}, {2, {6}}, {3, {5}}, {6, {}}, {5, {}}}, {Create(6)}).destroyedIds, + (std::vector{4})); + EXPECT_TRUE(YGNodeIsDirty(yoga(2))); + EXPECT_FALSE(YGNodeIsDirty(yoga(3))); + EXPECT_FALSE(YGNodeIsDirty(yoga(5))); + const auto state = xf->GetDiagnosticsState(); + EXPECT_EQ(Node(state, 2)["yogaChildren"], json::array({6})); + EXPECT_EQ(Node(state, 3)["yogaChildren"], json::array({5})); + EXPECT_EQ(Node(state, 1)["yogaChildren"], json::array({2, 3})); +} TEST_F(XFramesTest, PublicationIdenticalAndEmptyTreesAdvanceExactlyOnce) { EXPECT_EQ(Publish().nativeRevision, 1); EXPECT_EQ(Publish({{0, {1}}, {1, {}}}, {Create(1)}).nativeRevision, 2);