1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-15 16:32:41 +01:00

Codechange: Shrink GUIList vectors less often, reserve before use.

After sorting and filter lists for GUI, we often shirnk them to reduce size. However this has very little benefit:

1) The memory has already been allocated, so it doesn't prevent that memory being required.
2) It causes a new allocation and copy when the vector is shrunk, actually using more memory.
3) The list is in window state, so the lifetime is only while the window is open.
4) When a filter is clearer, the original size will be needed again, which will cause another allocation.

In fact it is beneficial to reserve to the known maximum in most cases, so do that instead.
This commit is contained in:
Peter Nelson
2024-04-27 08:20:13 +01:00
committed by Peter Nelson
parent 8308998388
commit 33aedc43a5
15 changed files with 7 additions and 17 deletions

View File

@@ -144,6 +144,7 @@ public:
if (!this->object_classes.NeedRebuild()) return;
this->object_classes.clear();
this->object_classes.reserve(ObjectClass::GetClassCount());
for (const auto &cls : ObjectClass::Classes()) {
if (cls.GetUISpecCount() == 0) continue; // Is this needed here?
@@ -151,7 +152,6 @@ public:
}
this->object_classes.Filter(this->string_filter);
this->object_classes.shrink_to_fit();
this->object_classes.RebuildDone();
this->object_classes.Sort();