From 5192708bda86625a40ce67ff297dca8138f9cc13 Mon Sep 17 00:00:00 2001 From: hop311 Date: Sat, 20 Apr 2024 23:16:45 +0100 Subject: Pre-allocate and use std::move for Godot Arrays and Dictionaries --- .../singletons/PopulationMenu.cpp | 39 ++++++++++++++-------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'extension/src/openvic-extension/singletons/PopulationMenu.cpp') diff --git a/extension/src/openvic-extension/singletons/PopulationMenu.cpp b/extension/src/openvic-extension/singletons/PopulationMenu.cpp index f97cae6..a598ceb 100644 --- a/extension/src/openvic-extension/singletons/PopulationMenu.cpp +++ b/extension/src/openvic-extension/singletons/PopulationMenu.cpp @@ -575,9 +575,10 @@ TypedArray MenuSingleton::get_population_menu_pop_rows(int32_t start // TODO - monthly change TypedArray array; + ERR_FAIL_COND_V(array.resize(count) != OK, {}); - for (int32_t idx = start; idx < start + count; ++idx) { - Pop const* pop = population_menu.filtered_pops[idx]; + for (int32_t idx = 0; idx < count; ++idx) { + Pop const* pop = population_menu.filtered_pops[start + idx]; Dictionary pop_dict; pop_dict[pop_size_key] = pop->get_size(); @@ -598,7 +599,7 @@ TypedArray MenuSingleton::get_population_menu_pop_rows(int32_t start pop_dict[pop_size_change_key] = pop->get_total_change(); pop_dict[pop_literacy_key] = pop->get_literacy().to_float(); - array.push_back(pop_dict); + array[idx] = std::move(pop_dict); } return array; @@ -619,9 +620,10 @@ PackedInt32Array MenuSingleton::get_population_menu_pop_filter_setup_info() { ERR_FAIL_COND_V_MSG(population_menu.pop_filters.empty(), {}, "Failed to generate population menu pop filters!"); PackedInt32Array array; + ERR_FAIL_COND_V(array.resize(population_menu.pop_filters.size()) != OK, {}); - for (auto const& [pop_type, filter] : population_menu.pop_filters) { - array.push_back(pop_type->get_sprite()); + for (int32_t idx = 0; idx < array.size(); ++idx) { + array[idx] = population_menu.pop_filters.data()[idx].first->get_sprite(); } return array; @@ -633,13 +635,18 @@ TypedArray MenuSingleton::get_population_menu_pop_filter_info() cons static const StringName pop_filter_selected_key = "selected"; TypedArray array; + ERR_FAIL_COND_V(array.resize(population_menu.pop_filters.size()) != OK, {}); + + for (int32_t idx = 0; idx < array.size(); ++idx) { + population_menu_t::pop_filter_t const& filter = population_menu.pop_filters.data()[idx].second; - for (auto const& [pop_type, filter] : population_menu.pop_filters) { Dictionary filter_dict; + filter_dict[pop_filter_count_key] = filter.count; filter_dict[pop_filter_change_key] = filter.promotion_demotion_change; filter_dict[pop_filter_selected_key] = filter.selected; - array.push_back(filter_dict); + + array[idx] = std::move(filter_dict); } return array; @@ -688,17 +695,20 @@ void MenuSingleton::population_menu_deselect_all_pop_filters() { PackedStringArray MenuSingleton::get_population_menu_distribution_setup_info() const { static const PackedStringArray distribution_names = []() -> PackedStringArray { - PackedStringArray array; - - for (char const* name : std::array { + constexpr std::array NAMES { /* Workforce (PopType) */ "WORKFORCE_DISTTITLE", /* Religion */ "RELIGION_DISTTITLE", /* Ideology */ "IDEOLOGY_DISTTITLE", /* Nationality (Culture) */ "NATIONALITY_DISTTITLE", /* Issues */ "DOMINANT_ISSUES_DISTTITLE", /* Vote */ "ELECTORATE_DISTTITLE" - }) { - array.push_back(name); + }; + + PackedStringArray array; + ERR_FAIL_COND_V(array.resize(NAMES.size()) != OK, {}); + + for (int32_t idx = 0; idx < array.size(); ++idx) { + array[idx] = NAMES[idx]; } return array; @@ -709,9 +719,10 @@ PackedStringArray MenuSingleton::get_population_menu_distribution_setup_info() c TypedArray MenuSingleton::get_population_menu_distribution_info() const { TypedArray array; + ERR_FAIL_COND_V(array.resize(population_menu.distributions.size()) != OK, {}); - for (fixed_point_map_t const& distribution : population_menu.distributions) { - array.push_back(GFXPieChartTexture::distribution_to_slices_array(distribution)); + for (int32_t idx = 0; idx < array.size(); ++idx) { + array[idx] = GFXPieChartTexture::distribution_to_slices_array(population_menu.distributions[idx]); } return array; -- cgit v1.2.3-56-ga3b1