The hash function of array in PHP is cashed

Source: Internet
Author: User
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}
?
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.