6

I recently read an article about Equation Group's Sophisticated Hacking and the smoking gun is a constant which also appears in JDK 8 source code e.g. ThreadLocal.java

What is the meaning of HASH_INCREMENT constant and how does it improve performance?

/**
 * The difference between successively generated hash codes - turns
 * implicit sequential thread-local IDs into near-optimally spread
 * multiplicative hash values for power-of-two-sized tables.
 */
private static final int HASH_INCREMENT = 0x61c88647;
2
  • Your question is answered in the comment included in your question.
    – Kayaman
    Aug 17, 2016 at 10:43
  • 2
    @Kayaman As you can see in the answer, there's more behind it.
    – maaartinus
    Aug 17, 2016 at 11:38

2 Answers 2

16

TLDR: It's an example of Fibonacci hashing.

If you convert 0x61c88647 into decimal, you'll get 1640531527 which is nonsensical until you realise that in 32 bits, it's the signed version of 2654435769. Again this number seems a bit odd until you realise that it is 232 ÷ φ where φ is the golden ratio (√5+1)÷2.

Now how does this fit into ThreadLocal? When you create a new ThreadLocal it is assigned an ID based on the previous id + our magic number. It's put into a ThreadLocalMap. In the event of a clash, the ThreadLocalMap puts the value into the next available space. Our magic value allows an optimal "spreading out" of the values in this hash in order to avoid this.

2
  • 1
    That "until you realise" made me weep. Thanks.
    – Joop Eggen
    Aug 17, 2016 at 10:57
  • The mathematics of hashing algorithms will do that to someone. Aug 17, 2016 at 10:58
1

0x61c88647 = 1640531527 ≈ 2 ^ 32 * (1 - 1 / φ), φ = (√5 + 1) ÷ 2, it is an another Golden ratio Num of 32 bits.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.