Skip to content

Commit

Permalink
removed string lookup
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Figueroa Manrique <santiago.figueroa.manrique@alliander.com>
  • Loading branch information
figueroa1395 committed Nov 13, 2024
1 parent c509044 commit 366b424
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,13 @@ UpdateCompProperties check_component_independence(ConstDataset const& update_dat
template <typename... ComponentType> using ComponentFlags = std::array<bool, sizeof...(ComponentType)>;

template <class... ComponentType>
ComponentFlags<ComponentType...>
is_update_independent(ConstDataset const& update_data,
std::map<std::string, Idx, std::less<>> const& relevant_component_count_map) {
ComponentFlags<ComponentType...> is_update_independent(ConstDataset const& update_data,
std::span<bool const> relevant_component_count) {
ComponentFlags<ComponentType...> result{};
size_t idx{};
utils::run_functor_with_all_types_return_void<ComponentType...>(
[&result, &relevant_component_count_map, &update_data, &idx]<typename CompType>() {
Idx n_component{};
auto it = relevant_component_count_map.find(CompType::name);
if (it != relevant_component_count_map.end()) {
n_component = it->second;
}
[&result, &relevant_component_count, &update_data, &idx]<typename CompType>() {
Idx n_component = relevant_component_count[idx];
result[idx] = check_component_independence<CompType>(update_data, n_component).is_independent();
++idx;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
return result;
}

// helper function to get the number of components per type
std::array<bool, n_types> get_n_components_per_type() const {
std::array<bool, n_types> result{};
size_t idx{};
main_core::utils::run_functor_with_all_types_return_void<ComponentType...>(
[&result, this, &idx]<typename CompType>() {
result[idx] = this->component_count<CompType>();
++idx;
});
return result;
}

// helper function to add vectors of components
template <class CompType> void add_component(std::vector<typename CompType::InputType> const& components) {
add_component<CompType>(components.begin(), components.end());
Expand Down Expand Up @@ -580,10 +592,9 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis

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

return [&base_model, &exceptions, &infos, &calculation_fn, &result_data, &update_data,
Expand Down

0 comments on commit 366b424

Please sign in to comment.