Changeset - r28536:d61f9f1bb262
[Not reviewed]
master
0 3 0
Peter Nelson - 11 months ago 2024-01-21 14:09:44
peter1138@openttd.org
Codechange: Replace GroupStatistics' num_engines with std::map. (#11849)

This removes manual memory management with calloc/free calls, and prevents potentially large arrays being allocated for each group.
3 files changed with 19 insertions and 19 deletions:
0 comments (0 inline, 0 general)
src/group.h
Show inline comments
 
@@ -24,15 +24,12 @@ extern GroupPool _group_pool; ///< Pool 
 
struct GroupStatistics {
 
	Money profit_last_year;                 ///< Sum of profits for all vehicles.
 
	Money profit_last_year_min_age;         ///< Sum of profits for vehicles considered for profit statistics.
 
	uint16_t *num_engines;                    ///< Caches the number of engines of each type the company owns.
 
	std::map<EngineID, uint16_t> num_engines; ///< Caches the number of engines of each type the company owns.
 
	uint16_t num_vehicle;                     ///< Number of vehicles.
 
	uint16_t num_vehicle_min_age;             ///< Number of vehicles considered for profit statistics;
 
	bool autoreplace_defined;               ///< Are any autoreplace rules set?
 
	bool autoreplace_finished;              ///< Have all autoreplacement finished?
 

	
 
	GroupStatistics();
 
	~GroupStatistics();
 

	
 
	void Clear();
 

	
 
	void ClearProfits()
 
@@ -49,6 +46,8 @@ struct GroupStatistics {
 
		this->autoreplace_finished = false;
 
	}
 

	
 
	uint16_t GetNumEngines(EngineID engine) const;
 

	
 
	static GroupStatistics &Get(CompanyID company, GroupID id_g, VehicleType type);
 
	static GroupStatistics &Get(const Vehicle *v);
 
	static GroupStatistics &GetAllGroup(const Vehicle *v);
src/group_cmd.cpp
Show inline comments
 
@@ -27,16 +27,6 @@
 
GroupPool _group_pool("Group");
 
INSTANTIATE_POOL_METHODS(Group)
 

	
 
GroupStatistics::GroupStatistics()
 
{
 
	this->num_engines = CallocT<uint16_t>(Engine::GetPoolSize());
 
}
 

	
 
GroupStatistics::~GroupStatistics()
 
{
 
	free(this->num_engines);
 
}
 

	
 
/**
 
 * Clear all caches.
 
 */
 
@@ -47,9 +37,20 @@ void GroupStatistics::Clear()
 
	this->num_vehicle_min_age = 0;
 
	this->profit_last_year_min_age = 0;
 

	
 
	/* This is also called when NewGRF change. So the number of engines might have changed. Reallocate. */
 
	free(this->num_engines);
 
	this->num_engines = CallocT<uint16_t>(Engine::GetPoolSize());
 
	/* This is also called when NewGRF change. So the number of engines might have changed. Reset. */
 
	this->num_engines.clear();
 
}
 

	
 
/**
 
 * Get number of vehicles of a specific engine ID.
 
 * @param engine Engine ID.
 
 * @returns number of vehicles of this engine ID.
 
 */
 
uint16_t GroupStatistics::GetNumEngines(EngineID engine) const
 
{
 
	auto found = this->num_engines.find(engine);
 
	if (found != std::end(this->num_engines)) return found->second;
 
	return 0;
 
}
 

	
 
/**
 
@@ -800,7 +801,7 @@ uint GetGroupNumEngines(CompanyID compan
 
	for (const Group *g : Group::Iterate()) {
 
		if (g->parent == id_g) count += GetGroupNumEngines(company, g->index, id_e);
 
	}
 
	return count + GroupStatistics::Get(company, id_g, e->type).num_engines[id_e];
 
	return count + GroupStatistics::Get(company, id_g, e->type).GetNumEngines(id_e);
 
}
 

	
 
/**
src/script/api/script_engine.cpp
Show inline comments
 
@@ -32,7 +32,7 @@
 
	/* AIs have only access to engines they can purchase or still have in use.
 
	 * Deity has access to all engined that will be or were available ever. */
 
	CompanyID company = ScriptObject::GetCompany();
 
	return ScriptCompanyMode::IsDeity() || ::IsEngineBuildable(engine_id, e->type, company) || ::Company::Get(company)->group_all[e->type].num_engines[engine_id] > 0;
 
	return ScriptCompanyMode::IsDeity() || ::IsEngineBuildable(engine_id, e->type, company) || ::Company::Get(company)->group_all[e->type].GetNumEngines(engine_id) > 0;
 
}
 

	
 
/* static */ bool ScriptEngine::IsBuildable(EngineID engine_id)
0 comments (0 inline, 0 general)