diff options
author | Jonathan G Rennison <j.g.rennison@gmail.com> | 2020-01-06 20:48:02 +0000 |
---|---|---|
committer | Charles Pigott <charlespigott@googlemail.com> | 2020-01-12 19:37:43 +0000 |
commit | d35f35a9682518769b13c8b576f4e69ceb0385d9 (patch) | |
tree | b157efe24798804bbe3d2e224cb34a616ffe2b97 /src | |
parent | c3223903ed4f39abe8589355882b30b973da434e (diff) | |
download | openttd-d35f35a9682518769b13c8b576f4e69ceb0385d9.tar.xz |
Codechange: Use cached town, station, industry names for list window sorting
This is to avoid needing to expensively regenerate name strings on
every comparison when sorting by name in the town/station/industry
list windows.
Diffstat (limited to 'src')
-rw-r--r-- | src/industry_gui.cpp | 18 | ||||
-rw-r--r-- | src/station_gui.cpp | 29 | ||||
-rw-r--r-- | src/town_gui.cpp | 20 |
3 files changed, 4 insertions, 63 deletions
diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 7410cb3a3..a5f03a2b9 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1251,7 +1251,6 @@ class IndustryDirectoryWindow : public Window { protected: /* Runtime saved values */ static Listing last_sorting; - static const Industry *last_industry; /* Constants for sorting stations */ static const StringID sorter_names[]; @@ -1350,8 +1349,6 @@ protected: this->industries.RebuildDone(); } - IndustryDirectoryWindow::last_industry = nullptr; // Reset name sorter sort cache - auto filter = std::make_pair(this->cargo_filter[this->accepted_cargo_filter_criteria], this->cargo_filter[this->produced_cargo_filter_criteria]); @@ -1398,19 +1395,7 @@ protected: /** Sort industries by name */ static bool IndustryNameSorter(const Industry * const &a, const Industry * const &b) { - static char buf_cache[96]; - static char buf[96]; - - SetDParam(0, a->index); - GetString(buf, STR_INDUSTRY_NAME, lastof(buf)); - - if (b != last_industry) { - last_industry = b; - SetDParam(0, b->index); - GetString(buf_cache, STR_INDUSTRY_NAME, lastof(buf_cache)); - } - - int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting). + int r = strnatcmp(a->GetCachedName(), b->GetCachedName()); // Sort by name (natural sorting). if (r == 0) return a->index < b->index; return r < 0; } @@ -1726,7 +1711,6 @@ public: }; Listing IndustryDirectoryWindow::last_sorting = {false, 0}; -const Industry *IndustryDirectoryWindow::last_industry = nullptr; /* Available station sorting functions. */ GUIIndustryList::SortFunction * const IndustryDirectoryWindow::sorter_funcs[] = { diff --git a/src/station_gui.cpp b/src/station_gui.cpp index a4804528c..764d34748 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -207,7 +207,6 @@ protected: static bool include_empty; // whether we should include stations without waiting cargo static const CargoTypes cargo_filter_max; static CargoTypes cargo_filter; // bitmap of cargo types to include - static const Station *last_station; /* Constants for sorting stations */ static const StringID sorter_names[]; @@ -259,19 +258,7 @@ protected: /** Sort stations by their name */ static bool StationNameSorter(const Station * const &a, const Station * const &b) { - static char buf_cache[64]; - char buf[64]; - - SetDParam(0, a->index); - GetString(buf, STR_STATION_NAME, lastof(buf)); - - if (b != last_station) { - last_station = b; - SetDParam(0, b->index); - GetString(buf_cache, STR_STATION_NAME, lastof(buf_cache)); - } - - int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting). + int r = strnatcmp(a->GetCachedName(), b->GetCachedName()); // Sort by name (natural sorting). if (r == 0) return a->index < b->index; return r < 0; } @@ -343,9 +330,6 @@ protected: { if (!this->stations.Sort()) return; - /* Reset name sorter sort cache */ - this->last_station = nullptr; - /* Set the modified widget dirty */ this->SetWidgetDirty(WID_STL_LIST); } @@ -703,7 +687,6 @@ byte CompanyStationsWindow::facilities = FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_ bool CompanyStationsWindow::include_empty = true; const CargoTypes CompanyStationsWindow::cargo_filter_max = ALL_CARGOTYPES; CargoTypes CompanyStationsWindow::cargo_filter = ALL_CARGOTYPES; -const Station *CompanyStationsWindow::last_station = nullptr; /* Available station sorting functions */ GUIStationList::SortFunction * const CompanyStationsWindow::sorter_funcs[] = { @@ -1222,21 +1205,13 @@ bool CargoSorter::SortCount(const CargoDataEntry *cd1, const CargoDataEntry *cd2 bool CargoSorter::SortStation(StationID st1, StationID st2) const { - static char buf1[MAX_LENGTH_STATION_NAME_CHARS]; - static char buf2[MAX_LENGTH_STATION_NAME_CHARS]; - if (!Station::IsValidID(st1)) { return Station::IsValidID(st2) ? this->order == SO_ASCENDING : this->SortId(st1, st2); } else if (!Station::IsValidID(st2)) { return order == SO_DESCENDING; } - SetDParam(0, st1); - GetString(buf1, STR_STATION_NAME, lastof(buf1)); - SetDParam(0, st2); - GetString(buf2, STR_STATION_NAME, lastof(buf2)); - - int res = strnatcmp(buf1, buf2); // Sort by name (natural sorting). + int res = strnatcmp(Station::Get(st1)->GetCachedName(), Station::Get(st2)->GetCachedName()); // Sort by name (natural sorting). if (res == 0) { return this->SortId(st1, st2); } else { diff --git a/src/town_gui.cpp b/src/town_gui.cpp index fc781fad0..1dcd65cb5 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -671,7 +671,6 @@ struct TownDirectoryWindow : public Window { private: /* Runtime saved values */ static Listing last_sorting; - static const Town *last_town; /* Constants for sorting towns */ static const StringID sorter_names[]; @@ -710,7 +709,6 @@ private: this->vscroll->SetCount((uint)this->towns.size()); // Update scrollbar as well. } /* Always sort the towns. */ - this->last_town = nullptr; this->towns.Sort(); this->SetWidgetDirty(WID_TD_LIST); // Force repaint of the displayed towns. } @@ -718,22 +716,7 @@ private: /** Sort by town name */ static bool TownNameSorter(const Town * const &a, const Town * const &b) { - static char buf_cache[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH]; - char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH]; - - SetDParam(0, a->index); - GetString(buf, STR_TOWN_NAME, lastof(buf)); - - /* If 'b' is the same town as in the last round, use the cached value - * We do this to speed stuff up ('b' is called with the same value a lot of - * times after each other) */ - if (b != last_town) { - last_town = b; - SetDParam(0, b->index); - GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache)); - } - - return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting). + return strnatcmp(a->GetCachedName(), b->GetCachedName()) < 0; // Sort by name (natural sorting). } /** Sort by population (default descending, as big towns are of the most interest). */ @@ -1005,7 +988,6 @@ public: }; Listing TownDirectoryWindow::last_sorting = {false, 0}; -const Town *TownDirectoryWindow::last_town = nullptr; /** Names of the sorting functions. */ const StringID TownDirectoryWindow::sorter_names[] = { |