From a41fb1f9f787a11ee4323b9c81ca6a6d0ba1c011 Mon Sep 17 00:00:00 2001 From: Florian Weik Date: Tue, 6 Apr 2021 15:56:21 +0200 Subject: [PATCH 1/2] core: Avoid redundant hash table lookups in particle type map --- src/core/particle_data.cpp | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/core/particle_data.cpp b/src/core/particle_data.cpp index a185fa71eff..49d2a9371d0 100644 --- a/src/core/particle_data.cpp +++ b/src/core/particle_data.cpp @@ -1142,39 +1142,48 @@ void init_type_map(int type) { if (type < 0) throw std::runtime_error("Types may not be negative"); - // fill particle map - if (particle_type_map.count(type) == 0) - particle_type_map[type] = std::unordered_set(); - + auto &map_for_type = particle_type_map[type]; for (auto const &p : partCfg()) { if (p.p.type == type) - particle_type_map.at(type).insert(p.p.identity); + map_for_type.insert(p.p.identity); } } void remove_id_from_map(int part_id, int type) { - if (particle_type_map.find(type) != particle_type_map.end()) - particle_type_map.at(type).erase(part_id); + auto it = particle_type_map.find(type); + if (it != particle_type_map.end()) + it->second.erase(part_id); } int get_random_p_id(int type, int random_index_in_type_map) { - if (random_index_in_type_map + 1 > particle_type_map.at(type).size()) + auto it = particle_type_map.find(type); + if (it == particle_type_map.end()) { + throw std::runtime_error("The provided particle type " + + std::to_string(type) + + " is currently not tracked by the system."); + } + + if (random_index_in_type_map + 1 > it->second.size()) throw std::runtime_error("The provided index exceeds the number of " "particle types listed in the particle_type_map"); - return *std::next(particle_type_map[type].begin(), random_index_in_type_map); + return *std::next(it->second.begin(), random_index_in_type_map); } void add_id_to_type_map(int part_id, int type) { - if (particle_type_map.find(type) != particle_type_map.end()) - particle_type_map.at(type).insert(part_id); + auto it = particle_type_map.find(type); + if (it != particle_type_map.end()) + it->second.insert(part_id); } int number_of_particles_with_type(int type) { - if (particle_type_map.count(type) == 0) + auto it = particle_type_map.find(type); + if (it == particle_type_map.end()) { throw std::runtime_error("The provided particle type " + std::to_string(type) + " is currently not tracked by the system."); - return static_cast(particle_type_map.at(type).size()); + } + + return static_cast(it->second.size()); } // The following functions are used by the python interface to obtain From 9640f74460065e8a7cb68e009fd619d02aa4b9b4 Mon Sep 17 00:00:00 2001 From: Florian Weik Date: Wed, 7 Apr 2021 10:13:29 +0200 Subject: [PATCH 2/2] core: Clear type map on initialization --- src/core/particle_data.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/particle_data.cpp b/src/core/particle_data.cpp index 49d2a9371d0..bf1d9127394 100644 --- a/src/core/particle_data.cpp +++ b/src/core/particle_data.cpp @@ -1143,6 +1143,7 @@ void init_type_map(int type) { throw std::runtime_error("Types may not be negative"); auto &map_for_type = particle_type_map[type]; + map_for_type.clear(); for (auto const &p : partCfg()) { if (p.p.type == type) map_for_type.insert(p.p.identity);