Improve performance of name sorting in town and station list windows

This commit is contained in:
Jonathan G Rennison
2019-05-12 18:03:57 +01:00
parent 748d73079a
commit f6b9395c6a
10 changed files with 81 additions and 38 deletions

View File

@@ -17,6 +17,8 @@
#include "viewport_type.h"
#include "station_map.h"
#include "core/geometry_type.hpp"
#include "core/alloc_type.hpp"
#include <memory>
typedef Pool<BaseStation, StationID, 32, 64000> StationPool;
extern StationPool _station_pool;
@@ -59,6 +61,7 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> {
char *name; ///< Custom name
StringID string_id; ///< Default name (town area) of station
std::unique_ptr<const char, FreeDeleter> 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
OwnerByte owner; ///< The owner of this station
@@ -113,6 +116,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) const_cast<BaseStation *>(this)->FillCachedName();
return this->cached_name.get();
}
virtual void MoveSign(TileIndex new_xy)
{
this->xy = new_xy;
@@ -176,6 +186,9 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> {
}
static void PostDestructor(size_t index);
private:
void FillCachedName();
};
#define FOR_ALL_BASE_STATIONS(var) FOR_ALL_ITEMS_FROM(BaseStation, station_index, var, 0)