From 98f80503e11d30861d9fe1079138544ac7866680 Mon Sep 17 00:00:00 2001 From: Herman Semenov Date: Tue, 7 Nov 2023 23:27:23 +0000 Subject: [PATCH] Optimize generate_story_ids using reserve and refactor function code (#1504) --- src/xrGame/alife_simulator_script.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/xrGame/alife_simulator_script.cpp b/src/xrGame/alife_simulator_script.cpp index 64ef844a3ba..2fdb5de572d 100644 --- a/src/xrGame/alife_simulator_script.cpp +++ b/src/xrGame/alife_simulator_script.cpp @@ -74,27 +74,25 @@ void generate_story_ids(STORY_PAIRS& result, _id_type INVALID_ID, LPCSTR section { result.clear(); - CInifile* Ini = pGameIni; + const CInifile* Ini = pGameIni; LPCSTR N, V; - u32 k; - shared_str temp; - LPCSTR section = section_name; - R_ASSERT(Ini->section_exist(section)); + u32 k = 0; + R_ASSERT(Ini->section_exist(section_name)); - for (k = 0; Ini->r_line(section, k, &N, &V); ++k) + result.reserve(Ini->line_count(section_name) + 1); + while (Ini->r_line(section_name, k, &N, &V)) { - temp = Ini->r_string_wb(section, N); + const shared_str& temp = Ini->r_string_wb(section_name, N); R_ASSERT3(!strchr(*temp, ' '), invalid_id_description, *temp); R_ASSERT2(xr_strcmp(*temp, INVALID_ID_STRING), invalid_id_redefinition); - STORY_PAIRS::const_iterator I = result.begin(); - STORY_PAIRS::const_iterator E = result.end(); - for (; I != E; ++I) - R_ASSERT3((*I).first != temp, duplicated_id_description, *temp); + for (const auto& story : result) + R_ASSERT3(story.first != temp, duplicated_id_description, *temp); result.emplace_back(*temp, atoi(N)); + ++k; } result.emplace_back(INVALID_ID_STRING, INVALID_ID);