Apache Lucene open-source search software
LUCENE-2360: don't use .toArray to recycle byte[] blocks used by doc stores
Michael McCandlessa528a707c10a
diff --git a/lucene/src/java/org/apache/lucene/index/ByteBlockPool.java b/lucene/src/java/org/apache/lucene/index/ByteBlockPool.javaindex 36832da5131..8d144588900 100644--- a/lucene/src/java/org/apache/lucene/index/ByteBlockPool.java+++ b/lucene/src/java/org/apache/lucene/index/ByteBlockPool.java@@ -34,6 +34,7 @@ package org.apache.lucene.index;* hit a non-zero byte. */import java.util.Arrays;+import java.util.List;import static org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_OBJECT_REF;import org.apache.lucene.util.ArrayUtil;@@ -42,6 +43,7 @@ final class ByteBlockPool {abstract static class Allocator {abstract void recycleByteBlocks(byte[][] blocks, int start, int end);+ abstract void recycleByteBlocks(List<byte[]> blocks);abstract byte[] getByteBlock(boolean trackAllocations);}diff --git a/lucene/src/java/org/apache/lucene/index/DocumentsWriter.java b/lucene/src/java/org/apache/lucene/index/DocumentsWriter.javaindex 00159527c91..803c81f1661 100644--- a/lucene/src/java/org/apache/lucene/index/DocumentsWriter.java+++ b/lucene/src/java/org/apache/lucene/index/DocumentsWriter.java@@ -207,8 +207,7 @@ final class DocumentsWriter {// Recycle the blocksfinal int blockCount = buffers.size();- final byte[][] blocks = buffers.toArray( new byte[blockCount][] );- perDocAllocator.recycleByteBlocks(blocks, 0, blockCount);+ perDocAllocator.recycleByteBlocks(buffers);buffers.clear();sizeInBytes = 0;@@ -1285,6 +1284,15 @@ final class DocumentsWriter {freeByteBlocks.add(blocks[i]);}}++ @Override+ void recycleByteBlocks(List<byte[]> blocks) {+ synchronized(DocumentsWriter.this) {+ final int size = blocks.size();+ for(int i=0;i<size;i++)+ freeByteBlocks.add(blocks.get(i));+ }+ }}/* Initial chunks size of the shared int[] blocks used todiff --git a/lucene/src/test/org/apache/lucene/index/TestByteSlices.java b/lucene/src/test/org/apache/lucene/index/TestByteSlices.javaindex fefc74cd546..f28adcde1fc 100644--- a/lucene/src/test/org/apache/lucene/index/TestByteSlices.java+++ b/lucene/src/test/org/apache/lucene/index/TestByteSlices.java@@ -16,6 +16,7 @@ package org.apache.lucene.index;import java.util.Random;import java.util.ArrayList;+import java.util.List;import org.apache.lucene.util.LuceneTestCase;public class TestByteSlices extends LuceneTestCase {@@ -41,6 +42,13 @@ public class TestByteSlices extends LuceneTestCase {for(int i=start;i<end;i++)freeByteBlocks.add(blocks[i]);}++ @Override+ synchronized void recycleByteBlocks(List<byte[]> blocks) {+ final int size = blocks.size();+ for(int i=0;i<size;i++)+ freeByteBlocks.add(blocks.get(i));+ }}public void testBasic() throws Throwable {