OCCT-7.8 Hashing mechanism

Hello,

Could you please explain what is the difference between these methods:
std::hash, opencascade::hash and MurmurHash ?
In OCCT Upgrade guide you wrote that now you use STL hasher. However, in TopoDS_Shape, for example, you use these three different methods.
So the question is which one is better to use for our custom structs that we place in opencascade map?

Dmitrii Pasukhin's picture

Hello. Thank  you for your question. The most part of OCCT classes now be able to use inside STL hash maps. std::hash is a template that we override for the most part of our classes. Overriding include specific hashing algo, based on the recommendations. OpenCascade maps, like NCollection_Map and all other maps based on https://github.com/dpasukhi/OCCT/blob/3e00512ae6638fa7db3f8bc21b84305476... with 2 available operators.

A custom struct hashers samples (there can be custom hasher or std::hash) the main idea - return size_t and bool:

https://github.com/dpasukhi/OCCT/blob/3e00512ae6638fa7db3f8bc21b84305476...

https://github.com/dpasukhi/OCCT/blob/3e00512ae6638fa7db3f8bc21b84305476...

So, OCCT classes uses only specific hashers. But that hashers based on STL std::hash mechanism, which overrided by OCCT hashing algo :)

My recommendation - if you trying to achieve the hash for OCCT or standard classes - use std::hash, it will works fine. If you would like deep optimization for your class, you can use https://github.com/dpasukhi/OCCT/blob/3e00512ae6638fa7db3f8bc21b84305476...

Best regards, Dmitrii.

Elizaveta Krylova's picture

Thank you for your answer. But it still does not clear for me. What do you mean by saying "would like deep optimization for your class"? Why it can be better to use MurmurHash than hash from STL? What is the reason for deep optimization using murmurhash from OpenCascade in comparison with std::hash? I mean why do not you use only STL algorithm for all data structures?

Dmitrii Pasukhin's picture

STL have different implementation for the different compilers. I was trying to use the std::hash for native types as is and have up to 20% difference on performance for different compilers. MSVC and gcc have totally different approach for small types and pointers. OpenCascade hashing is platform independent.

And for most scenarios OCCT needs combining of hashes or symmetric hashing, that is not implemented into STL. Combining of the hash elements is a great optimisation heat.

Best regards, Dmitrii.

Elizaveta Krylova's picture

I see. So, for more stable code operation it is better to use OCCT hasher, right?

Dmitrii Pasukhin's picture

If you use multiplatform and different compilers or need functionality to combine hashes - better to use OCCT or boost with similar functionality. In case if you working only with single compiler and have needs only for native types (single element for hashing), the native hasher is enough.

Best regards, Dmitrii.

Elizaveta Krylova's picture

Got it, thank you for your detailed answers.