YaoRaoLov 2018-11-01 23:29 采纳率: 50%
浏览 415

什么是最快的,可移植的方式来哈希指针,我们知道是指针对准固定大小的整型?

If we have a set of pointers we know are aligned to sizeof(void *) whats that fastest way to hash them?

Notes:

  • Example use case is taking elements of a pointer array or memory allocations and storing in a hash-map. Noting this because this question isn't about the kind of cryptographic hashing needed for passwords, security etc.

  • By fixed size int, I mean we know the exact size of the int and it wont vary (perhaps this is important since some hashing libraries use intptr_t or size_t for their hashing return values which might give a different answer to this question).

  • By portable, this should work for 32, 64 bit, big & little endian.

  • (uint32_t)(((intptr_t)p) >> 2) gives good results for 32bit, big endian, however I imagine it looses significant bits for 64bit systems, and I'm not sure if this gives a usable distribution for little endian.

转载于:https://stackoverflow.com/questions/53110781/whats-the-fastest-portable-way-to-hash-pointers-we-know-are-pointer-aligned-to

  • 写回答

2条回答 默认 最新

  • 乱世@小熊 2018-11-02 00:00
    关注

    When mod math is fast, a quick hash is to mod by a prime <= TARGET_TYPE_MAX. The mod will use all the bits of the p to form the hash.

    By using the largest prime, only a few buckets are lost, but speed is the goal.

    Example, if the target tpye is uint32_t, use 4294967291u.

    With variant sized integer types like int, use macros to select the precomputed prime. Primes just less than a power of two.

    #define LARGEST_PRIME8 251u
    #define LARGEST_PRIME15 32749u
    #define LARGEST_PRIME16 65521u
    #define LARGEST_PRIME31 2147483647u
    #define LARGEST_PRIME32 4294967291u
    #define LARGEST_PRIME63 9223372036854775783u
    #define LARGEST_PRIME64 18446744073709551557u
    
    uint32_t hash = (uint32_t) ((uintptr_t)(void *)p) % LARGEST_PRIME32);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊