-
Notifications
You must be signed in to change notification settings - Fork 415
AMREX_ENUM
: Cache String Map
#4643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Conversation
The `getEnumNameValuePairs` logic used below a lot of `AMREX_ENUM` functions is very slow for long-enough (e.g., N>70) enums. In WarpX 1D simulations, it took up close to 50% of the runtime, as Dave Grote found. This caches the key-value string list on first creation, because the type is known at compile-time. A better implementation might skip the dictionary altogether for functions that just need one string, like `getEnumNameString`.
Src/Base/AMReX_Enum.H
Outdated
@@ -52,7 +52,7 @@ namespace amrex | |||
std::enable_if_t<ET::value,int> = 0> | |||
T getEnum (std::string_view const& s) | |||
{ | |||
auto const& kv = getEnumNameValuePairs<T>(); | |||
static auto const& kv = getEnumNameValuePairs<T>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not get what the original &
was aiming to achieve here, because the getEnumNameValuePairs
function returns by value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice thx. TIL.
I think it still needs to go now that it is static (?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, although I am not against using const
, I use it less frequently than you. Anyway, always const is certainly wrong. Here is an example.
std::vector<int> make_vector()
{
std::vector<int> const r(...);
// code using r
return r;
}
auto r = make_vector();
That const
will prevent move, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is correct. There is a similar case preferring by-value arg passing (over const &
).
I think we should create a helper function to avoid multiple statics. Something like
|
Can we even create an optimized version of |
Depending the size, maybe using std::map or std::unordered_map is better than std::vector. But it's hard to avoid building the whole list. |
Summary
The
getEnumNameValuePairs
logic used below a lot ofAMREX_ENUM
functions is very slow for long-enough (e.g., N>70) enums.In WarpX 1D simulations, it took up close to 50% of the runtime, as Dave Grote found.
This caches the key-value string list on first creation, because the type is known at compile-time.
A better implementation might skip the dictionary altogether for functions that just need one string, like
getEnumNameString
.Additional background
BLAST-WarpX/warpx#6139
Checklist
The proposed changes: