From 4da9b7cc5b50c346309378fbb5ae8aa07746fd33 Mon Sep 17 00:00:00 2001 From: Tom Englund Date: Sun, 19 Jan 2025 10:38:42 +0000 Subject: core: reserve vector sizes as much as we can (#9118) avoid reallocations as much as possible with a few edge cases where the reservation overshoots a tiny bit. but a few bytes of memory short term is better used then the overhead of potential reallocation. --- src/render/OpenGL.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/render/OpenGL.cpp') 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); -- cgit v1.2.3