Skip to content

Commit

Permalink
ParmParse: Refactoring II (AMReX-Codes#4035)
Browse files Browse the repository at this point in the history
Use std::unordered_map instead of std::vector. This speeds up
significantly when there are thousands of parameters.
  • Loading branch information
WeiqunZhang authored Jul 22, 2024
1 parent 4392b19 commit 0c3273f
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 180 deletions.
32 changes: 11 additions & 21 deletions Src/Base/AMReX_ParmParse.H
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#include <AMReX_Config.H>

#include <AMReX_BLassert.H>
#include <AMReX_INT.H>
#include <AMReX_TypeTraits.H>

#include <array>
#include <iosfwd>
#include <unordered_map>
#include <set>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -1032,8 +1034,15 @@ public:
//! Returns [prefix.]* parameters.
[[nodiscard]] static std::set<std::string> getEntries (const std::string& prefix = std::string());

struct PP_entry;
using Table = std::vector<PP_entry>;
struct PP_entry {
// There can be multiple occurrences for a given name (e.g.,
// multiple lines starting with `foo =` in inputs. For each
// occurrence, there can be multiple values. Thus, the use of
// vector<vector<std::string>>.
std::vector<std::vector<std::string>> m_vals;
mutable Long m_count = 0;
};
using Table = std::unordered_map<std::string, PP_entry>;

[[nodiscard]] const Table& table() const {return *m_table;}

Expand All @@ -1048,25 +1057,6 @@ protected:
Table* m_table;
};

struct ParmParse::PP_entry
{
PP_entry (std::string a_name, std::vector<std::string> a_vals)
: m_name(std::move(a_name)), m_vals(std::move(a_vals)) {}

PP_entry (std::string a_name, std::string const& a_val)
: m_name(std::move(a_name)), m_vals({a_val}) {}

[[nodiscard]] std::string const& name () const noexcept { return m_name; }

[[nodiscard]] std::string print() const;

std::string m_name;
std::vector<std::string> m_vals;
mutable bool m_unused = true;
};

std::ostream& operator<< (std::ostream& os, const ParmParse::PP_entry& pp);

}

#endif /* AMREX_PARMPARSE_H_ */
Loading

0 comments on commit 0c3273f

Please sign in to comment.