Improve performance of name sorting in industry list window

pull/91/head
Jonathan G Rennison 5 years ago
parent f6b9395c6a
commit b91ee6fb4b

@ -64,6 +64,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
PartOfSubsidyByte part_of_subsidy; ///< NOSAVE: is this industry a source/destination of a subsidy?
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
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));
}
inline const char *GetCachedName() const
{
if (!this->cached_name) const_cast<Industry *>(this)->FillCachedName();
return this->cached_name.get();
}
private:
void FillCachedName();
protected:
static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame
};
void ClearAllIndustryCachedNames();
void PlantRandomFarmField(const Industry *i);
void ReleaseDisastersTargetingIndustry(IndustryID);

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

@ -1253,19 +1253,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));
}
return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting).
return strnatcmp(a->GetCachedName(), b->GetCachedName()) < 0; // Sort by name (natural sorting).
}
/** Sort industries by type and name */

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

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

Loading…
Cancel
Save