The hash function implementation of array in PHP
Today, we reviewed the method of implementing variable in PHP, and browsing its source code is to find all the data types in PHP through a union store.
The PHP language is a weakly typed language, and its implementation is managed by recording the type and value of the variable.
?
The most used non-array in PHP is, how is the array implemented?
In the PHP internal array is implemented by a Hashtable, which uses the link method to solve the problem of hash conflicts, in the worst case, the complexity of finding the array element is O (N), preferably 1.
?
And the method of calculating the hash value of the string is as follows, the source code is extracted for inspection:
PS: There are still two unknown points for the following functions:
1.? Why is the hash = 5381 set?
2.? Is this step=8 cycle a form of efficiency?
?
Static inline ulong Zend_inline_hash_func (const char *arkey, UINT nkeylength) {Register ULONG hash = 5381;?????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Is there any mystery about the setting of the initial value here? /* Variant with the hash unrolled eight times */for (; nkeylength >= 8; nkeylength-= 8) {?????????????????? What is the way of this step=8? hash = ((hash << 5) + hash) + *arkey++; hash = ((hash << 5) + hash) + *arkey++; hash = ((hash << 5) + hash) + *arkey++; hash = ((hash << 5) + hash) + *arkey++;?????????????????? Faster than direct *33 hash = ((hash << 5) + hash) + *arkey++; hash = ((hash << 5) + hash) + *arkey++; hash = ((hash << 5) + hash) + *arkey++; hash = ((hash << 5) + hash) + *arkey++; } switch (nkeylength) {Case 7:hash = ((hash << 5) + hash) + *arkey++;/* Fallthrough ... */????? ? ? ? ? ? ? ? ? ? Here is the remainder of the character hash case 6:hash = ((hash << 5) + hash) + *arkey++;/* Fallthrough ... */Case 5:hash = ((hash << 5) + hash) + *arkey++; /* Fallthrough ... */Case 4:hash = ((hash << 5) + hash) + *arkey++; /* Fallthrough ... */Case 3:hash = ((hash << 5) + hash) + *arkey++; /* Fallthrough ... */Case 2:hash = ((hash << 5) + hash) + *arkey++; /* Fallthrough ... */?????????? Case 1:hash = ((hash << 5) + hash) + *arkey++; Break Case 0:break; Empty_switch_default_case ()} return hash;????????????????????????????/ H Value}
?