reduce bytearray copy to minimal optimize VSizeIndexedWriter

This commit is contained in:
binlijin 2015-12-10 16:34:39 +08:00
parent a2d0bea223
commit 0eafbd55b2
3 changed files with 35 additions and 3 deletions

View File

@ -50,11 +50,31 @@ public class VSizeIndexedInts implements IndexedInts, Comparable<VSizeIndexedInt
return fromList(Lists.<Integer>newArrayList(), 0); return fromList(Lists.<Integer>newArrayList(), 0);
} }
/**
* provide for performance reason.
*/
public static byte[] getBytesNoPaddingfromList(List<Integer> 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<Integer> list, int maxValue) public static VSizeIndexedInts fromList(List<Integer> list, int maxValue)
{ {
int numBytes = getNumBytesForMax(maxValue); int numBytes = getNumBytesForMax(maxValue);
final ByteBuffer buffer = ByteBuffer.allocate((list.size() * numBytes) + (4 - numBytes)); 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<Integer> list, int numBytes, int maxValue)
{
int i = 0; int i = 0;
for (Integer val : list) { for (Integer val : list) {
if (val < 0) { if (val < 0) {
@ -69,8 +89,6 @@ public class VSizeIndexedInts implements IndexedInts, Comparable<VSizeIndexedInt
++i; ++i;
} }
buffer.position(0); buffer.position(0);
return new VSizeIndexedInts(buffer.asReadOnlyBuffer(), numBytes);
} }
public static byte getNumBytesForMax(int maxValue) public static byte getNumBytesForMax(int maxValue)

View File

@ -70,7 +70,7 @@ public class VSizeIndexedWriter implements Closeable
public void write(List<Integer> ints) throws IOException public void write(List<Integer> 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); valuesOut.write(bytesToWrite);

View File

@ -22,9 +22,12 @@ package io.druid.segment.data;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import com.google.common.primitives.Ints;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.Channels; 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<Integer> 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);
}
} }