Skip to content

Commit 366b424

Browse files
committed
removed string lookup
Signed-off-by: Santiago Figueroa Manrique <santiago.figueroa.manrique@alliander.com>
1 parent c509044 commit 366b424

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/update.hpp

+4-9
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,13 @@ UpdateCompProperties check_component_independence(ConstDataset const& update_dat
235235
template <typename... ComponentType> using ComponentFlags = std::array<bool, sizeof...(ComponentType)>;
236236

237237
template <class... ComponentType>
238-
ComponentFlags<ComponentType...>
239-
is_update_independent(ConstDataset const& update_data,
240-
std::map<std::string, Idx, std::less<>> const& relevant_component_count_map) {
238+
ComponentFlags<ComponentType...> is_update_independent(ConstDataset const& update_data,
239+
std::span<bool const> relevant_component_count) {
241240
ComponentFlags<ComponentType...> result{};
242241
size_t idx{};
243242
utils::run_functor_with_all_types_return_void<ComponentType...>(
244-
[&result, &relevant_component_count_map, &update_data, &idx]<typename CompType>() {
245-
Idx n_component{};
246-
auto it = relevant_component_count_map.find(CompType::name);
247-
if (it != relevant_component_count_map.end()) {
248-
n_component = it->second;
249-
}
243+
[&result, &relevant_component_count, &update_data, &idx]<typename CompType>() {
244+
Idx n_component = relevant_component_count[idx];
250245
result[idx] = check_component_independence<CompType>(update_data, n_component).is_independent();
251246
++idx;
252247
});

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
186186
return result;
187187
}
188188

189+
// helper function to get the number of components per type
190+
std::array<bool, n_types> get_n_components_per_type() const {
191+
std::array<bool, n_types> result{};
192+
size_t idx{};
193+
main_core::utils::run_functor_with_all_types_return_void<ComponentType...>(
194+
[&result, this, &idx]<typename CompType>() {
195+
result[idx] = this->component_count<CompType>();
196+
++idx;
197+
});
198+
return result;
199+
}
200+
189201
// helper function to add vectors of components
190202
template <class CompType> void add_component(std::vector<typename CompType::InputType> const& components) {
191203
add_component<CompType>(components.begin(), components.end());
@@ -580,10 +592,9 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
580592

581593
// cache component update order where possible.
582594
// the order for a cacheable (independent) component by definition is the same across all scenarios
583-
// TODO: (figueroa1395): don't use string lookup, it is slow
584-
std::map<std::string, Idx, std::less<>> const relevant_component_count_map = all_component_count();
595+
auto const relevant_component_count = get_n_components_per_type();
585596
auto const is_independent =
586-
main_core::is_update_independent<ComponentType...>(update_data, relevant_component_count_map);
597+
main_core::is_update_independent<ComponentType...>(update_data, relevant_component_count);
587598
all_scenarios_sequence = get_sequence_idx_map(update_data, 0, is_independent);
588599

589600
return [&base_model, &exceptions, &infos, &calculation_fn, &result_data, &update_data,

0 commit comments

Comments
 (0)