Created
November 17, 2013 08:45
-
-
Save johnislarry/7510944 to your computer and use it in GitHub Desktop.
wtf is hashcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hashCode() generation : | |
// | |
// Possibilities: | |
// * MD5Digest of {obj,stwRandom} | |
// * CRC32 of {obj,stwRandom} or any linear-feedback shift register function. | |
// * A DES- or AES-style SBox[] mechanism | |
// * One of the Phi-based schemes, such as: | |
// 2654435761 = 2^32 * Phi (golden ratio) | |
// HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stwRandom ; | |
// * A variation of Marsaglia's shift-xor RNG scheme. | |
// * (obj ^ stwRandom) is appealing, but can result | |
// in undesirable regularity in the hashCode values of adjacent objects | |
// (objects allocated back-to-back, in particular). This could potentially | |
// result in hashtable collisions and reduced hashtable efficiency. | |
// There are simple ways to "diffuse" the middle address bits over the | |
// generated hashCode values: | |
// | |
static inline intptr_t get_next_hash(Thread * Self, oop obj) { | |
intptr_t value = 0 ; | |
if (hashCode == 0) { | |
// This form uses an unguarded global Park-Miller RNG, | |
// so it's possible for two threads to race and generate the same RNG. | |
// On MP system we'll have lots of RW access to a global, so the | |
// mechanism induces lots of coherency traffic. | |
value = os::random() ; | |
} else | |
if (hashCode == 1) { | |
// This variation has the property of being stable (idempotent) | |
// between STW operations. This can be useful in some of the 1-0 | |
// synchronization schemes. | |
intptr_t addrBits = intptr_t(obj) >> 3 ; | |
value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ; | |
} else | |
if (hashCode == 2) { | |
value = 1 ; // for sensitivity testing | |
} else | |
if (hashCode == 3) { | |
value = ++GVars.hcSequence ; | |
} else | |
if (hashCode == 4) { | |
value = intptr_t(obj) ; | |
} else { | |
// Marsaglia's xor-shift scheme with thread-specific state | |
// This is probably the best overall implementation -- we'll | |
// likely make this the default in future releases. | |
unsigned t = Self->_hashStateX ; | |
t ^= (t << 11) ; | |
Self->_hashStateX = Self->_hashStateY ; | |
Self->_hashStateY = Self->_hashStateZ ; | |
Self->_hashStateZ = Self->_hashStateW ; | |
unsigned v = Self->_hashStateW ; | |
v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ; | |
Self->_hashStateW = v ; | |
value = v ; | |
} | |
value &= markOopDesc::hash_mask; | |
if (value == 0) value = 0xBAD ; | |
assert (value != markOopDesc::no_hash, "invariant") ; | |
TEVENT (hashCode: GENERATE) ; | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment