8087143: Reduce Symbol::_identity_hash to 2 bytes
authorminqi
Mon, 29 Jun 2015 13:48:55 -0700
changeset 31614 f60b185e0bdf
parent 31613 6eb832364566
child 31615 e48d94b97b6c
child 31616 01c68517607d
8087143: Reduce Symbol::_identity_hash to 2 bytes Summary: Convert Symbol::_identity_hash from integer to short integer to save two bytes. Also change identity_hash() to have 'this' and first two bytes of symbol join the calculation. Reviewed-by: iklam, coleenp, shade
hotspot/agent/src/share/classes/sun/jvm/hotspot/oops/Symbol.java
hotspot/src/share/vm/oops/symbol.cpp
hotspot/src/share/vm/oops/symbol.hpp
hotspot/src/share/vm/runtime/vmStructs.cpp
--- a/hotspot/agent/src/share/classes/sun/jvm/hotspot/oops/Symbol.java	Mon Jun 29 17:24:52 2015 +0000
+++ b/hotspot/agent/src/share/classes/sun/jvm/hotspot/oops/Symbol.java	Mon Jun 29 13:48:55 2015 -0700
@@ -47,7 +47,7 @@
     Type type  = db.lookupType("Symbol");
     length     = type.getCIntegerField("_length");
     baseOffset = type.getField("_body").getOffset();
-    idHash = type.getCIntegerField("_identity_hash");
+    idHash = type.getJShortField("_identity_hash");
   }
 
   // Format:
@@ -81,9 +81,9 @@
     return addr.getJByteAt(baseOffset + index);
   }
 
-  private static CIntegerField idHash;
+  private static JShortField idHash;
 
-  public int identityHash() { return     (int)idHash.getValue(this.addr); }
+  public short identityHash() { return     (short)idHash.getValue(this.addr); }
 
   public boolean equals(byte[] modUTF8Chars) {
     int l = (int) getLength();
--- a/hotspot/src/share/vm/oops/symbol.cpp	Mon Jun 29 17:24:52 2015 +0000
+++ b/hotspot/src/share/vm/oops/symbol.cpp	Mon Jun 29 13:48:55 2015 -0700
@@ -35,7 +35,7 @@
 Symbol::Symbol(const u1* name, int length, int refcount) {
   _refcount = refcount;
   _length = length;
-  _identity_hash = os::random();
+  _identity_hash = (short)os::random();
   for (int i = 0; i < _length; i++) {
     byte_at_put(i, name[i]);
   }
--- a/hotspot/src/share/vm/oops/symbol.hpp	Mon Jun 29 17:24:52 2015 +0000
+++ b/hotspot/src/share/vm/oops/symbol.hpp	Mon Jun 29 13:48:55 2015 -0700
@@ -102,23 +102,18 @@
 // type without virtual functions.
 class ClassLoaderData;
 
-// We separate the fields in SymbolBase from Symbol::_body so that
-// Symbol::size(int) can correctly calculate the space needed.
-class SymbolBase : public MetaspaceObj {
- public:
+class Symbol : public MetaspaceObj {
+  friend class VMStructs;
+  friend class SymbolTable;
+  friend class MoveSymbols;
+
+ private:
   ATOMIC_SHORT_PAIR(
     volatile short _refcount,  // needs atomic operation
     unsigned short _length     // number of UTF8 characters in the symbol (does not need atomic op)
   );
-  int            _identity_hash;
-};
-
-class Symbol : private SymbolBase {
-  friend class VMStructs;
-  friend class SymbolTable;
-  friend class MoveSymbols;
- private:
-  jbyte _body[1];
+  short _identity_hash;
+  jbyte _body[2];
 
   enum {
     // max_symbol_length is constrained by type of _length
@@ -126,7 +121,7 @@
   };
 
   static int size(int length) {
-    size_t sz = heap_word_size(sizeof(SymbolBase) + (length > 0 ? length : 0));
+    size_t sz = heap_word_size(sizeof(Symbol) + (length > 2 ? length - 2 : 0));
     return align_object_size(sz);
   }
 
@@ -150,8 +145,11 @@
 
   // Returns the largest size symbol we can safely hold.
   static int max_length()   { return max_symbol_length; }
-
-  int identity_hash()       { return _identity_hash; }
+  unsigned identity_hash() {
+    unsigned addr_bits = (unsigned)((uintptr_t)this >> (LogMinObjAlignmentInBytes + 3));
+    return (unsigned)_identity_hash |
+           ((addr_bits ^ (_length << 8) ^ (( _body[0] << 8) | _body[1])) << 16);
+  }
 
   // For symbol table alternate hashing
   unsigned int new_hash(juint seed);
--- a/hotspot/src/share/vm/runtime/vmStructs.cpp	Mon Jun 29 17:24:52 2015 +0000
+++ b/hotspot/src/share/vm/runtime/vmStructs.cpp	Mon Jun 29 13:48:55 2015 -0700
@@ -403,7 +403,7 @@
   nonstatic_field(ObjArrayKlass,               _element_klass,                                Klass*)                                \
   nonstatic_field(ObjArrayKlass,               _bottom_klass,                                 Klass*)                                \
   volatile_nonstatic_field(Symbol,             _refcount,                                     short)                                 \
-  nonstatic_field(Symbol,                      _identity_hash,                                int)                                   \
+  nonstatic_field(Symbol,                      _identity_hash,                                short)                                 \
   nonstatic_field(Symbol,                      _length,                                       unsigned short)                        \
   unchecked_nonstatic_field(Symbol,            _body,                                         sizeof(jbyte)) /* NOTE: no type */     \
   nonstatic_field(TypeArrayKlass,              _max_length,                                   int)                                   \