From c3223903ed4f39abe8589355882b30b973da434e Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 6 Jan 2020 20:40:31 +0000 Subject: Codechange: Cache resolved town, station and industry name strings --- src/base_station_base.h | 11 +++++++++++ src/industry.h | 12 ++++++++++++ src/industry_cmd.cpp | 15 +++++++++++++++ src/saveload/afterload.cpp | 8 ++++++++ src/settings_gui.cpp | 1 + src/station_cmd.cpp | 17 +++++++++++++++++ src/station_func.h | 1 + src/town.h | 12 ++++++++++++ src/town_cmd.cpp | 17 +++++++++++++++++ src/viewport_func.h | 1 + 10 files changed, 95 insertions(+) (limited to 'src') diff --git a/src/base_station_base.h b/src/base_station_base.h index a1c935fce..0467866e5 100644 --- a/src/base_station_base.h +++ b/src/base_station_base.h @@ -56,6 +56,7 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> { char *name; ///< Custom name 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 Owner owner; ///< The owner of this station @@ -108,6 +109,13 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> { */ 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) { this->xy = new_xy; @@ -161,6 +169,9 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> { } static void PostDestructor(size_t index); + +private: + void FillCachedName() const; }; /** diff --git a/src/industry.h b/src/industry.h index f462cf482..e82033dd1 100644 --- a/src/industry.h +++ b/src/industry.h @@ -62,6 +62,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { PartOfSubsidy part_of_subsidy; ///< NOSAVE: is this industry a source/destination of a subsidy? 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 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)); } + inline const char *GetCachedName() const + { + if (this->cached_name.empty()) this->FillCachedName(); + return this->cached_name.c_str(); + } + +private: + void FillCachedName() const; + protected: static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame }; +void ClearAllIndustryCachedNames(); + void PlantRandomFarmField(const Industry *i); void ReleaseDisastersTargetingIndustry(IndustryID); diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 6dce3c0ce..dfc43500f 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -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. diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index c37c297c5..022fd28e9 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -222,6 +222,13 @@ void UpdateAllVirtCoords() RebuildViewportKdtree(); } +void ClearAllCachedNames() +{ + ClearAllStationCachedNames(); + ClearAllTownCachedNames(); + ClearAllIndustryCachedNames(); +} + /** * Initialization of the windows and several kinds of caches. * This is not done directly in AfterLoadGame because these @@ -238,6 +245,7 @@ static void InitializeWindowsAndCaches() SetupColoursAndInitialWindow(); /* Update coordinates of the signs. */ + ClearAllCachedNames(); UpdateAllVirtCoords(); ResetViewportAfterLoadGame(); diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index fbccc1d43..968be54f6 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -526,6 +526,7 @@ struct GameOptionsWindow : Window { ReadLanguagePack(&_languages[index]); DeleteWindowByClass(WC_QUERY_STRING); CheckForMissingGlyphs(); + ClearAllCachedNames(); UpdateAllVirtCoords(); ReInitAllWindows(); break; diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 3b92f814b..adc2b0084 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -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. * @param st Station to query @@ -3933,6 +3949,7 @@ CommandCost CmdRenameStation(TileIndex tile, DoCommandFlag flags, uint32 p1, uin } if (flags & DC_EXEC) { + st->cached_name.clear(); free(st->name); st->name = reset ? nullptr : stredup(text); diff --git a/src/station_func.h b/src/station_func.h index e7a32b8a5..f9959089e 100644 --- a/src/station_func.h +++ b/src/station_func.h @@ -26,6 +26,7 @@ void FindStationsAroundTiles(const TileArea &location, StationList *stations, bo void ShowStationViewWindow(StationID station); void UpdateAllStationVirtCoords(); +void ClearAllStationCachedNames(); CargoArray GetProductionAroundTiles(TileIndex tile, int w, int h, int rad); CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad, CargoTypes *always_accepted = nullptr); diff --git a/src/town.h b/src/town.h index 9898e3fb6..8399fa63f 100644 --- a/src/town.h +++ b/src/town.h @@ -60,6 +60,7 @@ struct Town : TownPool::PoolItem<&_town_pool> { uint16 townnametype; uint32 townnameparts; 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. @@ -130,6 +131,13 @@ struct Town : TownPool::PoolItem<&_town_pool> { 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) { return Town::Get(GetTownIndex(tile)); @@ -137,11 +145,15 @@ struct Town : TownPool::PoolItem<&_town_pool> { static Town *GetRandom(); static void PostDestructor(size_t index); + +private: + void FillCachedName() const; }; uint32 GetWorldPopulation(); void UpdateAllTownVirtCoords(); +void ClearAllTownCachedNames(); void ShowTownViewWindow(TownID town); void ExpandTown(Town *t); diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 08ab17855..0a582eee7 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -199,6 +199,13 @@ void Town::InitializeLayout(TownLayout layout) 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 * @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 * @param t Town which population has changed @@ -2681,11 +2695,14 @@ CommandCost CmdRenameTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 } if (flags & DC_EXEC) { + t->cached_name.clear(); free(t->name); t->name = reset ? nullptr : stredup(text); t->UpdateVirtCoord(); InvalidateWindowData(WC_TOWN_DIRECTORY, 0, TDIWD_FORCE_RESORT); + ClearAllStationCachedNames(); + ClearAllIndustryCachedNames(); UpdateAllStationVirtCoords(); } return CommandCost(); diff --git a/src/viewport_func.h b/src/viewport_func.h index 0ac70b270..c47185323 100644 --- a/src/viewport_func.h +++ b/src/viewport_func.h @@ -74,6 +74,7 @@ bool ScrollMainWindowToTile(TileIndex tile, bool instant = false); bool ScrollMainWindowTo(int x, int y, int z = -1, bool instant = false); void UpdateAllVirtCoords(); +void ClearAllCachedNames(); extern Point _tile_fract_coords; -- cgit v1.2.3-54-g00ecf