Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of 0x61C88647 constant in ThreadLocal.java

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;
like image 941
Adrian Avatar asked Dec 14 '22 04:12

Adrian


2 Answers

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.

like image 109
Sinkingpoint Avatar answered Dec 17 '22 00:12

Sinkingpoint


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

like image 31
贾宇晓 Avatar answered Dec 17 '22 00:12

贾宇晓