From f7d7c246b8b76cbd4440b9c1665f0ccaa57e9880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Thu, 15 Jan 2015 08:32:11 +0100 Subject: [PATCH] Slightly optimize Workspace::xStackingOrder Callgrind analysis showed that this method has room for improvement. The bottle neck is mapping the Unmanaged against the list of windows retreived from xcb_query_tree. The number of windows in that list is rather large (>1000), which turns the loop into an expensive path. Workspace::findUnmanaged seems to be too expensive for that due to the overhead of using a functor and multiple method calls. The Inl. cost before the optimization for checking ~55,000 windows is 2.46. The change uses a good old const-iterator based loop over the unmanaged list to check for the window. This reduces the Incl. cost for checking ~55,000 windows to 0.28. REVIEW: 122067 --- layers.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/layers.cpp b/layers.cpp index 8b49f29615..e95305e110 100644 --- a/layers.cpp +++ b/layers.cpp @@ -670,11 +670,22 @@ ToplevelList Workspace::xStackingOrder() const x_stacking.append(c); xcb_window_t *windows = tree.children(); + const auto count = tree->children_len; + int foundUnmanagedCount = unmanaged.count(); for (unsigned int i = 0; - i < tree->children_len; + i < count; ++i) { - if (Unmanaged* c = findUnmanaged(windows[i])) - x_stacking.append(c); + for (auto it = unmanaged.constBegin(); it != unmanaged.constEnd(); ++it) { + Unmanaged *u = *it; + if (u->window() == windows[i]) { + x_stacking.append(u); + foundUnmanagedCount--; + break; + } + } + if (foundUnmanagedCount == 0) { + break; + } } if (m_compositor) { const_cast< Workspace* >(this)->m_compositor->checkUnredirect();