From 0eafbd55b231ec283d9979c39a6d802114d6074b Mon Sep 17 00:00:00 2001 From: binlijin Date: Thu, 10 Dec 2015 16:34:39 +0800 Subject: [PATCH] reduce bytearray copy to minimal optimize VSizeIndexedWriter --- .../druid/segment/data/VSizeIndexedInts.java | 22 +++++++++++++++++-- .../segment/data/VSizeIndexedWriter.java | 2 +- .../segment/data/VSizeIndexedIntsTest.java | 14 ++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/processing/src/main/java/io/druid/segment/data/VSizeIndexedInts.java b/processing/src/main/java/io/druid/segment/data/VSizeIndexedInts.java index 82ce0fc833e..079c3b00005 100644 --- a/processing/src/main/java/io/druid/segment/data/VSizeIndexedInts.java +++ b/processing/src/main/java/io/druid/segment/data/VSizeIndexedInts.java @@ -50,11 +50,31 @@ public class VSizeIndexedInts implements IndexedInts, ComparablenewArrayList(), 0); } + /** + * provide for performance reason. + */ + public static byte[] getBytesNoPaddingfromList(List list, int maxValue) + { + int numBytes = getNumBytesForMax(maxValue); + + final ByteBuffer buffer = ByteBuffer.allocate((list.size() * numBytes)); + writeToBuffer(buffer, list, numBytes, maxValue); + + return buffer.array(); + } + public static VSizeIndexedInts fromList(List list, int maxValue) { int numBytes = getNumBytesForMax(maxValue); final ByteBuffer buffer = ByteBuffer.allocate((list.size() * numBytes) + (4 - numBytes)); + writeToBuffer(buffer, list, numBytes, maxValue); + + return new VSizeIndexedInts(buffer.asReadOnlyBuffer(), numBytes); + } + + private static void writeToBuffer(ByteBuffer buffer, List list, int numBytes, int maxValue) + { int i = 0; for (Integer val : list) { if (val < 0) { @@ -69,8 +89,6 @@ public class VSizeIndexedInts implements IndexedInts, Comparable ints) throws IOException { - byte[] bytesToWrite = ints == null ? EMPTY_ARRAY : VSizeIndexedInts.fromList(ints, maxId).getBytesNoPadding(); + byte[] bytesToWrite = ints == null ? EMPTY_ARRAY : VSizeIndexedInts.getBytesNoPaddingfromList(ints, maxId); valuesOut.write(bytesToWrite); diff --git a/processing/src/test/java/io/druid/segment/data/VSizeIndexedIntsTest.java b/processing/src/test/java/io/druid/segment/data/VSizeIndexedIntsTest.java index 5416c1e0a7d..29be4e8fb9c 100644 --- a/processing/src/test/java/io/druid/segment/data/VSizeIndexedIntsTest.java +++ b/processing/src/test/java/io/druid/segment/data/VSizeIndexedIntsTest.java @@ -22,9 +22,12 @@ package io.druid.segment.data; import org.junit.Assert; import org.junit.Test; +import com.google.common.primitives.Ints; + import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import java.nio.channels.Channels; +import java.util.List; /** */ @@ -63,4 +66,15 @@ public class VSizeIndexedIntsTest } } + @Test + public void testGetBytesNoPaddingfromList() throws Exception + { + final int[] array = {1, 2, 4, 5, 6, 8, 9, 10}; + List list = Ints.asList(array); + int maxValue = Ints.max(array); + VSizeIndexedInts ints = VSizeIndexedInts.fromList(list, maxValue); + byte[] bytes1 = ints.getBytesNoPadding(); + byte[] bytes2 = VSizeIndexedInts.getBytesNoPaddingfromList(list, maxValue); + Assert.assertArrayEquals(bytes1, bytes2); + } }