< prev index next >

src/share/vm/gc/g1/g1StringDedupTable.hpp

Print this page




  23  */
  24 
  25 #ifndef SHARE_VM_GC_G1_G1STRINGDEDUPTABLE_HPP
  26 #define SHARE_VM_GC_G1_G1STRINGDEDUPTABLE_HPP
  27 
  28 #include "gc/g1/g1StringDedupStat.hpp"
  29 #include "runtime/mutexLocker.hpp"
  30 
  31 class G1StringDedupEntryCache;
  32 class G1StringDedupUnlinkOrOopsDoClosure;
  33 
  34 //
  35 // Table entry in the deduplication hashtable. Points weakly to the
  36 // character array. Can be chained in a linked list in case of hash
  37 // collisions or when placed in a freelist in the entry cache.
  38 //
  39 class G1StringDedupEntry : public CHeapObj<mtGC> {
  40 private:
  41   G1StringDedupEntry* _next;
  42   unsigned int      _hash;

  43   typeArrayOop      _obj;
  44 
  45 public:
  46   G1StringDedupEntry() :
  47     _next(NULL),
  48     _hash(0),

  49     _obj(NULL) {
  50   }
  51 
  52   G1StringDedupEntry* next() {
  53     return _next;
  54   }
  55 
  56   G1StringDedupEntry** next_addr() {
  57     return &_next;
  58   }
  59 
  60   void set_next(G1StringDedupEntry* next) {
  61     _next = next;
  62   }
  63 
  64   unsigned int hash() {
  65     return _hash;
  66   }
  67 
  68   void set_hash(unsigned int hash) {
  69     _hash = hash;
  70   }
  71 








  72   typeArrayOop obj() {
  73     return _obj;
  74   }
  75 
  76   typeArrayOop* obj_addr() {
  77     return &_obj;
  78   }
  79 
  80   void set_obj(typeArrayOop obj) {
  81     _obj = obj;
  82   }
  83 };
  84 
  85 //
  86 // The deduplication hashtable keeps track of all unique character arrays used
  87 // by String objects. Each table entry weakly points to an character array, allowing
  88 // otherwise unreachable character arrays to be declared dead and pruned from the
  89 // table.
  90 //
  91 // The table is dynamically resized to accommodate the current number of table entries.


 135   // Table statistics, only used for logging.
 136   static uintx                    _entries_added;
 137   static uintx                    _entries_removed;
 138   static uintx                    _resize_count;
 139   static uintx                    _rehash_count;
 140 
 141   G1StringDedupTable(size_t size, jint hash_seed = 0);
 142   ~G1StringDedupTable();
 143 
 144   // Returns the hash bucket at the given index.
 145   G1StringDedupEntry** bucket(size_t index) {
 146     return _buckets + index;
 147   }
 148 
 149   // Returns the hash bucket index for the given hash code.
 150   size_t hash_to_index(unsigned int hash) {
 151     return (size_t)hash & (_size - 1);
 152   }
 153 
 154   // Adds a new table entry to the given hash bucket.
 155   void add(typeArrayOop value, unsigned int hash, G1StringDedupEntry** list);
 156 
 157   // Removes the given table entry from the table.
 158   void remove(G1StringDedupEntry** pentry, uint worker_id);
 159 
 160   // Transfers a table entry from the current table to the destination table.
 161   void transfer(G1StringDedupEntry** pentry, G1StringDedupTable* dest);
 162 
 163   // Returns an existing character array in the given hash bucket, or NULL
 164   // if no matching character array exists.
 165   typeArrayOop lookup(typeArrayOop value, unsigned int hash,
 166                       G1StringDedupEntry** list, uintx &count);
 167 
 168   // Returns an existing character array in the table, or inserts a new
 169   // table entry if no matching character array exists.
 170   typeArrayOop lookup_or_add_inner(typeArrayOop value, unsigned int hash);
 171 
 172   // Thread safe lookup or add of table entry
 173   static typeArrayOop lookup_or_add(typeArrayOop value, unsigned int hash) {
 174     // Protect the table from concurrent access. Also note that this lock
 175     // acts as a fence for _table, which could have been replaced by a new
 176     // instance if the table was resized or rehashed.
 177     MutexLockerEx ml(StringDedupTable_lock, Mutex::_no_safepoint_check_flag);
 178     return _table->lookup_or_add_inner(value, hash);
 179   }
 180 
 181   // Returns true if the hashtable is currently using a Java compatible
 182   // hash function.
 183   static bool use_java_hash() {
 184     return _table->_hash_seed == 0;
 185   }
 186 
 187   static bool equals(typeArrayOop value1, typeArrayOop value2);
 188 
 189   // Computes the hash code for the given character array, using the
 190   // currently active hash function and hash seed.
 191   static unsigned int hash_code(typeArrayOop value);
 192 
 193   static uintx unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl,
 194                                  size_t partition_begin,
 195                                  size_t partition_end,
 196                                  uint worker_id);
 197 
 198 public:
 199   static void create();
 200 
 201   // Deduplicates the given String object, or adds its backing
 202   // character array to the deduplication hashtable.
 203   static void deduplicate(oop java_string, G1StringDedupStat& stat);
 204 
 205   // If a table resize is needed, returns a newly allocated empty
 206   // hashtable of the proper size.
 207   static G1StringDedupTable* prepare_resize();
 208 
 209   // Installs a newly resized table as the currently active table
 210   // and deletes the previously active table.
 211   static void finish_resize(G1StringDedupTable* resized_table);


  23  */
  24 
  25 #ifndef SHARE_VM_GC_G1_G1STRINGDEDUPTABLE_HPP
  26 #define SHARE_VM_GC_G1_G1STRINGDEDUPTABLE_HPP
  27 
  28 #include "gc/g1/g1StringDedupStat.hpp"
  29 #include "runtime/mutexLocker.hpp"
  30 
  31 class G1StringDedupEntryCache;
  32 class G1StringDedupUnlinkOrOopsDoClosure;
  33 
  34 //
  35 // Table entry in the deduplication hashtable. Points weakly to the
  36 // character array. Can be chained in a linked list in case of hash
  37 // collisions or when placed in a freelist in the entry cache.
  38 //
  39 class G1StringDedupEntry : public CHeapObj<mtGC> {
  40 private:
  41   G1StringDedupEntry* _next;
  42   unsigned int      _hash;
  43   bool              _latin1;
  44   typeArrayOop      _obj;
  45 
  46 public:
  47   G1StringDedupEntry() :
  48     _next(NULL),
  49     _hash(0),
  50     _latin1(false),
  51     _obj(NULL) {
  52   }
  53 
  54   G1StringDedupEntry* next() {
  55     return _next;
  56   }
  57 
  58   G1StringDedupEntry** next_addr() {
  59     return &_next;
  60   }
  61 
  62   void set_next(G1StringDedupEntry* next) {
  63     _next = next;
  64   }
  65 
  66   unsigned int hash() {
  67     return _hash;
  68   }
  69 
  70   void set_hash(unsigned int hash) {
  71     _hash = hash;
  72   }
  73 
  74   bool latin1() {
  75     return _latin1;
  76   }
  77 
  78   void set_latin1(bool latin1) {
  79     _latin1 = latin1;
  80   }
  81 
  82   typeArrayOop obj() {
  83     return _obj;
  84   }
  85 
  86   typeArrayOop* obj_addr() {
  87     return &_obj;
  88   }
  89 
  90   void set_obj(typeArrayOop obj) {
  91     _obj = obj;
  92   }
  93 };
  94 
  95 //
  96 // The deduplication hashtable keeps track of all unique character arrays used
  97 // by String objects. Each table entry weakly points to an character array, allowing
  98 // otherwise unreachable character arrays to be declared dead and pruned from the
  99 // table.
 100 //
 101 // The table is dynamically resized to accommodate the current number of table entries.


 145   // Table statistics, only used for logging.
 146   static uintx                    _entries_added;
 147   static uintx                    _entries_removed;
 148   static uintx                    _resize_count;
 149   static uintx                    _rehash_count;
 150 
 151   G1StringDedupTable(size_t size, jint hash_seed = 0);
 152   ~G1StringDedupTable();
 153 
 154   // Returns the hash bucket at the given index.
 155   G1StringDedupEntry** bucket(size_t index) {
 156     return _buckets + index;
 157   }
 158 
 159   // Returns the hash bucket index for the given hash code.
 160   size_t hash_to_index(unsigned int hash) {
 161     return (size_t)hash & (_size - 1);
 162   }
 163 
 164   // Adds a new table entry to the given hash bucket.
 165   void add(typeArrayOop value, bool latin1, unsigned int hash, G1StringDedupEntry** list);
 166 
 167   // Removes the given table entry from the table.
 168   void remove(G1StringDedupEntry** pentry, uint worker_id);
 169 
 170   // Transfers a table entry from the current table to the destination table.
 171   void transfer(G1StringDedupEntry** pentry, G1StringDedupTable* dest);
 172 
 173   // Returns an existing character array in the given hash bucket, or NULL
 174   // if no matching character array exists.
 175   typeArrayOop lookup(typeArrayOop value, bool latin1, unsigned int hash,
 176                       G1StringDedupEntry** list, uintx &count);
 177 
 178   // Returns an existing character array in the table, or inserts a new
 179   // table entry if no matching character array exists.
 180   typeArrayOop lookup_or_add_inner(typeArrayOop value, bool latin1, unsigned int hash);
 181 
 182   // Thread safe lookup or add of table entry
 183   static typeArrayOop lookup_or_add(typeArrayOop value, bool latin1, unsigned int hash) {
 184     // Protect the table from concurrent access. Also note that this lock
 185     // acts as a fence for _table, which could have been replaced by a new
 186     // instance if the table was resized or rehashed.
 187     MutexLockerEx ml(StringDedupTable_lock, Mutex::_no_safepoint_check_flag);
 188     return _table->lookup_or_add_inner(value, latin1, hash);
 189   }
 190 
 191   // Returns true if the hashtable is currently using a Java compatible
 192   // hash function.
 193   static bool use_java_hash() {
 194     return _table->_hash_seed == 0;
 195   }
 196 
 197   static bool equals(typeArrayOop value1, typeArrayOop value2);
 198 
 199   // Computes the hash code for the given character array, using the
 200   // currently active hash function and hash seed.
 201   static unsigned int hash_code(typeArrayOop value, bool latin1);
 202 
 203   static uintx unlink_or_oops_do(G1StringDedupUnlinkOrOopsDoClosure* cl,
 204                                  size_t partition_begin,
 205                                  size_t partition_end,
 206                                  uint worker_id);
 207 
 208 public:
 209   static void create();
 210 
 211   // Deduplicates the given String object, or adds its backing
 212   // character array to the deduplication hashtable.
 213   static void deduplicate(oop java_string, G1StringDedupStat& stat);
 214 
 215   // If a table resize is needed, returns a newly allocated empty
 216   // hashtable of the proper size.
 217   static G1StringDedupTable* prepare_resize();
 218 
 219   // Installs a newly resized table as the currently active table
 220   // and deletes the previously active table.
 221   static void finish_resize(G1StringDedupTable* resized_table);
< prev index next >