diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 496ca4fe..889ae181 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -2785,11 +2785,9 @@ PHLWINDOW CCompositor::getForceFocus() { void CCompositor::arrangeMonitors() { static auto* const PXWLFORCESCALEZERO = (Hyprlang::INT* const*)g_pConfigManager->getConfigValuePtr("xwayland:force_zero_scaling"); - std::vector toArrange; + std::vector toArrange(m_vMonitors.begin(), m_vMonitors.end()); std::vector arranged; - - for (auto const& m : m_vMonitors) - toArrange.push_back(m); + arranged.reserve(toArrange.size()); Debug::log(LOG, "arrangeMonitors: {} to arrange", toArrange.size()); diff --git a/src/desktop/Window.cpp b/src/desktop/Window.cpp index 241a17d7..829f4e2e 100644 --- a/src/desktop/Window.cpp +++ b/src/desktop/Window.cpp @@ -282,6 +282,8 @@ void CWindow::updateWindowDecos() { // make a copy because updateWindow can remove decos. std::vector decos; + // reserve to avoid reallocations + decos.reserve(m_dWindowDecorations.size()); for (auto const& wd : m_dWindowDecorations) { decos.push_back(wd.get()); diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index a1979206..65891840 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -1891,9 +1891,7 @@ SDispatchResult CKeybindManager::workspaceOpt(std::string args) { // apply // we make a copy because changeWindowFloatingMode might invalidate the iterator - std::vector ptrs; - for (auto const& w : g_pCompositor->m_vWindows) - ptrs.push_back(w); + std::vector ptrs(g_pCompositor->m_vWindows.begin(), g_pCompositor->m_vWindows.end()); for (auto const& w : ptrs) { if (!w->m_bIsMapped || w->m_pWorkspace != PWORKSPACE || w->isHidden()) diff --git a/src/protocols/DRMLease.cpp b/src/protocols/DRMLease.cpp index fce1e345..6fdbaf4b 100644 --- a/src/protocols/DRMLease.cpp +++ b/src/protocols/DRMLease.cpp @@ -34,6 +34,9 @@ CDRMLeaseResource::CDRMLeaseResource(SP resource_, SP> outputs; + // reserve to avoid reallocations + outputs.reserve(requested.size()); + for (auto const& m : requested) { outputs.emplace_back(m->monitor->output); } diff --git a/src/protocols/GlobalShortcuts.cpp b/src/protocols/GlobalShortcuts.cpp index 6fac2d7f..9f8f422c 100644 --- a/src/protocols/GlobalShortcuts.cpp +++ b/src/protocols/GlobalShortcuts.cpp @@ -83,8 +83,19 @@ void CGlobalShortcutsProtocol::sendGlobalShortcutEvent(std::string appid, std::s std::vector CGlobalShortcutsProtocol::getAllShortcuts() { std::vector copy; - for (auto const& c : m_vClients) { - for (auto const& sh : c->shortcuts) { + // calculate the total number of shortcuts, precomputing size is linear + // and potential reallocation is more costly then the added precompute overhead of looping + // and finding the total size. + size_t totalShortcuts = 0; + for (const auto& c : m_vClients) { + totalShortcuts += c->shortcuts.size(); + } + + // reserve number of elements to avoid reallocations + copy.reserve(totalShortcuts); + + for (const auto& c : m_vClients) { + for (const auto& sh : c->shortcuts) { copy.push_back(*sh); } } diff --git a/src/protocols/Screencopy.cpp b/src/protocols/Screencopy.cpp index 8bdaf8f9..5cf54eb4 100644 --- a/src/protocols/Screencopy.cpp +++ b/src/protocols/Screencopy.cpp @@ -412,6 +412,8 @@ void CScreencopyProtocol::onOutputCommit(PHLMONITOR pMonitor) { } std::vector> framesToRemove; + // reserve number of elements to avoid reallocations + framesToRemove.reserve(m_vFramesAwaitingWrite.size()); // share frame if correct output for (auto const& f : m_vFramesAwaitingWrite) { diff --git a/src/protocols/ToplevelExport.cpp b/src/protocols/ToplevelExport.cpp index 2d40cb78..fb49f722 100644 --- a/src/protocols/ToplevelExport.cpp +++ b/src/protocols/ToplevelExport.cpp @@ -393,6 +393,8 @@ void CToplevelExportProtocol::onOutputCommit(PHLMONITOR pMonitor) { return; // nothing to share std::vector> framesToRemove; + // reserve number of elements to avoid reallocations + framesToRemove.reserve(m_vFramesAwaitingWrite.size()); // share frame if correct output for (auto const& f : m_vFramesAwaitingWrite) { diff --git a/src/render/OpenGL.cpp b/src/render/OpenGL.cpp index c5bb3620..ceb94c81 100644 --- a/src/render/OpenGL.cpp +++ b/src/render/OpenGL.cpp @@ -400,7 +400,10 @@ std::optional> CHyprOpenGLImpl::getModsForFormat(EGLint fo m_sProc.eglQueryDmaBufModifiersEXT(m_pEglDisplay, format, len, mods.data(), external.data(), &len); std::vector result; - bool linearIsExternal = false; + // reserve number of elements to avoid reallocations + result.reserve(mods.size()); + + bool linearIsExternal = false; for (size_t i = 0; i < mods.size(); ++i) { if (external.at(i)) { if (mods.at(i) == DRM_FORMAT_MOD_LINEAR) @@ -449,6 +452,8 @@ void CHyprOpenGLImpl::initDRMFormats() { Debug::log(LOG, "Supported DMA-BUF formats:"); std::vector dmaFormats; + // reserve number of elements to avoid reallocations + dmaFormats.reserve(formats.size()); for (auto const& fmt : formats) { std::vector mods; @@ -472,8 +477,10 @@ void CHyprOpenGLImpl::initDRMFormats() { }); std::vector> modifierData; + // reserve number of elements to avoid reallocations + modifierData.reserve(mods.size()); - auto fmtName = drmGetFormatName(fmt); + auto fmtName = drmGetFormatName(fmt); Debug::log(LOG, "EGL: GPU Supports Format {} (0x{:x})", fmtName ? fmtName : "?unknown?", fmt); for (auto const& mod : mods) { auto modName = drmGetFormatModifierName(mod); @@ -2942,7 +2949,8 @@ SP CHyprOpenGLImpl::createEGLSync(int fenceFD) { Debug::log(ERR, "createEGLSync: dup failed"); return nullptr; } - + // reserve number of elements to avoid reallocations + attribs.reserve(3); attribs.push_back(EGL_SYNC_NATIVE_FENCE_FD_ANDROID); attribs.push_back(dupFd); attribs.push_back(EGL_NONE); diff --git a/src/render/decorations/DecorationPositioner.cpp b/src/render/decorations/DecorationPositioner.cpp index c2d34fb6..2ba3c4a7 100644 --- a/src/render/decorations/DecorationPositioner.cpp +++ b/src/render/decorations/DecorationPositioner.cpp @@ -122,6 +122,9 @@ void CDecorationPositioner::onWindowUpdate(PHLWINDOW pWindow) { // std::vector datas; + // reserve to avoid reallocations + datas.reserve(pWindow->m_dWindowDecorations.size()); + for (auto const& wd : pWindow->m_dWindowDecorations) { datas.push_back(getDataFor(wd.get(), pWindow)); } diff --git a/src/xwayland/Dnd.cpp b/src/xwayland/Dnd.cpp index 58fc6db3..d4ae3780 100644 --- a/src/xwayland/Dnd.cpp +++ b/src/xwayland/Dnd.cpp @@ -75,6 +75,8 @@ void CX11DataDevice::sendEnter(uint32_t serial, SP surf, con data.data32[1] |= 1; std::vector targets; + // reserve to avoid reallocations + targets.reserve(SOURCE->mimes().size()); for (auto& mime : SOURCE->mimes()) { targets.emplace_back(g_pXWayland->pWM->mimeToAtom(mime)); diff --git a/src/xwayland/XWM.cpp b/src/xwayland/XWM.cpp index 44485381..ae8e0ccc 100644 --- a/src/xwayland/XWM.cpp +++ b/src/xwayland/XWM.cpp @@ -643,6 +643,8 @@ void CXWM::handleSelectionRequest(xcb_selection_request_event_t* e) { Debug::log(WARN, "[xwm] WARNING: No mimes in TARGETS?"); std::vector atoms; + // reserve to avoid reallocations + atoms.reserve(mimes.size() + 2); atoms.push_back(HYPRATOMS["TIMESTAMP"]); atoms.push_back(HYPRATOMS["TARGETS"]); @@ -989,6 +991,8 @@ void CXWM::sendState(SP surf) { } std::vector props; + // reserve to avoid reallocations + props.reserve(6); // props below if (surf->modal) props.push_back(HYPRATOMS["_NET_WM_STATE_MODAL"]); if (surf->fullscreen)