Gre*_*reg 79
它似乎仅受脚本内存限制的限制.
快速测试给我一个128mb的关键没问题:
ini_set('memory_limit', '1024M');
$key = str_repeat('x', 1024 * 1024 * 128);
$foo = array($key => $key);
echo strlen(key($foo)) . "<br>";
echo strlen($foo[$key]) . "<br>";
Run Code Online (Sandbox Code Playgroud)
在zend_hash.h中,您可以找到zend_inline_hash_func()可以显示如何在PHP中散列键字符串的方法,因此使用小于8个字符的字符串长度的键对于性能更好.
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 -= 8) {
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;
}
Run Code Online (Sandbox Code Playgroud)