Skip to content

improve hash function in client_hash #33

@jaromil

Description

@jaromil

The hash function uses a simple bitmask of the file descriptor value (fd & 0x3FF), which is a weak hash for file descriptors. File descriptors are typically allocated sequentially starting from 3, meaning the first 1024 clients will have nearly perfect distribution, but if fds wrap around or are reused with certain patterns, collisions will increase.

While this is likely acceptable for the expected workload, consider using a better hash function (e.g., multiply by a prime and shift) for more uniform distribution. For example: ((fd * 2654435761u) >> 22) provides better mixing of bits.

    /* Use multiplicative hashing for better distribution of sequential fds */
    uint32_t ufd = (uint32_t)fd;
    uint32_t h = (ufd * 2654435761u) >> (32 - CLIENT_HASH_BITS);
    return h & CLIENT_HASH_MASK;

Originally posted by @Copilot in #29

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions