boost::openmethod::policies::fast_perfect_hash
Hash type ids using a fast, perfect hash function.
Synopsis
Declared in <boost/openmethod/policies/fast_perfect_hash.hpp>
struct fast_perfect_hash
: type_hash
Description
fast_perfect_hash implements the type_hash policy using a hash function in the form H(x)=(M*x)>>S. It attempts to determine values for M and S that do not result in collisions for the set of registered type_ids. This may fail for certain sets of inputs, although it is very likely to succeed for addresses of std::type_info objects.
There is no guarantee that every value in the codomain of the function corresponds to a value in the domain, or even that the codomain is a dense range of integers. In other words, a lot of space may be wasted in presence of large sets of type_ids.
The code generated by x86‐64 clang 22 with ‐O2 for constructing a virtual_ptr from snoopy, an Animal& whose dynamic type is not known at compile time; st stands for the registry's state. No custom registry is needed: default_registry uses fast_perfect_hash.
virtual_ptr<Animal> animal = snoopy;
The highlighted instructions compute the hash:
mov rax, qword ptr [rdi] ; snoopy's v-table
#mov rdx, qword ptr [rip + st+48]#
#imul rdx, qword ptr [rax - 8]# ; [rax - 8] is &typeid(snoopy)
#movzx ecx, byte ptr [rip + st+56]#
#shr rdx, cl#
mov rax, qword ptr [rip + st+80]
mov rax, qword ptr [rax + 8*rdx]
Example
// `vptr_vector` indexes by the type id unless a `type_hash` policy maps it to
// a small integer first. With `std_rtti`, where a type id is a pointer, that
// makes the difference between a vector of a few entries and one that cannot
// be allocated at all.
struct hashed_registry :
registry<
policies::std_rtti, policies::fast_perfect_hash,
policies::vptr_vector> {};