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.
This commit is contained in:
Jonathan G Rennison
2020-01-06 20:48:02 +00:00
committed by Charles Pigott
parent c3223903ed
commit d35f35a968
3 changed files with 4 additions and 63 deletions

View File

@@ -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 {