What is the max key size for an array in PHP?

前端 未结 3 714
野趣味
野趣味 2020-11-28 12:41

I am generating associative arrays and the key value is a string concat of 1..n columns.

Is there a max length for keys that will come back to bite me? If so, I\'ll

3条回答
  •  醉话见心
    2020-11-28 13:17

    In zend_hash.h, you can find zend_inline_hash_func() method that can show how to hash key string in PHP, So use key which string length less than 8 characters is better for performance.

    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;   
    }
    

提交回复
热议问题