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.
pull/132/head
Jonathan G Rennison 5 years ago committed by Charles Pigott
parent c3223903ed
commit d35f35a968

@ -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[] = {

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

@ -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[] = {

Loading…
Cancel
Save