php的hash演算法介紹

來源:互聯網
上載者:User

 PHP的Hash採用的是目前最為普遍的DJBX33A (Daniel J. Bernstein, Times 33 with Addition), 這個演算法被廣泛運用與多個軟體項目,Apache, Perl和Berkeley DB等。對於字串而言這是目前所知道的最好的雜湊演算法,原因在於該演算法的速度非常快,而且分類非常好(衝突小,分布均勻)

Hash Table是PHP的核心,這話一點都不過分。 PHP的數組,關聯陣列,對象屬性,函數表,符號表,等等都是用HashTable來做為容器的。 PHP的HashTable採用的拉鏈法來解決衝突, 這個自不用多說, 我今天主要關注的就是PHP的Hash演算法, 和這個演算法本身透露出來的一些思想。 PHP的Hash採用的是目前最為普遍的DJBX33A (Daniel J. Bernstein, Times 33 with Addition), 這個演算法被廣泛運用與多個軟體項目,Apache, Perl和Berkeley DB等. 對於字串而言這是目前所知道的最好的雜湊演算法,原因在於該演算法的速度非常快,而且分類非常好(衝突小,分布均勻). 演算法的核心思想就是: 代碼如下:hash(i) = hash(i-1) * 33 + str[i]  在zend_hash.h中,我們可以找到在PHP中的這個演算法:  代碼如下:static inline ulong zend_inline_hash_func(char *arKey, uint nKeyLength){    register ulong hash = 5381;     /* variant with the hash unrolled eight times */    for (; nKeyLength >= 8; nKeyLength -=  {        hash = ((hash << 5) + hash) + *arKey++;        hash = ((hash << 5) + hash) + *arKey++;        hash = ((hash << 5) + hash) + *arKey++;        hash = ((hash << 5) + hash) + *arKey++;        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... */        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;}  相比在Apache和Perl中直接採用的經典Times 33演算法:  代碼如下:hashing function used in Perl 5.005:  # Return the hashed value of a string: $hash = perlhash("key")  # (Defined by the PERL_HASH macro in hv.h)  sub perlhash  {      $hash = 0;      foreach (split //, shift) {          $hash = $hash*33 + ord($_);      }      return $hash;  }  在PHP的hash演算法中, 我們可以看出很處細緻的不同. 首先, 最不一樣的就是, PHP中並沒有使用直接乘33, 而是採用了:  代碼如下:hash << 5 + hash  這樣當然會比用乘快了. 然後, 特別要主意的就是使用的unrolled, 我前幾天看過一片文章講Discuz的緩衝機制, 其中就有一條說是Discuz會根據文章的熱度不同採用不同的緩衝策略, 根據使用者習慣,而只緩衝文章的第一頁(因為很少有人會翻文章). 於此類似的思想, PHP鼓勵8位一下的字元索引, 他以8為單位使用unrolled來提高效率, 這不得不說也是個很細節的,很細緻的地方. 另外還有inline, register變數 … 可以看出PHP的開發人員在hash的最佳化上也是煞費苦心 最後就是, hash的初始值設定成了5381, 相比在Apache中的times演算法和Perl中的Hash演算法(都採用初始hash為0), 為什麼選5381呢? 具體的原因我也不知道, 但是我發現了5381的一些特性: 代碼如下:Magic Constant 5381:1. odd number2. prime number3. deficient number 看了這些, 我有理由相信這個初始值的選定能提供更好的分類. 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.