changeset 20892:2818f2862a0d

Use increasingly big arrays to avoid wasting space when writing few bytes
author Christian Wimmer <christian.wimmer@oracle.com>
date Sat, 11 Apr 2015 00:12:10 -0700
parents 129a09815063
children d689979bf956
files graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/util/UnsafeArrayTypeWriter.java
diffstat 1 files changed, 9 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/util/UnsafeArrayTypeWriter.java	Fri Apr 10 21:53:38 2015 -0700
+++ b/graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/util/UnsafeArrayTypeWriter.java	Sat Apr 11 00:12:10 2015 -0700
@@ -37,12 +37,17 @@
  */
 public class UnsafeArrayTypeWriter implements TypeWriter {
 
-    private static final int CHUNK_SIZE = 4000;
+    private static final int MIN_CHUNK_LENGTH = 200;
+    private static final int MAX_CHUNK_LENGTH = 16000;
 
     static class Chunk {
-        protected final byte[] data = new byte[CHUNK_SIZE];
+        protected final byte[] data;
         protected int size;
         protected Chunk next;
+
+        protected Chunk(int arrayLength) {
+            data = new byte[arrayLength];
+        }
     }
 
     private Chunk firstChunk;
@@ -50,7 +55,7 @@
     private int totalSize;
 
     public UnsafeArrayTypeWriter() {
-        firstChunk = new Chunk();
+        firstChunk = new Chunk(MIN_CHUNK_LENGTH);
         writeChunk = firstChunk;
     }
 
@@ -153,7 +158,7 @@
 
     private long writeOffset(int writeBytes) {
         if (writeChunk.size + writeBytes >= writeChunk.data.length) {
-            Chunk newChunk = new Chunk();
+            Chunk newChunk = new Chunk(Math.min(writeChunk.data.length * 2, MAX_CHUNK_LENGTH));
             writeChunk.next = newChunk;
             writeChunk = newChunk;
         }