< prev index next >

src/java.base/share/classes/java/lang/Integer.java

Print this page
rev 51374 : 8209120: Archive the Integer.IntegerCache
Reviewed-by: jiangli, alanb, plevart

@@ -995,11 +995,12 @@
      */
 
     private static class IntegerCache {
         static final int low = -128;
         static final int high;
-        static final Integer cache[];
+        static final Integer[] cache;
+        static Integer[] archivedCache;
 
         static {
             // high value may be configured by property
             int h = 127;
             String integerCacheHighPropValue =

@@ -1014,15 +1015,23 @@
                     // If the property cannot be parsed into an int, ignore it.
                 }
             }
             high = h;
 
-            cache = new Integer[(high - low) + 1];
-            int j = low;
-            for(int k = 0; k < cache.length; k++)
-                cache[k] = new Integer(j++);
+            // Load IntegerCache.archivedCache from archive, if possible
+            VM.initializeFromArchive(IntegerCache.class);
+            int size = (high - low) + 1;
 
+            // Use the archived cache if it exists and is large enough
+            if (archivedCache == null || size > archivedCache.length) {
+                Integer[] c = new Integer[size];
+                int j = low;
+                for(int k = 0; k < c.length; k++)
+                    c[k] = new Integer(j++);
+                archivedCache = c;
+            }
+            cache = archivedCache;
             // range [-128, 127] must be interned (JLS7 5.1.7)
             assert IntegerCache.high >= 127;
         }
 
         private IntegerCache() {}
< prev index next >