8240124: Better VM Interning

Reviewed-by: rehn, pliden, smarks, rhalade, ahgross, jwilhelm
This commit is contained in:
Coleen Phillimore 2020-04-28 12:07:35 -04:00 committed by Henry Jen
parent fa25d083be
commit a5cb23e29f
8 changed files with 365 additions and 286 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,13 +22,30 @@
* *
*/ */
/*
* halfsiphash code adapted from reference implementation
* (https://github.com/veorq/SipHash/blob/master/halfsiphash.c)
* which is distributed with the following copyright:
*
* SipHash reference C implementation
*
* Copyright (c) 2016 Jean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com>
*
* To the extent possible under law, the author(s) have dedicated all copyright
* and related and neighboring rights to this software to the public domain
* worldwide. This software is distributed without any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along
* with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include "precompiled.hpp" #include "precompiled.hpp"
#include "classfile/altHashing.hpp" #include "classfile/altHashing.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp" #include "classfile/systemDictionary.hpp"
#include "oops/markWord.hpp" #include "oops/markWord.hpp"
#include "oops/oop.inline.hpp" #include "oops/oop.inline.hpp"
#include "runtime/thread.hpp" #include "runtime/os.hpp"
// Get the hash code of the classes mirror if it exists, otherwise just // Get the hash code of the classes mirror if it exists, otherwise just
// return a random number, which is one of the possible hash code used for // return a random number, which is one of the possible hash code used for
@ -40,169 +57,176 @@ static intptr_t object_hash(Klass* k) {
} }
// Seed value used for each alternative hash calculated. // Seed value used for each alternative hash calculated.
juint AltHashing::compute_seed() { uint64_t AltHashing::compute_seed() {
jlong nanos = os::javaTimeNanos(); uint64_t nanos = os::javaTimeNanos();
jlong now = os::javaTimeMillis(); uint64_t now = os::javaTimeMillis();
jint SEED_MATERIAL[8] = { uint32_t SEED_MATERIAL[8] = {
(jint) object_hash(SystemDictionary::String_klass()), (uint32_t) object_hash(SystemDictionary::String_klass()),
(jint) object_hash(SystemDictionary::System_klass()), (uint32_t) object_hash(SystemDictionary::System_klass()),
(jint) os::random(), // current thread isn't a java thread (uint32_t) os::random(), // current thread isn't a java thread
(jint) (((julong)nanos) >> 32), (uint32_t) (((uint64_t)nanos) >> 32),
(jint) nanos, (uint32_t) nanos,
(jint) (((julong)now) >> 32), (uint32_t) (((uint64_t)now) >> 32),
(jint) now, (uint32_t) now,
(jint) (os::javaTimeNanos() >> 2) (uint32_t) (os::javaTimeNanos() >> 2)
}; };
return murmur3_32(SEED_MATERIAL, 8); return halfsiphash_64(SEED_MATERIAL, 8);
} }
// utility function copied from java/lang/Integer
// Murmur3 hashing for Symbol static uint32_t Integer_rotateLeft(uint32_t i, int distance) {
juint AltHashing::murmur3_32(juint seed, const jbyte* data, int len) { return (i << distance) | (i >> (32 - distance));
juint h1 = seed;
int count = len;
int offset = 0;
// body
while (count >= 4) {
juint k1 = (data[offset] & 0x0FF)
| (data[offset + 1] & 0x0FF) << 8
| (data[offset + 2] & 0x0FF) << 16
| data[offset + 3] << 24;
count -= 4;
offset += 4;
k1 *= 0xcc9e2d51;
k1 = Integer_rotateLeft(k1, 15);
k1 *= 0x1b873593;
h1 ^= k1;
h1 = Integer_rotateLeft(h1, 13);
h1 = h1 * 5 + 0xe6546b64;
}
// tail
if (count > 0) {
juint k1 = 0;
switch (count) {
case 3:
k1 ^= (data[offset + 2] & 0xff) << 16;
// fall through
case 2:
k1 ^= (data[offset + 1] & 0xff) << 8;
// fall through
case 1:
k1 ^= (data[offset] & 0xff);
// fall through
default:
k1 *= 0xcc9e2d51;
k1 = Integer_rotateLeft(k1, 15);
k1 *= 0x1b873593;
h1 ^= k1;
}
}
// finalization
h1 ^= len;
// finalization mix force all bits of a hash block to avalanche
h1 ^= h1 >> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >> 16;
return h1;
} }
// Murmur3 hashing for Strings static void halfsiphash_rounds(uint32_t v[4], int rounds) {
juint AltHashing::murmur3_32(juint seed, const jchar* data, int len) { while (rounds-- > 0) {
juint h1 = seed; v[0] += v[1];
v[1] = Integer_rotateLeft(v[1], 5);
v[1] ^= v[0];
v[0] = Integer_rotateLeft(v[0], 16);
v[2] += v[3];
v[3] = Integer_rotateLeft(v[3], 8);
v[3] ^= v[2];
v[0] += v[3];
v[3] = Integer_rotateLeft(v[3], 7);
v[3] ^= v[0];
v[2] += v[1];
v[1] = Integer_rotateLeft(v[1], 13);
v[1] ^= v[2];
v[2] = Integer_rotateLeft(v[2], 16);
}
}
static void halfsiphash_adddata(uint32_t v[4], uint32_t newdata, int rounds) {
v[3] ^= newdata;
halfsiphash_rounds(v, rounds);
v[0] ^= newdata;
}
static void halfsiphash_init32(uint32_t v[4], uint64_t seed) {
v[0] = seed & 0xffffffff;
v[1] = seed >> 32;
v[2] = 0x6c796765 ^ v[0];
v[3] = 0x74656462 ^ v[1];
}
static void halfsiphash_init64(uint32_t v[4], uint64_t seed) {
halfsiphash_init32(v, seed);
v[1] ^= 0xee;
}
static uint64_t halfsiphash_finish64(uint32_t v[4], int rounds) {
uint64_t rv;
v[2] ^= 0xee;
halfsiphash_rounds(v, rounds);
rv = v[1] ^ v[3];
v[1] ^= 0xdd;
halfsiphash_rounds(v, rounds);
rv |= (uint64_t)(v[1] ^ v[3]) << 32;
return rv;
}
// HalfSipHash-2-4 (64-bit output) for Symbols
uint64_t AltHashing::halfsiphash_64(uint64_t seed, const int8_t* data, int len) {
uint32_t v[4];
uint32_t newdata;
int off = 0; int off = 0;
int count = len; int count = len;
halfsiphash_init64(v, seed);
// body // body
while (count >= 2) { while (count >= 4) {
jchar d1 = data[off++] & 0xFFFF;
jchar d2 = data[off++];
juint k1 = (d1 | d2 << 16);
count -= 2; // Avoid sign extension with 0x0ff
newdata = (data[off] & 0x0FF)
| (data[off + 1] & 0x0FF) << 8
| (data[off + 2] & 0x0FF) << 16
| data[off + 3] << 24;
k1 *= 0xcc9e2d51; count -= 4;
k1 = Integer_rotateLeft(k1, 15); off += 4;
k1 *= 0x1b873593;
h1 ^= k1; halfsiphash_adddata(v, newdata, 2);
h1 = Integer_rotateLeft(h1, 13);
h1 = h1 * 5 + 0xe6546b64;
} }
// tail // tail
newdata = ((uint32_t)len) << 24; // (Byte.SIZE / Byte.SIZE);
if (count > 0) { if (count > 0) {
juint k1 = (juint)data[off]; switch (count) {
case 3:
k1 *= 0xcc9e2d51; newdata |= (data[off + 2] & 0x0ff) << 16;
k1 = Integer_rotateLeft(k1, 15); // fall through
k1 *= 0x1b873593; case 2:
h1 ^= k1; newdata |= (data[off + 1] & 0x0ff) << 8;
// fall through
case 1:
newdata |= (data[off] & 0x0ff);
// fall through
}
} }
halfsiphash_adddata(v, newdata, 2);
// finalization // finalization
h1 ^= len * 2; // (Character.SIZE / Byte.SIZE); return halfsiphash_finish64(v, 4);
// finalization mix force all bits of a hash block to avalanche
h1 ^= h1 >> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >> 16;
return h1;
} }
// Hash used for the seed. // HalfSipHash-2-4 (64-bit output) for Strings
juint AltHashing::murmur3_32(juint seed, const jint* data, int len) { uint64_t AltHashing::halfsiphash_64(uint64_t seed, const uint16_t* data, int len) {
juint h1 = seed; uint32_t v[4];
uint32_t newdata;
int off = 0;
int count = len;
halfsiphash_init64(v, seed);
// body
while (count >= 2) {
uint16_t d1 = data[off++] & 0x0FFFF;
uint16_t d2 = data[off++];
newdata = (d1 | d2 << 16);
count -= 2;
halfsiphash_adddata(v, newdata, 2);
}
// tail
newdata = ((uint32_t)len * 2) << 24; // (Character.SIZE / Byte.SIZE);
if (count > 0) {
newdata |= (uint32_t)data[off];
}
halfsiphash_adddata(v, newdata, 2);
// finalization
return halfsiphash_finish64(v, 4);
}
// HalfSipHash-2-4 (64-bit output) for integers (used to create seed)
uint64_t AltHashing::halfsiphash_64(uint64_t seed, const uint32_t* data, int len) {
uint32_t v[4];
int off = 0; int off = 0;
int end = len; int end = len;
halfsiphash_init64(v, seed);
// body // body
while (off < end) { while (off < end) {
juint k1 = (juint)data[off++]; halfsiphash_adddata(v, (uint32_t)data[off++], 2);
k1 *= 0xcc9e2d51;
k1 = Integer_rotateLeft(k1, 15);
k1 *= 0x1b873593;
h1 ^= k1;
h1 = Integer_rotateLeft(h1, 13);
h1 = h1 * 5 + 0xe6546b64;
} }
// tail (always empty, as body is always 32-bit chunks) // tail (always empty, as body is always 32-bit chunks)
// finalization // finalization
halfsiphash_adddata(v, ((uint32_t)len * 4) << 24, 2); // (Integer.SIZE / Byte.SIZE);
h1 ^= len * 4; // (Integer.SIZE / Byte.SIZE); return halfsiphash_finish64(v, 4);
// finalization mix force all bits of a hash block to avalanche
h1 ^= h1 >> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >> 16;
return h1;
} }
juint AltHashing::murmur3_32(const jint* data, int len) { // HalfSipHash-2-4 (64-bit output) for integers (used to create seed)
return murmur3_32(0, data, len); uint64_t AltHashing::halfsiphash_64(const uint32_t* data, int len) {
return halfsiphash_64((uint64_t)0, data, len);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,29 +26,25 @@
#define SHARE_CLASSFILE_ALTHASHING_HPP #define SHARE_CLASSFILE_ALTHASHING_HPP
#include "jni.h" #include "jni.h"
#include "classfile/symbolTable.hpp" #include "memory/allocation.hpp"
/** /**
* Hashing utilities. * Implementation of alternate more secure hashing.
*
* Implementation of Murmur3 hashing.
* This code was translated from src/share/classes/sun/misc/Hashing.java
* code in the JDK.
*/ */
class AltHashing : AllStatic { class AltHashing : AllStatic {
friend class AltHashingTest; friend class AltHashingTest;
// utility function copied from java/lang/Integer // For the seed computation
static juint Integer_rotateLeft(juint i, int distance) { static uint64_t halfsiphash_64(const uint32_t* data, int len);
return (i << distance) | (i >> (32 - distance)); static uint64_t halfsiphash_64(uint64_t seed, const uint32_t* data, int len);
}
static juint murmur3_32(const jint* data, int len);
static juint murmur3_32(juint seed, const jint* data, int len);
public: public:
static juint compute_seed(); static uint64_t compute_seed();
static juint murmur3_32(juint seed, const jbyte* data, int len);
static juint murmur3_32(juint seed, const jchar* data, int len); // For Symbols
static uint64_t halfsiphash_64(uint64_t seed, const int8_t* data, int len);
// For Strings
static uint64_t halfsiphash_64(uint64_t seed, const uint16_t* data, int len);
}; };
#endif // SHARE_CLASSFILE_ALTHASHING_HPP #endif // SHARE_CLASSFILE_ALTHASHING_HPP

View File

@ -91,11 +91,11 @@ static size_t _current_size = 0;
static volatile size_t _items_count = 0; static volatile size_t _items_count = 0;
volatile bool _alt_hash = false; volatile bool _alt_hash = false;
static juint murmur_seed = 0; static uint64_t _alt_hash_seed = 0;
uintx hash_string(const jchar* s, int len, bool useAlt) { uintx hash_string(const jchar* s, int len, bool useAlt) {
return useAlt ? return useAlt ?
AltHashing::murmur3_32(murmur_seed, s, len) : AltHashing::halfsiphash_64(_alt_hash_seed, s, len) :
java_lang_String::hash_code(s, len); java_lang_String::hash_code(s, len);
} }
@ -523,7 +523,7 @@ void StringTable::rehash_table() {
return; return;
} }
murmur_seed = AltHashing::compute_seed(); _alt_hash_seed = AltHashing::compute_seed();
{ {
if (do_rehash()) { if (do_rehash()) {
rehashed = true; rehashed = true;

View File

@ -96,7 +96,7 @@ static volatile bool _lookup_shared_first = false;
// Static arena for symbols that are not deallocated // Static arena for symbols that are not deallocated
Arena* SymbolTable::_arena = NULL; Arena* SymbolTable::_arena = NULL;
static juint murmur_seed = 0; static uint64_t _alt_hash_seed = 0;
static inline void log_trace_symboltable_helper(Symbol* sym, const char* msg) { static inline void log_trace_symboltable_helper(Symbol* sym, const char* msg) {
#ifndef PRODUCT #ifndef PRODUCT
@ -108,7 +108,7 @@ static inline void log_trace_symboltable_helper(Symbol* sym, const char* msg) {
// Pick hashing algorithm. // Pick hashing algorithm.
static uintx hash_symbol(const char* s, int len, bool useAlt) { static uintx hash_symbol(const char* s, int len, bool useAlt) {
return useAlt ? return useAlt ?
AltHashing::murmur3_32(murmur_seed, (const jbyte*)s, len) : AltHashing::halfsiphash_64(_alt_hash_seed, (const int8_t*)s, len) :
java_lang_String::hash_code((const jbyte*)s, len); java_lang_String::hash_code((const jbyte*)s, len);
} }
@ -785,7 +785,7 @@ void SymbolTable::rehash_table() {
return; return;
} }
murmur_seed = AltHashing::compute_seed(); _alt_hash_seed = AltHashing::compute_seed();
if (do_rehash()) { if (do_rehash()) {
rehashed = true; rehashed = true;

View File

@ -156,7 +156,7 @@ void StringDedupEntryCache::free(StringDedupEntry* entry, uint worker_id) {
assert(worker_id < _nlists, "Invalid worker id"); assert(worker_id < _nlists, "Invalid worker id");
entry->set_obj(NULL); entry->set_obj(NULL);
entry->set_hash(0); entry->set_java_hash(0, false /* latin1 */);
if (_cached[worker_id].length() < _max_list_length) { if (_cached[worker_id].length() < _max_list_length) {
// Cache is not full // Cache is not full
@ -224,7 +224,7 @@ StringDedupTable* StringDedupTable::_resized_table = NULL;
StringDedupTable* StringDedupTable::_rehashed_table = NULL; StringDedupTable* StringDedupTable::_rehashed_table = NULL;
volatile size_t StringDedupTable::_claimed_index = 0; volatile size_t StringDedupTable::_claimed_index = 0;
StringDedupTable::StringDedupTable(size_t size, jint hash_seed) : StringDedupTable::StringDedupTable(size_t size, uint64_t hash_seed) :
_size(size), _size(size),
_entries(0), _entries(0),
_shrink_threshold((uintx)(size * _shrink_load_factor)), _shrink_threshold((uintx)(size * _shrink_load_factor)),
@ -246,11 +246,14 @@ void StringDedupTable::create() {
_table = new StringDedupTable(_min_size); _table = new StringDedupTable(_min_size);
} }
void StringDedupTable::add(typeArrayOop value, bool latin1, unsigned int hash, StringDedupEntry** list) { void StringDedupTable::add(typeArrayOop value, bool latin1, uint64_t hash, StringDedupEntry** list) {
StringDedupEntry* entry = _entry_cache->alloc(); StringDedupEntry* entry = _entry_cache->alloc();
entry->set_obj(value); entry->set_obj(value);
entry->set_hash(hash); if (use_java_hash()) {
entry->set_latin1(latin1); entry->set_java_hash((unsigned int)hash, latin1);
} else {
entry->set_alt_hash(hash);
}
entry->set_next(*list); entry->set_next(*list);
*list = entry; *list = entry;
_entries++; _entries++;
@ -265,17 +268,18 @@ void StringDedupTable::remove(StringDedupEntry** pentry, uint worker_id) {
void StringDedupTable::transfer(StringDedupEntry** pentry, StringDedupTable* dest) { void StringDedupTable::transfer(StringDedupEntry** pentry, StringDedupTable* dest) {
StringDedupEntry* entry = *pentry; StringDedupEntry* entry = *pentry;
*pentry = entry->next(); *pentry = entry->next();
unsigned int hash = entry->hash(); uint64_t hash = use_java_hash() ? entry->java_hash() : entry->alt_hash();
size_t index = dest->hash_to_index(hash); size_t index = dest->hash_to_index(hash);
StringDedupEntry** list = dest->bucket(index); StringDedupEntry** list = dest->bucket(index);
entry->set_next(*list); entry->set_next(*list);
*list = entry; *list = entry;
} }
typeArrayOop StringDedupTable::lookup(typeArrayOop value, bool latin1, unsigned int hash, typeArrayOop StringDedupTable::lookup(typeArrayOop value, bool latin1, uint64_t hash,
StringDedupEntry** list, uintx &count) { StringDedupEntry** list, uintx &count) {
for (StringDedupEntry* entry = *list; entry != NULL; entry = entry->next()) { for (StringDedupEntry* entry = *list; entry != NULL; entry = entry->next()) {
if (entry->hash() == hash && entry->latin1() == latin1) { if ((use_java_hash() && entry->java_hash() == hash && entry->java_hash_latin1() == latin1) ||
(!use_java_hash() && entry->alt_hash() == hash)) {
oop* obj_addr = (oop*)entry->obj_addr(); oop* obj_addr = (oop*)entry->obj_addr();
oop obj = NativeAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(obj_addr); oop obj = NativeAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(obj_addr);
if (obj != NULL && java_lang_String::value_equals(value, static_cast<typeArrayOop>(obj))) { if (obj != NULL && java_lang_String::value_equals(value, static_cast<typeArrayOop>(obj))) {
@ -290,7 +294,7 @@ typeArrayOop StringDedupTable::lookup(typeArrayOop value, bool latin1, unsigned
return NULL; return NULL;
} }
typeArrayOop StringDedupTable::lookup_or_add_inner(typeArrayOop value, bool latin1, unsigned int hash) { typeArrayOop StringDedupTable::lookup_or_add_inner(typeArrayOop value, bool latin1, uint64_t hash) {
size_t index = hash_to_index(hash); size_t index = hash_to_index(hash);
StringDedupEntry** list = bucket(index); StringDedupEntry** list = bucket(index);
uintx count = 0; uintx count = 0;
@ -314,29 +318,31 @@ typeArrayOop StringDedupTable::lookup_or_add_inner(typeArrayOop value, bool lati
return existing_value; return existing_value;
} }
unsigned int StringDedupTable::hash_code(typeArrayOop value, bool latin1) { unsigned int StringDedupTable::java_hash_code(typeArrayOop value, bool latin1) {
assert(use_java_hash(), "Should not use java hash code");
unsigned int hash; unsigned int hash;
int length = value->length(); int length = value->length();
if (latin1) { if (latin1) {
const jbyte* data = (jbyte*)value->base(T_BYTE); const jbyte* data = (jbyte*)value->base(T_BYTE);
if (use_java_hash()) { hash = java_lang_String::hash_code(data, length);
hash = java_lang_String::hash_code(data, length);
} else {
hash = AltHashing::murmur3_32(_table->_hash_seed, data, length);
}
} else { } else {
length /= sizeof(jchar) / sizeof(jbyte); // Convert number of bytes to number of chars length /= sizeof(jchar) / sizeof(jbyte); // Convert number of bytes to number of chars
const jchar* data = (jchar*)value->base(T_CHAR); const jchar* data = (jchar*)value->base(T_CHAR);
if (use_java_hash()) { hash = java_lang_String::hash_code(data, length);
hash = java_lang_String::hash_code(data, length);
} else {
hash = AltHashing::murmur3_32(_table->_hash_seed, data, length);
}
} }
return hash; return hash;
} }
uint64_t StringDedupTable::alt_hash_code(typeArrayOop value) {
assert(!use_java_hash(), "Should not use alt hash code");
int length = value->length();
const jbyte* data = (jbyte*)value->base(T_BYTE);
return AltHashing::halfsiphash_64(_table->_hash_seed, (const int8_t*)data, length);
}
void StringDedupTable::deduplicate(oop java_string, StringDedupStat* stat) { void StringDedupTable::deduplicate(oop java_string, StringDedupStat* stat) {
assert(java_lang_String::is_instance(java_string), "Must be a string"); assert(java_lang_String::is_instance(java_string), "Must be a string");
NoSafepointVerifier nsv; NoSafepointVerifier nsv;
@ -351,7 +357,7 @@ void StringDedupTable::deduplicate(oop java_string, StringDedupStat* stat) {
} }
bool latin1 = java_lang_String::is_latin1(java_string); bool latin1 = java_lang_String::is_latin1(java_string);
unsigned int hash = 0; uint64_t hash = 0;
if (use_java_hash()) { if (use_java_hash()) {
if (!java_lang_String::hash_is_set(java_string)) { if (!java_lang_String::hash_is_set(java_string)) {
@ -360,7 +366,7 @@ void StringDedupTable::deduplicate(oop java_string, StringDedupStat* stat) {
hash = java_lang_String::hash_code(java_string); hash = java_lang_String::hash_code(java_string);
} else { } else {
// Compute hash // Compute hash
hash = hash_code(value, latin1); hash = alt_hash_code(value);
stat->inc_hashed(); stat->inc_hashed();
} }
@ -510,9 +516,9 @@ uintx StringDedupTable::unlink_or_oops_do(StringDedupUnlinkOrOopsDoClosure* cl,
// destination partitions. finish_rehash() will do a single // destination partitions. finish_rehash() will do a single
// threaded transfer of all entries. // threaded transfer of all entries.
typeArrayOop value = (typeArrayOop)*p; typeArrayOop value = (typeArrayOop)*p;
bool latin1 = (*entry)->latin1(); assert(!use_java_hash(), "Should not be using Java hash");
unsigned int hash = hash_code(value, latin1); uint64_t hash = alt_hash_code(value);
(*entry)->set_hash(hash); (*entry)->set_alt_hash(hash);
} }
// Move to next entry // Move to next entry
@ -606,9 +612,14 @@ void StringDedupTable::verify() {
guarantee(Universe::heap()->is_in(value), "Object must be on the heap"); guarantee(Universe::heap()->is_in(value), "Object must be on the heap");
guarantee(!value->is_forwarded(), "Object must not be forwarded"); guarantee(!value->is_forwarded(), "Object must not be forwarded");
guarantee(value->is_typeArray(), "Object must be a typeArrayOop"); guarantee(value->is_typeArray(), "Object must be a typeArrayOop");
bool latin1 = (*entry)->latin1(); uint64_t hash;
unsigned int hash = hash_code(value, latin1); if (use_java_hash()) {
guarantee((*entry)->hash() == hash, "Table entry has inorrect hash"); hash = (*entry)->java_hash();
guarantee(java_hash_code(value, (*entry)->java_hash_latin1()) == hash, "Table entry has incorrect hash");
} else {
hash = (*entry)->alt_hash();
guarantee(alt_hash_code(value) == hash, "Table entry has incorrect hash");
}
guarantee(_table->hash_to_index(hash) == bucket, "Table entry has incorrect index"); guarantee(_table->hash_to_index(hash) == bucket, "Table entry has incorrect index");
entry = (*entry)->next_addr(); entry = (*entry)->next_addr();
} }
@ -620,12 +631,14 @@ void StringDedupTable::verify() {
StringDedupEntry** entry1 = _table->bucket(bucket); StringDedupEntry** entry1 = _table->bucket(bucket);
while (*entry1 != NULL) { while (*entry1 != NULL) {
typeArrayOop value1 = (*entry1)->obj(); typeArrayOop value1 = (*entry1)->obj();
bool latin1_1 = (*entry1)->latin1();
StringDedupEntry** entry2 = (*entry1)->next_addr(); StringDedupEntry** entry2 = (*entry1)->next_addr();
while (*entry2 != NULL) { while (*entry2 != NULL) {
typeArrayOop value2 = (*entry2)->obj(); typeArrayOop value2 = (*entry2)->obj();
bool latin1_2 = (*entry2)->latin1(); guarantee(value1 != value2, "Table entries must not have the same array");
guarantee(latin1_1 != latin1_2 || !java_lang_String::value_equals(value1, value2), "Table entries must not have identical arrays"); if (use_java_hash()) {
guarantee((*entry1)->java_hash_latin1() != (*entry2)->java_hash_latin1() ||
!java_lang_String::value_equals(value1, value2), "Table entries must not have identical arrays");
}
entry2 = (*entry2)->next_addr(); entry2 = (*entry2)->next_addr();
} }
entry1 = (*entry1)->next_addr(); entry1 = (*entry1)->next_addr();
@ -647,6 +660,6 @@ void StringDedupTable::print_statistics() {
_table->_entries, percent_of((size_t)_table->_entries, _table->_size), _entry_cache->size(), _entries_added, _entries_removed); _table->_entries, percent_of((size_t)_table->_entries, _table->_size), _entry_cache->size(), _entries_added, _entries_removed);
log.debug(" Resize Count: " UINTX_FORMAT ", Shrink Threshold: " UINTX_FORMAT "(" STRDEDUP_PERCENT_FORMAT_NS "), Grow Threshold: " UINTX_FORMAT "(" STRDEDUP_PERCENT_FORMAT_NS ")", log.debug(" Resize Count: " UINTX_FORMAT ", Shrink Threshold: " UINTX_FORMAT "(" STRDEDUP_PERCENT_FORMAT_NS "), Grow Threshold: " UINTX_FORMAT "(" STRDEDUP_PERCENT_FORMAT_NS ")",
_resize_count, _table->_shrink_threshold, _shrink_load_factor * 100.0, _table->_grow_threshold, _grow_load_factor * 100.0); _resize_count, _table->_shrink_threshold, _shrink_load_factor * 100.0, _table->_grow_threshold, _grow_load_factor * 100.0);
log.debug(" Rehash Count: " UINTX_FORMAT ", Rehash Threshold: " UINTX_FORMAT ", Hash Seed: 0x%x", _rehash_count, _rehash_threshold, _table->_hash_seed); log.debug(" Rehash Count: " UINTX_FORMAT ", Rehash Threshold: " UINTX_FORMAT ", Hash Seed: " UINT64_FORMAT_X, _rehash_count, _rehash_threshold, _table->_hash_seed);
log.debug(" Age Threshold: " UINTX_FORMAT, StringDeduplicationAgeThreshold); log.debug(" Age Threshold: " UINTX_FORMAT, StringDeduplicationAgeThreshold);
} }

View File

@ -39,15 +39,16 @@ class StringDedupUnlinkOrOopsDoClosure;
class StringDedupEntry : public CHeapObj<mtGC> { class StringDedupEntry : public CHeapObj<mtGC> {
private: private:
StringDedupEntry* _next; StringDedupEntry* _next;
unsigned int _hash; uint64_t _hash;
bool _latin1;
typeArrayOop _obj; typeArrayOop _obj;
static const uint64_t java_hash_mask = ((uint64_t)1 << 32) - 1;
static const uint64_t java_latin1_mask = (uint64_t)1 << 63;
public: public:
StringDedupEntry() : StringDedupEntry() :
_next(NULL), _next(NULL),
_hash(0), _hash(0),
_latin1(false),
_obj(NULL) { _obj(NULL) {
} }
@ -63,22 +64,29 @@ public:
_next = next; _next = next;
} }
unsigned int hash() { unsigned int java_hash() {
return (unsigned int)(_hash & java_hash_mask);
}
bool java_hash_latin1() {
return (_hash & java_latin1_mask) != 0;
}
void set_java_hash(unsigned int hash, bool latin1) {
_hash = hash;
if (latin1) {
_hash |= java_latin1_mask;
}
}
uint64_t alt_hash() {
return _hash; return _hash;
} }
void set_hash(unsigned int hash) { void set_alt_hash(uint64_t hash) {
_hash = hash; _hash = hash;
} }
bool latin1() {
return _latin1;
}
void set_latin1(bool latin1) {
_latin1 = latin1;
}
typeArrayOop obj() { typeArrayOop obj() {
return _obj; return _obj;
} }
@ -130,8 +138,8 @@ private:
// The hash seed also dictates which hash function to use. A // The hash seed also dictates which hash function to use. A
// zero hash seed means we will use the Java compatible hash // zero hash seed means we will use the Java compatible hash
// function (which doesn't use a seed), and a non-zero hash // function (which doesn't use a seed), and a non-zero hash
// seed means we use the murmur3 hash function. // seed means we use the alternate and better hash function.
jint _hash_seed; uint64_t _hash_seed;
// Constants governing table resize/rehash/cache. // Constants governing table resize/rehash/cache.
static const size_t _min_size; static const size_t _min_size;
@ -153,7 +161,7 @@ private:
static StringDedupTable* _resized_table; static StringDedupTable* _resized_table;
static StringDedupTable* _rehashed_table; static StringDedupTable* _rehashed_table;
StringDedupTable(size_t size, jint hash_seed = 0); StringDedupTable(size_t size, uint64_t hash_seed = 0);
~StringDedupTable(); ~StringDedupTable();
// Returns the hash bucket at the given index. // Returns the hash bucket at the given index.
@ -162,12 +170,12 @@ private:
} }
// Returns the hash bucket index for the given hash code. // Returns the hash bucket index for the given hash code.
size_t hash_to_index(unsigned int hash) { size_t hash_to_index(uint64_t hash) {
return (size_t)hash & (_size - 1); return (size_t)hash & (_size - 1);
} }
// Adds a new table entry to the given hash bucket. // Adds a new table entry to the given hash bucket.
void add(typeArrayOop value, bool latin1, unsigned int hash, StringDedupEntry** list); void add(typeArrayOop value, bool latin1, uint64_t hash, StringDedupEntry** list);
// Removes the given table entry from the table. // Removes the given table entry from the table.
void remove(StringDedupEntry** pentry, uint worker_id); void remove(StringDedupEntry** pentry, uint worker_id);
@ -177,15 +185,15 @@ private:
// Returns an existing character array in the given hash bucket, or NULL // Returns an existing character array in the given hash bucket, or NULL
// if no matching character array exists. // if no matching character array exists.
typeArrayOop lookup(typeArrayOop value, bool latin1, unsigned int hash, typeArrayOop lookup(typeArrayOop value, bool latin1, uint64_t hash,
StringDedupEntry** list, uintx &count); StringDedupEntry** list, uintx &count);
// Returns an existing character array in the table, or inserts a new // Returns an existing character array in the table, or inserts a new
// table entry if no matching character array exists. // table entry if no matching character array exists.
typeArrayOop lookup_or_add_inner(typeArrayOop value, bool latin1, unsigned int hash); typeArrayOop lookup_or_add_inner(typeArrayOop value, bool latin1, uint64_t hash);
// Thread safe lookup or add of table entry // Thread safe lookup or add of table entry
static typeArrayOop lookup_or_add(typeArrayOop value, bool latin1, unsigned int hash) { static typeArrayOop lookup_or_add(typeArrayOop value, bool latin1, uint64_t hash) {
// Protect the table from concurrent access. Also note that this lock // Protect the table from concurrent access. Also note that this lock
// acts as a fence for _table, which could have been replaced by a new // acts as a fence for _table, which could have been replaced by a new
// instance if the table was resized or rehashed. // instance if the table was resized or rehashed.
@ -201,7 +209,8 @@ private:
// Computes the hash code for the given character array, using the // Computes the hash code for the given character array, using the
// currently active hash function and hash seed. // currently active hash function and hash seed.
static unsigned int hash_code(typeArrayOop value, bool latin1); static unsigned int java_hash_code(typeArrayOop value, bool latin1);
static uint64_t alt_hash_code(typeArrayOop value);
static uintx unlink_or_oops_do(StringDedupUnlinkOrOopsDoClosure* cl, static uintx unlink_or_oops_do(StringDedupUnlinkOrOopsDoClosure* cl,
size_t partition_begin, size_t partition_begin,

View File

@ -150,13 +150,13 @@ template <int N> static void get_header_version(char (&header_version) [N]) {
} else { } else {
// Get the hash value. Use a static seed because the hash needs to return the same // Get the hash value. Use a static seed because the hash needs to return the same
// value over multiple jvm invocations. // value over multiple jvm invocations.
unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len); uint64_t hash = AltHashing::halfsiphash_64(8191, (const int8_t*)vm_version, version_len);
// Truncate the ident, saving room for the 8 hex character hash value. // Truncate the ident, saving room for the 16 hex character hash value.
strncpy(header_version, vm_version, JVM_IDENT_MAX-9); strncpy(header_version, vm_version, JVM_IDENT_MAX-17);
// Append the hash code as eight hex digits. // Append the hash code as 16 hex digits.
sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash); sprintf(&header_version[JVM_IDENT_MAX-17], "%016" PRIx64, hash);
header_version[JVM_IDENT_MAX-1] = 0; // Null terminate. header_version[JVM_IDENT_MAX-1] = 0; // Null terminate.
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,86 +22,123 @@
*/ */
#include "precompiled.hpp" #include "precompiled.hpp"
#include "classfile/altHashing.hpp" #include "classfile/altHashing.hpp"
#include "utilities/debug.hpp"
#include "utilities/formatBuffer.hpp"
#include "unittest.hpp" #include "unittest.hpp"
// Internal test for alternate hashing. Translated from JDK version
// test/sun/misc/Hashing.java
static const jbyte ONE_BYTE[] = {(jbyte) 0x80};
static const jbyte TWO_BYTE[] = {(jbyte) 0x80, (jbyte) 0x81};
static const jchar ONE_CHAR[] = {(jchar) 0x8180};
static const jbyte THREE_BYTE[] = {(jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82};
static const jbyte FOUR_BYTE[] = {(jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82, (jbyte) 0x83};
static const jchar TWO_CHAR[] = {(jchar) 0x8180, (jchar) 0x8382};
static const jint ONE_INT[] = {(jint) 0x83828180};
static const jbyte SIX_BYTE[] = {(jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82, (jbyte) 0x83, (jbyte) 0x84, (jbyte) 0x85};
static const jchar THREE_CHAR[] = {(jchar) 0x8180, (jchar) 0x8382, (jchar) 0x8584};
static const jbyte EIGHT_BYTE[] = {
(jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82,
(jbyte) 0x83, (jbyte) 0x84, (jbyte) 0x85,
(jbyte) 0x86, (jbyte) 0x87
};
static const jchar FOUR_CHAR[] = {
(jchar) 0x8180, (jchar) 0x8382,
(jchar) 0x8584, (jchar) 0x8786
};
static const jint TWO_INT[] = {(jint) 0x83828180, (jint) 0x87868584};
static const juint MURMUR3_32_X86_CHECK_VALUE = 0xB0F57EE3;
class AltHashingTest : public ::testing::Test { class AltHashingTest : public ::testing::Test {
public: public:
static juint murmur3_32(const jint* data, int len) { static void testHalfsiphash_64_ByteArray() {
return AltHashing::murmur3_32(data, len); // printf("testHalfsiphash_64_CharArray\n");
const int factor = 4;
int8_t vector[256];
int8_t hashes[factor * 256];
for (int i = 0; i < 256; i++) {
vector[i] = (int8_t) i;
}
// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
for (int i = 0; i < 256; i++) {
uint64_t hash = AltHashing::halfsiphash_64(256 - i, vector, i);
hashes[i * factor] = (int8_t) hash;
hashes[i * factor + 1] = (int8_t)(hash >> 8);
hashes[i * factor + 2] = (int8_t)(hash >> 16);
hashes[i * factor + 3] = (int8_t)(hash >> 24);
}
// hash to get const result.
uint64_t final_hash = AltHashing::halfsiphash_64(0, hashes, factor*256);
// Value found using reference implementation for the hashes array.
// halfsiphash((const uint8_t*)hashes, factor*256, (const uint8_t *)&k,
// (uint8_t*)&reference, 8);
static const uint64_t HALFSIPHASH_64_BYTE_CHECK_VALUE = 0x15a7911e30917ee8;
ASSERT_EQ(HALFSIPHASH_64_BYTE_CHECK_VALUE, final_hash) <<
err_msg(
"Calculated hash result not as expected. Expected " UINT64_FORMAT " got " UINT64_FORMAT,
HALFSIPHASH_64_BYTE_CHECK_VALUE,
final_hash);
}
static void testHalfsiphash_64_CharArray() {
// printf("testHalfsiphash_64_CharArray\n");
const int factor = 2;
uint16_t vector[256];
uint16_t hashes[factor * 256];
for (int i = 0; i < 256; i++) {
vector[i] = (uint16_t) i;
}
// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
for (int i = 0; i < 256; i++) {
uint64_t hash = AltHashing::halfsiphash_64(256 - i, vector, i);
hashes[i * factor] = (uint16_t) hash;
hashes[i * factor + 1] = (uint16_t)(hash >> 16);
}
// hash to get const result.
uint64_t final_hash = AltHashing::halfsiphash_64(0, hashes, factor*256);
// Value found using reference implementation for the hashes array.
// halfsiphash((const uint8_t*)hashes, 2*factor*256, (const uint8_t *)&k,
// (uint8_t*)&reference, 8);
static const uint64_t HALFSIPHASH_64_CHAR_CHECK_VALUE = 0xf392d8a6a9e24103;
ASSERT_EQ(HALFSIPHASH_64_CHAR_CHECK_VALUE, final_hash) <<
err_msg(
"Calculated hash result not as expected. Expected " UINT64_FORMAT " got " UINT64_FORMAT,
HALFSIPHASH_64_CHAR_CHECK_VALUE,
final_hash);
}
// Test against sample hashes published with the reference implementation:
// https://github.com/veorq/SipHash
static void testHalfsiphash_64_FromReference() {
// printf("testHalfsiphash_64_FromReference\n");
const uint64_t seed = 0x0706050403020100;
const uint64_t results[16] = {
0xc83cb8b9591f8d21, 0xa12ee55b178ae7d5,
0x8c85e4bc20e8feed, 0x99c7f5ae9f1fc77b,
0xb5f37b5fd2aa3673, 0xdba7ee6f0a2bf51b,
0xf1a63fae45107470, 0xb516001efb5f922d,
0x6c6211d8469d7028, 0xdc7642ec407ad686,
0x4caec8671cc8385b, 0x5ab1dc27adf3301e,
0x3e3ea94bc0a8eaa9, 0xe150f598795a4402,
0x1d5ff142f992a4a1, 0x60e426bf902876d6
};
uint32_t vector[16];
for (int i = 0; i < 16; i++)
vector[i] = 0x03020100 + i * 0x04040404;
for (int i = 0; i < 16; i++) {
uint64_t hash = AltHashing::halfsiphash_64(seed, vector, i);
ASSERT_EQ(results[i], hash) <<
err_msg(
"Calculated hash result not as expected. Round %d: "
"Expected " UINT64_FORMAT_X " got " UINT64_FORMAT_X "\n",
i,
results[i],
hash);
}
} }
}; };
TEST_F(AltHashingTest, murmur3_32_byte_array_test) { TEST_F(AltHashingTest, halfsiphash_test_ByteArray) {
jbyte vector[256]; AltHashingTest::testHalfsiphash_64_ByteArray();
jbyte hashes[4 * 256];
for (int i = 0; i < 256; i++) {
vector[i] = (jbyte) i;
}
// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
for (int i = 0; i < 256; i++) {
juint hash = AltHashing::murmur3_32(256 - i, vector, i);
hashes[i * 4] = (jbyte) hash;
hashes[i * 4 + 1] = (jbyte) (hash >> 8);
hashes[i * 4 + 2] = (jbyte) (hash >> 16);
hashes[i * 4 + 3] = (jbyte) (hash >> 24);
}
// hash to get const result.
juint final_hash = AltHashing::murmur3_32(0, hashes, 4 * 256);
ASSERT_EQ(MURMUR3_32_X86_CHECK_VALUE, final_hash)
<< "Calculated hash result not as expected.";
} }
TEST_F(AltHashingTest, halfsiphash_test_CharArray) {
TEST_F(AltHashingTest, equivalent_hashes_test) { AltHashingTest::testHalfsiphash_64_CharArray();
juint jbytes, jchars, ints; }
TEST_F(AltHashingTest, halfsiphash_test_FromReference) {
jbytes = AltHashing::murmur3_32(0, TWO_BYTE, 2); AltHashingTest::testHalfsiphash_64_FromReference();
jchars = AltHashing::murmur3_32(0, ONE_CHAR, 1);
ASSERT_EQ(jbytes, jchars) << "Hashes did not match.";
jbytes = AltHashing::murmur3_32(0, FOUR_BYTE, 4);
jchars = AltHashing::murmur3_32(0, TWO_CHAR, 2);
ints = AltHashingTest::murmur3_32(ONE_INT, 1);
ASSERT_EQ(jbytes, jchars) << "Hashes did not match.";
ASSERT_EQ(jbytes, ints) << "Hashes did not match.";
jbytes = AltHashing::murmur3_32(0, SIX_BYTE, 6);
jchars = AltHashing::murmur3_32(0, THREE_CHAR, 3);
ASSERT_EQ(jbytes, jchars) << "Hashes did not match.";
jbytes = AltHashing::murmur3_32(0, EIGHT_BYTE, 8);
jchars = AltHashing::murmur3_32(0, FOUR_CHAR, 4);
ints = AltHashingTest::murmur3_32(TWO_INT, 2);
ASSERT_EQ(jbytes, jchars) << "Hashes did not match.";
ASSERT_EQ(jbytes, ints) << "Hashes did not match.";
} }