diff options
| author | Tom Englund <tomenglund26@gmail.com> | 2025-01-19 10:38:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-19 10:38:42 +0000 |
| commit | 4da9b7cc5b50c346309378fbb5ae8aa07746fd33 (patch) | |
| tree | a618fb83c13bac02729751d866a884e7207d08e7 /src/protocols | |
| parent | f56153a9c1b0a00fac0932a95e0cfa5a4f6c681f (diff) | |
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.
Diffstat (limited to 'src/protocols')
| -rw-r--r-- | src/protocols/DRMLease.cpp | 3 | ||||
| -rw-r--r-- | src/protocols/GlobalShortcuts.cpp | 15 | ||||
| -rw-r--r-- | src/protocols/Screencopy.cpp | 2 | ||||
| -rw-r--r-- | src/protocols/ToplevelExport.cpp | 2 |
4 files changed, 20 insertions, 2 deletions
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<CWpDrmLeaseV1> resource_, SP<CDRMLeaseRe }()); std::vector<SP<Aquamarine::IOutput>> 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<SShortcut> CGlobalShortcutsProtocol::getAllShortcuts() { std::vector<SShortcut> 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<WP<CScreencopyFrame>> 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<WP<CToplevelExportFrame>> framesToRemove; + // reserve number of elements to avoid reallocations + framesToRemove.reserve(m_vFramesAwaitingWrite.size()); // share frame if correct output for (auto const& f : m_vFramesAwaitingWrite) { |
