< prev index next >

src/java.base/share/classes/jdk/internal/jimage/ImageStringsReader.java

Print this page
rev 16767 : 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
Reviewed-by: jlaskey
rev 16768 : imported patch imgstr_oneup

@@ -36,10 +36,12 @@
  * but also compiled and delivered as part of the jrtfs.jar to support access
  * to the jimage file provided by the shipped JDK by tools running on JDK 8.
  */
 public class ImageStringsReader implements ImageStrings {
     public static final int HASH_MULTIPLIER = 0x01000193;
+    public static final int POSITIVE_MASK = 0x7FFFFFFF;
+
     private final BasicImageReader reader;
 
     ImageStringsReader(BasicImageReader reader) {
         this.reader = Objects.requireNonNull(reader);
     }

@@ -52,44 +54,31 @@
     @Override
     public int add(final String string) {
         throw new InternalError("Can not add strings at runtime");
     }
 
-    private static int hashCode(byte[] bytes, int offset, int count, int seed) {
-        Objects.requireNonNull(bytes);
-
-        if (offset < 0 || count < 0 || offset > bytes.length - count) {
-            throw new IndexOutOfBoundsException("offset=" + offset + ", count=" + count);
-        }
-
-        int limit = offset + count;
-
-        if (limit < 0 || limit > bytes.length) {
-            throw new IndexOutOfBoundsException("limit=" + limit);
-        }
-
-        for (int i = offset; i < limit; i++) {
-            seed = (seed * HASH_MULTIPLIER) ^ (bytes[i] & 0xFF);
-        }
-
-        return seed & 0x7FFFFFFF;
+    public static int hashCode(String string) {
+        return hashCode(string, HASH_MULTIPLIER);
     }
 
-    public static int hashCode(byte[] bytes, int seed) {
-        return hashCode(bytes, 0, bytes.length, seed);
+    public static int hashCode(String string, int seed) {
+        return unmaskedHashCode(string, seed) & POSITIVE_MASK;
     }
 
-    public static int hashCode(byte[] bytes) {
-        return hashCode(bytes, 0, bytes.length, HASH_MULTIPLIER);
+    public static int hashCode(String module, String string) {
+        return hashCode(module, string, HASH_MULTIPLIER);
     }
 
-    public static int hashCode(String string) {
-        return hashCode(string, HASH_MULTIPLIER);
+    public static int hashCode(String module, String string, int seed) {
+        seed = unmaskedHashCode("/", seed);
+        seed = unmaskedHashCode(module, seed);
+        seed = unmaskedHashCode("/", seed);
+        seed = unmaskedHashCode(string, seed);
+        return seed & POSITIVE_MASK;
     }
 
-    public static int hashCode(String string, int seed) {
-
+    public static int unmaskedHashCode(String string, int seed) {
         int slen = string.length();
         byte[] buffer = null;
 
         for (int i = 0; i < slen; i++) {
             char ch = string.charAt(i);

@@ -118,12 +107,11 @@
                 seed = (seed * HASH_MULTIPLIER) ^ (0x80);
             } else {
                 seed = (seed * HASH_MULTIPLIER) ^ (uch);
             }
         }
-
-        return seed & 0x7FFFFFFF;
+        return seed;
     }
 
     static int charsFromMUTF8Length(byte[] bytes, int offset, int count) {
         int length = 0;
 

@@ -263,10 +251,11 @@
     }
 
     static int mutf8FromStringLength(String s) {
         int length = 0;
         int slen = s.length();
+
         for (int i = 0; i < slen; i++) {
             char ch = s.charAt(i);
             int uch = ch & 0xFFFF;
 
             if ((uch & ~0x7F) != 0) {
< prev index next >