mariusbancila / stduuid

A C++17 cross-platform implementation for UUIDs
MIT License
758 stars 112 forks source link

uuids::to_string and hash containers performances #41

Closed rickyviking closed 2 years ago

rickyviking commented 3 years ago

Hi,
using uuids as keys in stl unordered containers I have noticed a certain performance impact, both in filling and searching. I've come up with the conclusion that this is due to the hash function, which converts the uuids to their string representation and then computes the hash.
Here are some benchmarks I've run for 1'000'000 entries (on i7-7820X 3.6GHz):

operation millisecs
create uuids 85
to_string() 5183
fill std::set 771
fills std::unordered_set 11003
search std::set 942
search std::unordered_set 5455

As the numbers show, it's much faster both to fill and to search standard containers rather than hash ones, which is counter-intuitive (especially for the search part).

  1. is this an expected/known behavior?
  2. would it be possible for find a more efficient hashing algorithm?

[UPDATE] I've done a quick test replacing the hash algorithm with the simple one used here: updated results below are more in line with what you would expect from a hash container. So is there a specific design implementation behind the string version? Thanks!

operation millisecs
create uuids 84
to_string() 5134
fill std::set 783
fills std::unordered_set 336
search std::set 924
search std::unordered_set 261
mariusbancila commented 2 years ago

A faster implementation of std::hash<uuid> is now available.

Although probably now needed, the previous one is still available if you define the UUID_HASH_STRING_BASED macro.

rickyviking commented 2 years ago

Great, benchmarks with your new implementation are aligned to the ones I reported above.