You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While profiling code comparing two flat_hash_map instances, I noticed that during the execution of operator==, each key seems to be compared twice:
First, when calling find() on the second map to locate the corresponding element.
Then, when comparing the value_type (i.e., std::pair<const Key, Value>) objects themselves, which compares both key and value. Since the key has already been looked up via find(), this second key comparison seems redundant.
Is this expected behavior for compatibility/generalization reasons, or would you consider optimizing operator== to avoid comparing the key twice when possible?
Example of optimized pseudocode (to avoid redundant key comparison):
if (map1.size() != map2.size()) returnfalse; for (constauto& [k, v] : map1) { auto it = map2.find(k); if (it == map2.end() || it->second != v) returnfalse; } returntrue;
I’m aware that the current implementation probably favors code generality and reusability, but given that flat_hash_map aims for performance, this might be an area worth considering.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
While profiling code comparing two flat_hash_map instances, I noticed that during the execution of operator==, each key seems to be compared twice:
First, when calling find() on the second map to locate the corresponding element.
Then, when comparing the value_type (i.e., std::pair<const Key, Value>) objects themselves, which compares both key and value.
Since the key has already been looked up via find(), this second key comparison seems redundant.
Is this expected behavior for compatibility/generalization reasons, or would you consider optimizing operator== to avoid comparing the key twice when possible?
Example of optimized pseudocode (to avoid redundant key comparison):
I’m aware that the current implementation probably favors code generality and reusability, but given that flat_hash_map aims for performance, this might be an area worth considering.
What’s your take on this?
Thanks again for your work!
Beta Was this translation helpful? Give feedback.
All reactions