Codechange: Cache resolved town, station and industry name strings

This commit is contained in:
Jonathan G Rennison 2020-01-06 20:40:31 +00:00 committed by Charles Pigott
parent f1734e7815
commit c3223903ed
10 changed files with 95 additions and 0 deletions

View File

@ -56,6 +56,7 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> {
char *name; ///< Custom name char *name; ///< Custom name
StringID string_id; ///< Default name (town area) of station StringID string_id; ///< Default name (town area) of station
mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the station, if not using a custom name
Town *town; ///< The town this station is associated with Town *town; ///< The town this station is associated with
Owner owner; ///< The owner of this station Owner owner; ///< The owner of this station
@ -108,6 +109,13 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> {
*/ */
virtual void UpdateVirtCoord() = 0; virtual void UpdateVirtCoord() = 0;
inline const char *GetCachedName() const
{
if (this->name != nullptr) return this->name;
if (this->cached_name.empty()) this->FillCachedName();
return this->cached_name.c_str();
}
virtual void MoveSign(TileIndex new_xy) virtual void MoveSign(TileIndex new_xy)
{ {
this->xy = new_xy; this->xy = new_xy;
@ -161,6 +169,9 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> {
} }
static void PostDestructor(size_t index); static void PostDestructor(size_t index);
private:
void FillCachedName() const;
}; };
/** /**

View File

@ -62,6 +62,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
PartOfSubsidy part_of_subsidy; ///< NOSAVE: is this industry a source/destination of a subsidy? PartOfSubsidy 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.
mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the industry
Owner founder; ///< Founder of the industry Owner 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
@ -157,10 +158,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.empty()) this->FillCachedName();
return this->cached_name.c_str();
}
private:
void FillCachedName() const;
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

@ -2319,6 +2319,21 @@ void Industry::RecomputeProductionMultipliers()
} }
} }
void Industry::FillCachedName() const
{
char buf[256];
int64 args_array[] = { this->index };
StringParameters tmp_params(args_array);
char *end = GetStringWithArgs(buf, STR_INDUSTRY_NAME, &tmp_params, lastof(buf));
this->cached_name.assign(buf, end);
}
void ClearAllIndustryCachedNames()
{
for (Industry *ind : Industry::Iterate()) {
ind->cached_name.clear();
}
}
/** /**
* 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

@ -222,6 +222,13 @@ void UpdateAllVirtCoords()
RebuildViewportKdtree(); RebuildViewportKdtree();
} }
void ClearAllCachedNames()
{
ClearAllStationCachedNames();
ClearAllTownCachedNames();
ClearAllIndustryCachedNames();
}
/** /**
* Initialization of the windows and several kinds of caches. * Initialization of the windows and several kinds of caches.
* This is not done directly in AfterLoadGame because these * This is not done directly in AfterLoadGame because these
@ -238,6 +245,7 @@ static void InitializeWindowsAndCaches()
SetupColoursAndInitialWindow(); SetupColoursAndInitialWindow();
/* Update coordinates of the signs. */ /* Update coordinates of the signs. */
ClearAllCachedNames();
UpdateAllVirtCoords(); UpdateAllVirtCoords();
ResetViewportAfterLoadGame(); ResetViewportAfterLoadGame();

View File

@ -526,6 +526,7 @@ struct GameOptionsWindow : Window {
ReadLanguagePack(&_languages[index]); ReadLanguagePack(&_languages[index]);
DeleteWindowByClass(WC_QUERY_STRING); DeleteWindowByClass(WC_QUERY_STRING);
CheckForMissingGlyphs(); CheckForMissingGlyphs();
ClearAllCachedNames();
UpdateAllVirtCoords(); UpdateAllVirtCoords();
ReInitAllWindows(); ReInitAllWindows();
break; break;

View File

@ -453,6 +453,22 @@ void UpdateAllStationVirtCoords()
} }
} }
void BaseStation::FillCachedName() const
{
char buf[MAX_LENGTH_STATION_NAME_CHARS * MAX_CHAR_LENGTH];
int64 args_array[] = { this->index };
StringParameters tmp_params(args_array);
char *end = GetStringWithArgs(buf, Waypoint::IsExpected(this) ? STR_WAYPOINT_NAME : STR_STATION_NAME, &tmp_params, lastof(buf));
this->cached_name.assign(buf, end);
}
void ClearAllStationCachedNames()
{
for (BaseStation *st : BaseStation::Iterate()) {
st->cached_name.clear();
}
}
/** /**
* Get a mask of the cargo types that the station accepts. * Get a mask of the cargo types that the station accepts.
* @param st Station to query * @param st Station to query
@ -3933,6 +3949,7 @@ CommandCost CmdRenameStation(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
} }
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
st->cached_name.clear();
free(st->name); free(st->name);
st->name = reset ? nullptr : stredup(text); st->name = reset ? nullptr : stredup(text);

View File

@ -26,6 +26,7 @@ void FindStationsAroundTiles(const TileArea &location, StationList *stations, bo
void ShowStationViewWindow(StationID station); void ShowStationViewWindow(StationID station);
void UpdateAllStationVirtCoords(); void UpdateAllStationVirtCoords();
void ClearAllStationCachedNames();
CargoArray GetProductionAroundTiles(TileIndex tile, int w, int h, int rad); CargoArray GetProductionAroundTiles(TileIndex tile, int w, int h, int rad);
CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad, CargoTypes *always_accepted = nullptr); CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad, CargoTypes *always_accepted = nullptr);

View File

@ -60,6 +60,7 @@ struct Town : TownPool::PoolItem<&_town_pool> {
uint16 townnametype; uint16 townnametype;
uint32 townnameparts; uint32 townnameparts;
char *name; ///< Custom town name. If nullptr, the town was not renamed and uses the generated name. char *name; ///< Custom town name. If nullptr, the town was not renamed and uses the generated name.
mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the town, if not using a custom town name
byte flags; ///< See #TownFlags. byte flags; ///< See #TownFlags.
@ -130,6 +131,13 @@ struct Town : TownPool::PoolItem<&_town_pool> {
void UpdateVirtCoord(); void UpdateVirtCoord();
inline const char *GetCachedName() const
{
if (this->name != nullptr) return this->name;
if (this->cached_name.empty()) this->FillCachedName();
return this->cached_name.c_str();
}
static inline Town *GetByTile(TileIndex tile) static inline Town *GetByTile(TileIndex tile)
{ {
return Town::Get(GetTownIndex(tile)); return Town::Get(GetTownIndex(tile));
@ -137,11 +145,15 @@ struct Town : TownPool::PoolItem<&_town_pool> {
static Town *GetRandom(); static Town *GetRandom();
static void PostDestructor(size_t index); static void PostDestructor(size_t index);
private:
void FillCachedName() const;
}; };
uint32 GetWorldPopulation(); uint32 GetWorldPopulation();
void UpdateAllTownVirtCoords(); void UpdateAllTownVirtCoords();
void ClearAllTownCachedNames();
void ShowTownViewWindow(TownID town); void ShowTownViewWindow(TownID town);
void ExpandTown(Town *t); void ExpandTown(Town *t);

View File

@ -199,6 +199,13 @@ void Town::InitializeLayout(TownLayout layout)
return Town::Get(index); return Town::Get(index);
} }
void Town::FillCachedName() const
{
char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
char *end = GetTownName(buf, this, lastof(buf));
this->cached_name.assign(buf, end);
}
/** /**
* Get the cost for removing this house * Get the cost for removing this house
* @return the cost (inflation corrected etc) * @return the cost (inflation corrected etc)
@ -412,6 +419,13 @@ void UpdateAllTownVirtCoords()
} }
} }
void ClearAllTownCachedNames()
{
for (Town *t : Town::Iterate()) {
t->cached_name.clear();
}
}
/** /**
* Change the towns population * Change the towns population
* @param t Town which population has changed * @param t Town which population has changed
@ -2681,11 +2695,14 @@ CommandCost CmdRenameTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
} }
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
t->cached_name.clear();
free(t->name); free(t->name);
t->name = reset ? nullptr : stredup(text); t->name = reset ? nullptr : stredup(text);
t->UpdateVirtCoord(); t->UpdateVirtCoord();
InvalidateWindowData(WC_TOWN_DIRECTORY, 0, TDIWD_FORCE_RESORT); InvalidateWindowData(WC_TOWN_DIRECTORY, 0, TDIWD_FORCE_RESORT);
ClearAllStationCachedNames();
ClearAllIndustryCachedNames();
UpdateAllStationVirtCoords(); UpdateAllStationVirtCoords();
} }
return CommandCost(); return CommandCost();

View File

@ -74,6 +74,7 @@ bool ScrollMainWindowToTile(TileIndex tile, bool instant = false);
bool ScrollMainWindowTo(int x, int y, int z = -1, bool instant = false); bool ScrollMainWindowTo(int x, int y, int z = -1, bool instant = false);
void UpdateAllVirtCoords(); void UpdateAllVirtCoords();
void ClearAllCachedNames();
extern Point _tile_fract_coords; extern Point _tile_fract_coords;