Improve performance of name sorting in industry list window

This commit is contained in:
Jonathan G Rennison
2019-05-12 18:55:25 +01:00
parent f6b9395c6a
commit b91ee6fb4b
5 changed files with 34 additions and 13 deletions

View File

@@ -64,6 +64,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
PartOfSubsidyByte part_of_subsidy; ///< NOSAVE: is this industry a source/destination of a subsidy? PartOfSubsidyByte part_of_subsidy; ///< NOSAVE: is this industry a source/destination of a subsidy?
StationList stations_near; ///< NOSAVE: List of nearby stations. StationList stations_near; ///< NOSAVE: List of nearby stations.
std::unique_ptr<const char, FreeDeleter> cached_name; ///< NOSAVE: Cache of the resolved name of the industry
OwnerByte founder; ///< Founder of the industry OwnerByte founder; ///< Founder of the industry
Date construction_date; ///< Date of the construction of the industry Date construction_date; ///< Date of the construction of the industry
@@ -159,10 +160,21 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
memset(&counts, 0, sizeof(counts)); memset(&counts, 0, sizeof(counts));
} }
inline const char *GetCachedName() const
{
if (!this->cached_name) const_cast<Industry *>(this)->FillCachedName();
return this->cached_name.get();
}
private:
void FillCachedName();
protected: protected:
static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame
}; };
void ClearAllIndustryCachedNames();
void PlantRandomFarmField(const Industry *i); void PlantRandomFarmField(const Industry *i);
void ReleaseDisastersTargetingIndustry(IndustryID); void ReleaseDisastersTargetingIndustry(IndustryID);

View File

@@ -2280,6 +2280,25 @@ void Industry::RecomputeProductionMultipliers()
} }
} }
void Industry::FillCachedName()
{
char buf[256];
int64 args_array[] = { this->index };
StringParameters tmp_params(args_array);
char *end = GetStringWithArgs(buf, STR_INDUSTRY_NAME, &tmp_params, lastof(buf));
char *alloced = MallocT<char>(end - buf + 1);
memcpy(alloced, buf, end - buf + 1);
this->cached_name.reset(alloced);
}
void ClearAllIndustryCachedNames()
{
Industry *ind;
FOR_ALL_INDUSTRIES(ind) {
ind->cached_name.reset();
}
}
/** /**
* Set the #probability and #min_number fields for the industry type \a it for a running game. * Set the #probability and #min_number fields for the industry type \a it for a running game.

View File

@@ -1253,19 +1253,7 @@ protected:
/** Sort industries by name */ /** Sort industries by name */
static bool IndustryNameSorter(const Industry * const &a, const Industry * const &b) static bool IndustryNameSorter(const Industry * const &a, const Industry * const &b)
{ {
static char buf_cache[96]; return strnatcmp(a->GetCachedName(), b->GetCachedName()) < 0; // Sort by name (natural sorting).
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));
}
return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting).
} }
/** Sort industries by type and name */ /** Sort industries by type and name */

View File

@@ -236,6 +236,7 @@ void ClearAllCachedNames()
{ {
ClearAllStationCachedNames(); ClearAllStationCachedNames();
ClearAllTownCachedNames(); ClearAllTownCachedNames();
ClearAllIndustryCachedNames();
} }
/** /**

View File

@@ -2888,6 +2888,7 @@ CommandCost CmdRenameTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
t->UpdateVirtCoord(); t->UpdateVirtCoord();
InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 1); InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 1);
ClearAllStationCachedNames(); ClearAllStationCachedNames();
ClearAllIndustryCachedNames();
UpdateAllStationVirtCoords(); UpdateAllStationVirtCoords();
} }
return CommandCost(); return CommandCost();