Skip to content
Permalink
Browse files

8238863: Refactor out static initialization from Dict constructors

Reviewed-by: neliasso, dlong
  • Loading branch information
cl4es committed Feb 14, 2020
1 parent 945e422 commit b4b8ead78bab585dc4f9a7f071ce2e6a64d06cc8
Showing with 11 additions and 27 deletions.
  1. +11 −27 src/hotspot/share/libadt/dict.cpp
@@ -35,9 +35,13 @@
//------------------------------data-----------------------------------------
// String hash tables
#define MAXID 20
static uint8_t initflag = 0; // True after 1st initialization
static const char shft[MAXID] = {1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6};
static short xsum[MAXID];
// Precomputed table of null character hashes
// xsum[0] = (1 << shft[0]) + 1;
// for(int i = 1; i < MAXID; i++) {
// xsum[i] = (1 << shft[i]) + 1 + xsum[i - 1];
// }
static const short xsum[MAXID] = {3,8,17,34,67,132,261,264,269,278,295,328,393,522,525,530,539,556,589,654};

//------------------------------bucket---------------------------------------
class bucket : public ResourceObj {
@@ -57,16 +61,6 @@ class bucket : public ResourceObj {
// doubling in size costs no more than a constant factor in speed.
Dict::Dict(CmpKey initcmp, Hash inithash) : _arena(Thread::current()->resource_area()),
_hash(inithash), _cmp(initcmp) {
int i;

// Precompute table of null character hashes
if( !initflag ) { // Not initializated yet?
xsum[0] = (1<<shft[0])+1; // Initialize
for(i=1; i<MAXID; i++) {
xsum[i] = (1<<shft[i])+1+xsum[i-1];
}
initflag = 1; // Never again
}

_size = 16; // Size is a power of 2
_cnt = 0; // Dictionary is empty
@@ -76,19 +70,9 @@ Dict::Dict(CmpKey initcmp, Hash inithash) : _arena(Thread::current()->resource_a

Dict::Dict(CmpKey initcmp, Hash inithash, Arena *arena, int size)
: _arena(arena), _hash(inithash), _cmp(initcmp) {
int i;

// Precompute table of null character hashes
if( !initflag ) { // Not initializated yet?
xsum[0] = (1<<shft[0])+1; // Initialize
for(i=1; i<MAXID; i++) {
xsum[i] = (1<<shft[i])+1+xsum[i-1];
}
initflag = 1; // Never again
}
// Size is a power of 2
_size = MAX2(16, round_up_power_of_2(size));

i = MAX2(16, round_up_power_of_2(size));
_size = i; // 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);
@@ -310,10 +294,10 @@ int hashstr(const void *t) {
const char *s = (const char *)t;

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
c = (c << 1) + 1; // Characters are always odd!
sum += c + (c << shft[k++]); // Universal hash function
}
return (int)((sum+xsum[k]) >> 1); // Hash key, un-modulo'd table size
return (int)((sum + xsum[k]) >> 1); // Hash key, un-modulo'd table size
}

//------------------------------hashptr--------------------------------------

0 comments on commit b4b8ead

Please sign in to comment.