mirror of
https://github.com/hyprwm/Hyprland.git
synced 2025-08-17 21:13:48 -07:00
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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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) {
|
||||
|
@@ -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) {
|
||||
|
Reference in New Issue
Block a user