@@ -111,6 +111,16 @@ std::vector<WarHistory const*> DiplomaticHistoryManager::get_wars(Date date) con
111111bool DiplomaticHistoryManager::load_diplomacy_history_file (
112112 CountryDefinitionManager const & country_definition_manager, ast::NodeCPtr root
113113) {
114+ // Default vanilla alliances is 2, 54 is the max I've seen in mods
115+ // Eliminates reallocations at the cost of memory usage
116+ alliances.reserve (16 );
117+ // Default vanilla subjects is 42, 152 is the max I've seen in mods
118+ // Eliminates reallocations at the cost of memory usage
119+ subjects.reserve (64 );
120+ // Default vanilla reparations is 0, yet to see a mod use it
121+ // Eliminates reallocations at the cost of memory usage
122+ reparations.reserve (1 );
123+
114124 return expect_dictionary_keys (
115125 " alliance" , ZERO_OR_MORE, [this , &country_definition_manager](ast::NodeCPtr node) -> bool {
116126 CountryDefinition const * first = nullptr ;
@@ -129,7 +139,7 @@ bool DiplomaticHistoryManager::load_diplomacy_history_file(
129139 " end_date" , ZERO_OR_ONE, expect_date_identifier_or_string (assign_variable_callback (end))
130140 )(node);
131141
132- alliances.push_back ({ first, second, { start, end } });
142+ alliances.emplace_back ( first, second, Period { start, end });
133143 return ret;
134144 },
135145 " vassal" , ZERO_OR_MORE, [this , &country_definition_manager](ast::NodeCPtr node) -> bool {
@@ -149,7 +159,7 @@ bool DiplomaticHistoryManager::load_diplomacy_history_file(
149159 " end_date" , ZERO_OR_ONE, expect_date_identifier_or_string (assign_variable_callback (end))
150160 )(node);
151161
152- subjects.push_back ({ overlord, subject, SubjectHistory::type_t ::VASSAL, { start, end } });
162+ subjects.emplace_back ( overlord, subject, SubjectHistory::type_t ::VASSAL, Period { start, end });
153163 return ret;
154164 },
155165 " union" , ZERO_OR_MORE, [this , &country_definition_manager](ast::NodeCPtr node) -> bool {
@@ -169,7 +179,7 @@ bool DiplomaticHistoryManager::load_diplomacy_history_file(
169179 " end_date" , ZERO_OR_ONE, expect_date_identifier_or_string (assign_variable_callback (end))
170180 )(node);
171181
172- subjects.push_back ({ overlord, subject, SubjectHistory::type_t ::UNION, { start, end } });
182+ subjects.emplace_back ( overlord, subject, SubjectHistory::type_t ::UNION, Period { start, end });
173183 return ret;
174184 },
175185 " substate" , ZERO_OR_MORE, [this , &country_definition_manager](ast::NodeCPtr node) -> bool {
@@ -189,7 +199,7 @@ bool DiplomaticHistoryManager::load_diplomacy_history_file(
189199 " end_date" , ZERO_OR_ONE, expect_date_identifier_or_string (assign_variable_callback (end))
190200 )(node);
191201
192- subjects.push_back ({ overlord, subject, SubjectHistory::type_t ::SUBSTATE, { start, end } });
202+ subjects.emplace_back ( overlord, subject, SubjectHistory::type_t ::SUBSTATE, Period { start, end });
193203 return ret;
194204 },
195205 " reparations" , ZERO_OR_MORE, [this , &country_definition_manager](ast::NodeCPtr node) -> bool {
@@ -209,29 +219,45 @@ bool DiplomaticHistoryManager::load_diplomacy_history_file(
209219 " end_date" , ZERO_OR_ONE, expect_date_identifier_or_string (assign_variable_callback (end))
210220 )(node);
211221
212- reparations.push_back ({ receiver, sender, { start, end } });
222+ reparations.emplace_back ( receiver, sender, Period { start, end });
213223 return ret;
214224 }
215225 )(root);
216226}
217227
218228bool DiplomaticHistoryManager::load_war_history_file (DefinitionManager const & definition_manager, ast::NodeCPtr root) {
219229 std::string_view name {};
220- std::vector<WarHistory::war_participant_t > attackers {};
221- std::vector<WarHistory::war_participant_t > defenders {};
222- std::vector<WarHistory::added_wargoal_t > wargoals {};
230+
231+ // Ensures that if ever multithreaded, only one vector is used per thread
232+ // Else acts like static
233+ thread_local std::vector<WarHistory::war_participant_t > attackers;
234+ // Default max vanilla attackers is 1, 1 is the max I've seen in mods
235+ // Eliminates reallocations
236+ attackers.reserve (1 );
237+
238+ thread_local std::vector<WarHistory::war_participant_t > defenders;
239+ // Default max vanilla defenders is 1, 1 is the max I've seen in mods
240+ // Eliminates reallocations
241+ defenders.reserve (1 );
242+
243+ thread_local std::vector<WarHistory::added_wargoal_t > wargoals;
244+ // Default max vanilla wargoals is 2, 2 is the max I've seen in mods
245+ // Eliminates reallocations
246+ wargoals.reserve (1 );
247+
223248 Date current_date {};
224249
250+
225251 bool ret = expect_dictionary_keys_and_default (
226- [&definition_manager, &attackers, &defenders, &wargoals, & current_date, &name](
252+ [&definition_manager, ¤t_date, &name](
227253 std::string_view key, ast::NodeCPtr node
228254 ) -> bool {
229255 bool ret = expect_date_str (assign_variable_callback (current_date))(key);
230256
231257 ret &= expect_dictionary_keys (
232258 " add_attacker" , ZERO_OR_MORE,
233259 definition_manager.get_country_definition_manager ().expect_country_definition_identifier (
234- [&attackers, & current_date, &name](CountryDefinition const & country) -> bool {
260+ [¤t_date, &name](CountryDefinition const & country) -> bool {
235261 for (auto const & attacker : attackers) {
236262 if (attacker.get_country () == &country) {
237263 Logger::error (
@@ -243,13 +269,13 @@ bool DiplomaticHistoryManager::load_war_history_file(DefinitionManager const& de
243269 }
244270 }
245271
246- attackers.push_back ({ &country, { current_date, {} } });
272+ attackers.emplace_back ( &country, Period { current_date, {} });
247273 return true ;
248274 }
249275 ),
250276 " add_defender" , ZERO_OR_MORE,
251277 definition_manager.get_country_definition_manager ().expect_country_definition_identifier (
252- [&defenders, & current_date, &name](CountryDefinition const & country) -> bool {
278+ [¤t_date, &name](CountryDefinition const & country) -> bool {
253279 for (auto const & defender : defenders) {
254280 if (defender.get_country () == &country) {
255281 Logger::error (
@@ -261,13 +287,13 @@ bool DiplomaticHistoryManager::load_war_history_file(DefinitionManager const& de
261287 }
262288 }
263289
264- defenders.push_back ({ &country, { current_date, {} } });
290+ defenders.emplace_back ( &country, Period { current_date, {} });
265291 return true ;
266292 }
267293 ),
268294 " rem_attacker" , ZERO_OR_MORE,
269295 definition_manager.get_country_definition_manager ().expect_country_definition_identifier (
270- [&attackers, & current_date, &name](CountryDefinition const & country) -> bool {
296+ [¤t_date, &name](CountryDefinition const & country) -> bool {
271297 WarHistory::war_participant_t * participant_to_remove = nullptr ;
272298
273299 for (auto & attacker : attackers) {
@@ -291,7 +317,7 @@ bool DiplomaticHistoryManager::load_war_history_file(DefinitionManager const& de
291317 ),
292318 " rem_defender" , ZERO_OR_MORE,
293319 definition_manager.get_country_definition_manager ().expect_country_definition_identifier (
294- [&defenders, & current_date, &name](CountryDefinition const & country) -> bool {
320+ [¤t_date, &name](CountryDefinition const & country) -> bool {
295321 WarHistory::war_participant_t * participant_to_remove = nullptr ;
296322
297323 for (auto & defender : defenders) {
@@ -313,7 +339,7 @@ bool DiplomaticHistoryManager::load_war_history_file(DefinitionManager const& de
313339 return participant_to_remove->period .try_set_end (current_date);
314340 }
315341 ),
316- " war_goal" , ZERO_OR_MORE, [&definition_manager, &wargoals, & current_date](ast::NodeCPtr value) -> bool {
342+ " war_goal" , ZERO_OR_MORE, [&definition_manager, ¤t_date](ast::NodeCPtr value) -> bool {
317343 CountryDefinition const * actor = nullptr ;
318344 CountryDefinition const * receiver = nullptr ;
319345 WargoalType const * type = nullptr ;
@@ -341,7 +367,7 @@ bool DiplomaticHistoryManager::load_war_history_file(DefinitionManager const& de
341367 )
342368 )(value);
343369
344- wargoals.push_back ({ current_date, actor, receiver, type, third_party, target } );
370+ wargoals.emplace_back ( current_date, actor, receiver, type, third_party, target);
345371 return ret;
346372 }
347373 )(node);
@@ -351,7 +377,7 @@ bool DiplomaticHistoryManager::load_war_history_file(DefinitionManager const& de
351377 " name" , ZERO_OR_ONE, expect_string (assign_variable_callback (name))
352378 )(root);
353379
354- wars.push_back ({ name, std::move (attackers), std::move (defenders), std::move (wargoals) } );
380+ wars.emplace_back ( name, std::move (attackers), std::move (defenders), std::move (wargoals));
355381
356382 return ret;
357383}
0 commit comments