8256274: C2: Optimize copying of the shared type dictionary

Reviewed-by: neliasso, kvn
This commit is contained in:
Claes Redestad 2020-11-17 07:15:04 +00:00
parent 537b40e013
commit 1228517261
3 changed files with 112 additions and 177 deletions
src/hotspot/share

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -47,7 +47,7 @@ static const short xsum[MAXID] = {3,8,17,34,67,132,261,264,269,278,295,328,393,5
class bucket : public ResourceObj {
public:
uint _cnt, _max; // Size of bucket
void **_keyvals; // Array of keys and values
void** _keyvals; // Array of keys and values
};
//------------------------------Dict-----------------------------------------
@ -64,42 +64,37 @@ Dict::Dict(CmpKey initcmp, Hash inithash) : _arena(Thread::current()->resource_a
_size = 16; // Size is a power of 2
_cnt = 0; // Dictionary is empty
_bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
memset((void*)_bin,0,sizeof(bucket)*_size);
_bin = (bucket*)_arena->Amalloc_4(sizeof(bucket) * _size);
memset((void*)_bin, 0, sizeof(bucket) * _size);
}
Dict::Dict(CmpKey initcmp, Hash inithash, Arena *arena, int size)
Dict::Dict(CmpKey initcmp, Hash inithash, Arena* arena, int size)
: _arena(arena), _hash(inithash), _cmp(initcmp) {
// Size is a power of 2
_size = MAX2(16, round_up_power_of_2(size));
_cnt = 0; // Dictionary is empty
_bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
memset((void*)_bin,0,sizeof(bucket)*_size);
_bin = (bucket*)_arena->Amalloc_4(sizeof(bucket) * _size);
memset((void*)_bin, 0, sizeof(bucket) * _size);
}
// Deep copy into arena of choice
Dict::Dict(const Dict &d, Arena* arena)
: _arena(arena), _size(d._size), _cnt(d._cnt), _hash(d._hash), _cmp(d._cmp) {
_bin = (bucket*)_arena->Amalloc_4(sizeof(bucket) * _size);
memcpy((void*)_bin, (void*)d._bin, sizeof(bucket) * _size);
for (uint i = 0; i < _size; i++) {
if (!_bin[i]._keyvals) {
continue;
}
_bin[i]._keyvals = (void**)_arena->Amalloc_4(sizeof(void*) * _bin[i]._max * 2);
memcpy(_bin[i]._keyvals, d._bin[i]._keyvals, _bin[i]._cnt * 2 * sizeof(void*));
}
}
//------------------------------~Dict------------------------------------------
// Delete an existing dictionary.
Dict::~Dict() {
/*
tty->print("~Dict %d/%d: ",_cnt,_size);
for( uint i=0; i < _size; i++) // For complete new table do
tty->print("%d ",_bin[i]._cnt);
tty->print("\n");*/
/*for( uint i=0; i<_size; i++ ) {
FREE_FAST( _bin[i]._keyvals );
} */
}
//------------------------------Clear----------------------------------------
// Zap to empty; ready for re-use
void Dict::Clear() {
_cnt = 0; // Empty contents
for( uint i=0; i<_size; i++ )
_bin[i]._cnt = 0; // Empty buckets, but leave allocated
// Leave _size & _bin alone, under the assumption that dictionary will
// grow to this size again.
}
Dict::~Dict() { }
//------------------------------doubhash---------------------------------------
// Double hash table size. If can't do so, just suffer. If can, then run
@ -107,32 +102,32 @@ void Dict::Clear() {
// table doubled, exactly 1 new bit is exposed in the mask - so everything
// in the old table ends up on 1 of two lists in the new table; a hi and a
// lo list depending on the value of the bit.
void Dict::doubhash(void) {
void Dict::doubhash() {
uint oldsize = _size;
_size <<= 1; // Double in size
_bin = (bucket*)_arena->Arealloc(_bin, sizeof(bucket) * oldsize, sizeof(bucket) * _size);
memset((void*)(&_bin[oldsize]), 0, oldsize * sizeof(bucket));
// Rehash things to spread into new table
for (uint i = 0; i < oldsize; i++) { // For complete OLD table do
bucket *b = &_bin[i]; // Handy shortcut for _bin[i]
bucket* b = &_bin[i]; // Handy shortcut for _bin[i]
if (!b->_keyvals) continue; // Skip empties fast
bucket *nb = &_bin[i+oldsize]; // New bucket shortcut
bucket* nb = &_bin[i+oldsize]; // New bucket shortcut
uint j = b->_max; // Trim new bucket to nearest power of 2
while (j > b->_cnt) { j >>= 1; } // above old bucket _cnt
if (!j) { j = 1; } // Handle zero-sized buckets
nb->_max = j << 1;
// Allocate worst case space for key-value pairs
nb->_keyvals = (void**)_arena->Amalloc_4(sizeof(void *) * nb->_max * 2);
nb->_keyvals = (void**)_arena->Amalloc_4(sizeof(void* ) * nb->_max * 2);
uint nbcnt = 0;
for (j = 0; j < b->_cnt; ) { // Rehash all keys in this bucket
void *key = b->_keyvals[j + j];
for (j = 0; j < b->_cnt;) { // Rehash all keys in this bucket
void* key = b->_keyvals[j + j];
if ((_hash(key) & (_size-1)) != i) { // Moving to hi bucket?
nb->_keyvals[nbcnt + nbcnt] = key;
nb->_keyvals[nbcnt + nbcnt + 1] = b->_keyvals[j + j + 1];
nb->_cnt = nbcnt = nbcnt + 1;
b->_cnt--; // Remove key/value from lo bucket
b->_cnt--; // Remove key/value from lo bucket
b->_keyvals[j + j] = b->_keyvals[b->_cnt + b->_cnt];
b->_keyvals[j + j + 1] = b->_keyvals[b->_cnt + b->_cnt + 1];
// Don't increment j, hash compacted element also.
@ -143,70 +138,35 @@ void Dict::doubhash(void) {
} // End of for all buckets
}
//------------------------------Dict-----------------------------------------
// Deep copy a dictionary.
Dict::Dict( const Dict &d ) : ResourceObj(d), _arena(d._arena), _size(d._size), _cnt(d._cnt), _hash(d._hash), _cmp(d._cmp) {
_bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
memcpy( (void*)_bin, (void*)d._bin, sizeof(bucket)*_size );
for( uint i=0; i<_size; i++ ) {
if( !_bin[i]._keyvals ) continue;
_bin[i]._keyvals=(void**)_arena->Amalloc_4( sizeof(void *)*_bin[i]._max*2);
memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*));
}
}
//------------------------------Dict-----------------------------------------
// Deep copy a dictionary.
Dict &Dict::operator =( const Dict &d ) {
if( _size < d._size ) { // If must have more buckets
_arena = d._arena;
_bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size );
memset( (void*)(&_bin[_size]), 0, (d._size-_size)*sizeof(bucket) );
_size = d._size;
}
uint i;
for( i=0; i<_size; i++ ) // All buckets are empty
_bin[i]._cnt = 0; // But leave bucket allocations alone
_cnt = d._cnt;
*(Hash*)(&_hash) = d._hash;
*(CmpKey*)(&_cmp) = d._cmp;
for( i=0; i<_size; i++ ) {
bucket *b = &d._bin[i]; // Shortcut to source bucket
for( uint j=0; j<b->_cnt; j++ )
Insert( b->_keyvals[j+j], b->_keyvals[j+j+1] );
}
return *this;
}
//------------------------------Insert----------------------------------------
// Insert or replace a key/value pair in the given dictionary. If the
// dictionary is too full, it's size is doubled. The prior value being
// replaced is returned (NULL if this is a 1st insertion of that key). If
// an old value is found, it's swapped with the prior key-value pair on the
// list. This moves a commonly searched-for value towards the list head.
void *Dict::Insert(void *key, void *val, bool replace) {
uint hash = _hash( key ); // Get hash key
uint i = hash & (_size-1); // Get hash key, corrected for size
bucket *b = &_bin[i]; // Handy shortcut
for( uint j=0; j<b->_cnt; j++ ) {
if( !_cmp(key,b->_keyvals[j+j]) ) {
void*Dict::Insert(void* key, void* val, bool replace) {
uint hash = _hash(key); // Get hash key
uint i = hash & (_size - 1); // Get hash key, corrected for size
bucket* b = &_bin[i];
for (uint j = 0; j < b->_cnt; j++) {
if (!_cmp(key, b->_keyvals[j + j])) {
if (!replace) {
return b->_keyvals[j+j+1];
return b->_keyvals[j + j + 1];
} else {
void *prior = b->_keyvals[j+j+1];
b->_keyvals[j+j ] = key; // Insert current key-value
b->_keyvals[j+j+1] = val;
return prior; // Return prior
void* prior = b->_keyvals[j + j + 1];
b->_keyvals[j + j ] = key;
b->_keyvals[j + j + 1] = val;
return prior;
}
}
}
if( ++_cnt > _size ) { // Hash table is full
if (++_cnt > _size) { // Hash table is full
doubhash(); // Grow whole table if too full
i = hash & (_size-1); // Rehash
b = &_bin[i]; // Handy shortcut
i = hash & (_size - 1); // Rehash
b = &_bin[i];
}
if( b->_cnt == b->_max ) { // Must grow bucket?
if( !b->_keyvals ) {
if (b->_cnt == b->_max) { // Must grow bucket?
if (!b->_keyvals) {
b->_max = 2; // Initial bucket size
b->_keyvals = (void**)_arena->Amalloc_4(sizeof(void*) * b->_max * 2);
} else {
@ -214,56 +174,42 @@ void *Dict::Insert(void *key, void *val, bool replace) {
b->_max <<= 1; // Double bucket
}
}
b->_keyvals[b->_cnt+b->_cnt ] = key;
b->_keyvals[b->_cnt+b->_cnt+1] = val;
b->_keyvals[b->_cnt + b->_cnt ] = key;
b->_keyvals[b->_cnt + b->_cnt + 1] = val;
b->_cnt++;
return NULL; // Nothing found prior
}
//------------------------------Delete---------------------------------------
// Find & remove a value from dictionary. Return old value.
void *Dict::Delete(void *key) {
uint i = _hash( key ) & (_size-1); // Get hash key, corrected for size
bucket *b = &_bin[i]; // Handy shortcut
for( uint j=0; j<b->_cnt; j++ )
if( !_cmp(key,b->_keyvals[j+j]) ) {
void *prior = b->_keyvals[j+j+1];
void* Dict::Delete(void* key) {
uint i = _hash(key) & (_size - 1); // Get hash key, corrected for size
bucket* b = &_bin[i]; // Handy shortcut
for (uint j = 0; j < b->_cnt; j++) {
if (!_cmp(key, b->_keyvals[j + j])) {
void* prior = b->_keyvals[j + j + 1];
b->_cnt--; // Remove key/value from lo bucket
b->_keyvals[j+j ] = b->_keyvals[b->_cnt+b->_cnt ];
b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
b->_keyvals[j+j ] = b->_keyvals[b->_cnt + b->_cnt ];
b->_keyvals[j+j+1] = b->_keyvals[b->_cnt + b->_cnt + 1];
_cnt--; // One less thing in table
return prior;
}
}
return NULL;
}
//------------------------------FindDict-------------------------------------
// Find a key-value pair in the given dictionary. If not found, return NULL.
// If found, move key-value pair towards head of list.
void *Dict::operator [](const void *key) const {
uint i = _hash( key ) & (_size-1); // Get hash key, corrected for size
bucket *b = &_bin[i]; // Handy shortcut
for( uint j=0; j<b->_cnt; j++ )
if( !_cmp(key,b->_keyvals[j+j]) )
return b->_keyvals[j+j+1];
return NULL;
}
//------------------------------CmpDict--------------------------------------
// CmpDict compares two dictionaries; they must have the same keys (their
// keys must match using CmpKey) and they must have the same values (pointer
// comparison). If so 1 is returned, if not 0 is returned.
int32_t Dict::operator ==(const Dict &d2) const {
if( _cnt != d2._cnt ) return 0;
if( _hash != d2._hash ) return 0;
if( _cmp != d2._cmp ) return 0;
for( uint i=0; i < _size; i++) { // For complete hash table do
bucket *b = &_bin[i]; // Handy shortcut
if( b->_cnt != d2._bin[i]._cnt ) return 0;
if( memcmp(b->_keyvals, d2._bin[i]._keyvals, b->_cnt*2*sizeof(void*) ) )
return 0; // Key-value pairs must match
void* Dict::operator [](const void* key) const {
uint i = _hash(key) & (_size - 1); // Get hash key, corrected for size
bucket* b = &_bin[i]; // Handy shortcut
for (uint j = 0; j < b->_cnt; j++) {
if (!_cmp(key, b->_keyvals[j + j])) {
return b->_keyvals[j + j + 1];
}
}
return 1; // All match, is OK
return NULL;
}
//------------------------------print------------------------------------------
@ -271,7 +217,7 @@ int32_t Dict::operator ==(const Dict &d2) const {
void Dict::print() {
DictI i(this); // Moved definition in iterator here because of g++.
tty->print("Dict@" INTPTR_FORMAT "[%d] = {", p2i(this), _cnt);
for( ; i.test(); ++i ) {
for (; i.test(); ++i) {
tty->print("(" INTPTR_FORMAT "," INTPTR_FORMAT "),", p2i(i._key), p2i(i._value));
}
tty->print_cr("}");
@ -288,12 +234,12 @@ void Dict::print() {
// be in the range 0-127 (I double & add 1 to force oddness). Keys are
// limited to MAXID characters in length. Experimental evidence on 150K of
// C text shows excellent spreading of values for any size hash table.
int hashstr(const void *t) {
int hashstr(const void* t) {
char c, k = 0;
int32_t sum = 0;
const char *s = (const char *)t;
const char* s = (const char*)t;
while( ((c = *s++) != '\0') && (k < MAXID-1) ) { // Get characters till null or MAXID-1
while (((c = *s++) != '\0') && (k < MAXID-1)) { // Get characters till null or MAXID-1
c = (c << 1) + 1; // Characters are always odd!
sum += c + (c << shft[k++]); // Universal hash function
}
@ -303,33 +249,37 @@ int hashstr(const void *t) {
//------------------------------hashptr--------------------------------------
// Slimey cheap hash function; no guaranteed performance. Better than the
// default for pointers, especially on MS-DOS machines.
int hashptr(const void *key) {
int hashptr(const void* key) {
return ((intptr_t)key >> 2);
}
// Slimey cheap hash function; no guaranteed performance.
int hashkey(const void *key) {
int hashkey(const void* key) {
return (intptr_t)key;
}
//------------------------------Key Comparator Functions---------------------
int32_t cmpstr(const void *k1, const void *k2) {
return strcmp((const char *)k1,(const char *)k2);
int32_t cmpstr(const void* k1, const void* k2) {
return strcmp((const char*)k1, (const char*)k2);
}
// Cheap key comparator.
int32_t cmpkey(const void *key1, const void *key2) {
if (key1 == key2) return 0;
int32_t cmpkey(const void* key1, const void* key2) {
if (key1 == key2) {
return 0;
}
intptr_t delta = (intptr_t)key1 - (intptr_t)key2;
if (delta > 0) return 1;
if (delta > 0) {
return 1;
}
return -1;
}
//=============================================================================
//------------------------------reset------------------------------------------
// Create an iterator and initialize the first variables.
void DictI::reset( const Dict *dict ) {
_d = dict; // The dictionary
void DictI::reset(const Dict* dict) {
_d = dict;
_i = (uint)-1; // Before the first bin
_j = 0; // Nothing left in the current bin
++(*this); // Step to first real value
@ -339,15 +289,17 @@ void DictI::reset( const Dict *dict ) {
// Find the next key-value pair in the dictionary, or return a NULL key and
// value.
void DictI::operator ++(void) {
if( _j-- ) { // Still working in current bin?
_key = _d->_bin[_i]._keyvals[_j+_j];
_value = _d->_bin[_i]._keyvals[_j+_j+1];
if (_j--) { // Still working in current bin?
_key = _d->_bin[_i]._keyvals[_j + _j];
_value = _d->_bin[_i]._keyvals[_j + _j + 1];
return;
}
while( ++_i < _d->_size ) { // Else scan for non-zero bucket
while (++_i < _d->_size) { // Else scan for non-zero bucket
_j = _d->_bin[_i]._cnt;
if( !_j ) continue;
if (!_j) {
continue;
}
_j--;
_key = _d->_bin[_i]._keyvals[_j+_j];
_value = _d->_bin[_i]._keyvals[_j+_j+1];

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -39,67 +39,56 @@ class Dict;
// key comparison routine determines if two keys are equal or not. A hash
// function can be provided; if it's not provided the key itself is used
// instead. A nice string hash function is included.
typedef int32_t (*CmpKey)(const void *key1, const void *key2);
typedef int (*Hash)(const void *key);
typedef void (*FuncDict)(const void *key, const void *val, Dict *d);
typedef int32_t (*CmpKey)(const void* key1, const void* key2);
typedef int (*Hash)(const void* key);
class Dict : public ResourceObj { // Dictionary structure
private:
class Arena *_arena; // Where to draw storage from
class bucket *_bin; // Hash table is array of buckets
class Arena* _arena; // Where to draw storage from
class bucket* _bin; // Hash table is array of buckets
uint _size; // Size (# of slots) in hash table
uint32_t _cnt; // Number of key-value pairs in hash table
const Hash _hash; // Hashing function
const CmpKey _cmp; // Key comparison function
void doubhash( void ); // Double hash table size
void doubhash(); // Double hash table size
public:
friend class DictI; // Friendly iterator function
// cmp is a key comparision routine. hash is a routine to hash a key.
Dict( CmpKey cmp, Hash hash );
Dict( CmpKey cmp, Hash hash, Arena *arena, int size=16 );
Dict(CmpKey cmp, Hash hash);
Dict(CmpKey cmp, Hash hash, Arena* arena, int size = 16);
Dict(const Dict &base, Arena* arena); // Deep-copy
~Dict();
Dict( const Dict & ); // Deep-copy guts
Dict &operator =( const Dict & );
// Zap to empty; ready for re-use
void Clear();
// Return # of key-value pairs in dict
uint32_t Size(void) const { return _cnt; }
// Insert inserts the given key-value pair into the dictionary. The prior
// value of the key is returned; NULL if the key was not previously defined.
void *Insert(void *key, void *val, bool replace = true); // A new key-value
void *Delete(void *key); // Delete & return old
void* Insert(void* key, void* val, bool replace = true); // A new key-value
void* Delete(void* key); // Delete & return old
// Find finds the value of a given key; or NULL if not found.
// The dictionary is NOT changed.
void *operator [](const void *key) const; // Do a lookup
// == compares two dictionaries; they must have the same keys (their keys
// must match using CmpKey) and they must have the same values (pointer
// comparison). If so 1 is returned, if not 0 is returned.
int32_t operator ==(const Dict &d) const; // Compare dictionaries for equal
void* operator [](const void* key) const; // Do a lookup
// Print out the dictionary contents as key-value pairs
void print();
};
// Hashing functions
int hashstr(const void *s); // Nice string hash
int hashstr(const void* s); // Nice string hash
// Slimey cheap hash function; no guaranteed performance. Better than the
// default for pointers, especially on MS-DOS machines.
int hashptr(const void *key);
int hashptr(const void* key);
// Slimey cheap hash function; no guaranteed performance.
int hashkey(const void *key);
int hashkey(const void* key);
// Key comparators
int32_t cmpstr(const void *k1, const void *k2);
int32_t cmpstr(const void* k1, const void* k2);
// Slimey cheap key comparator.
int32_t cmpkey(const void *key1, const void *key2);
int32_t cmpkey(const void* key1, const void* key2);
//------------------------------Iteration--------------------------------------
// The class of dictionary iterators. Fails in the presences of modifications
@ -107,15 +96,16 @@ int32_t cmpkey(const void *key1, const void *key2);
// Usage: for( DictI i(dict); i.test(); ++i ) { body = i.key; body = i.value;}
class DictI {
private:
const Dict *_d; // Dictionary being iterated over
const Dict* _d; // Dictionary being iterated over
uint _i; // Counter over the bins
uint _j; // Counter inside each bin
public:
const void *_key, *_value; // Easy access to the key-value pair
DictI( const Dict *d ) {reset(d);}; // Create a new iterator
void reset( const Dict *dict ); // Reset existing iterator
const void* _key;
const void* _value;
DictI(const Dict* d) { reset(d); }; // Create a new iterator
void reset(const Dict* dict); // Reset existing iterator
void operator ++(void); // Increment iterator
int test(void) { return _i<_d->_size;} // Test for end of iteration
int test(void) { return _i < _d->_size; } // Test for end of iteration
};
#endif // SHARE_LIBADT_DICT_HPP

@ -701,15 +701,8 @@ void Type::Initialize(Compile* current) {
Arena* type_arena = current->type_arena();
// Create the hash-cons'ing dictionary with top-level storage allocation
Dict *tdic = new (type_arena) Dict( (CmpKey)Type::cmp,(Hash)Type::uhash, type_arena, 128 );
Dict *tdic = new (type_arena) Dict(*_shared_type_dict, type_arena);
current->set_type_dict(tdic);
// Transfer the shared types.
DictI i(_shared_type_dict);
for( ; i.test(); ++i ) {
Type* t = (Type*)i._value;
tdic->Insert(t,t); // New Type, insert into Type table
}
}
//------------------------------hashcons---------------------------------------