From 5cd2a370795b2447e1a894befbf3c4ee155484be Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 18 Aug 2020 16:25:58 -0600 Subject: [PATCH] LibGfx: Small improvement for DisjointRectSet::shatter This avoids a call to clone() which would be discarded immediately. Also, avoid essentially cloning for each hammer rectangle unless there is actually a need for it. --- Libraries/LibGfx/DisjointRectSet.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Libraries/LibGfx/DisjointRectSet.cpp b/Libraries/LibGfx/DisjointRectSet.cpp index b68f4cab05..1ec3d95d90 100644 --- a/Libraries/LibGfx/DisjointRectSet.cpp +++ b/Libraries/LibGfx/DisjointRectSet.cpp @@ -173,10 +173,13 @@ DisjointRectSet DisjointRectSet::shatter(const DisjointRectSet& hammer) const return clone(); // TODO: This could use some optimization - auto shards = clone(); - for (auto& hammer_rect : hammer.m_rects) { - auto shattered = shards.shatter(hammer_rect); - shards = move(shattered); + DisjointRectSet shards = shatter(hammer.m_rects[0]); + auto rects_count = hammer.m_rects.size(); + for (size_t i = 1; i < rects_count && !shards.is_empty(); i++) { + if (hammer.m_rects[i].intersects(shards.m_rects)) { + auto shattered = shards.shatter(hammer.m_rects[i]); + shards = move(shattered); + } } // Since there should be no overlaps, we don't need to call shatter() return shards;