409953 More efficient implementation of BufferUtil.writeTo(...)

This commit is contained in:
Thomas Becker 2013-06-06 12:59:18 +02:00 committed by Joakim Erdfelt
parent 7792c1dad7
commit 928275271d
2 changed files with 23 additions and 24 deletions

View File

@ -84,7 +84,7 @@ import java.nio.charset.Charset;
*/ */
public class BufferUtil public class BufferUtil
{ {
static final int TEMP_BUFFER_SIZE = 4096; static final int TEMP_BUFFER_SIZE = 512;
static final byte SPACE = 0x20; static final byte SPACE = 0x20;
static final byte MINUS = '-'; static final byte MINUS = '-';
static final byte[] DIGIT = static final byte[] DIGIT =
@ -439,19 +439,12 @@ public class BufferUtil
else else
{ {
byte[] bytes = new byte[TEMP_BUFFER_SIZE]; byte[] bytes = new byte[TEMP_BUFFER_SIZE];
int j = 0; while(buffer.hasRemaining()){
for (int i = buffer.position(); i < buffer.limit(); i++) int byteCountToWrite = Math.min(buffer.remaining(), TEMP_BUFFER_SIZE);
{ buffer.get(bytes, 0, byteCountToWrite);
bytes[j] = buffer.get(i); out.write(bytes,0 , byteCountToWrite);
j++;
if (j == TEMP_BUFFER_SIZE)
{
out.write(bytes);
j = 0;
} }
} }
out.write(bytes, 0, j);
}
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */

View File

@ -241,25 +241,31 @@ public class BufferUtilTest
"ignored.") "ignored.")
public void testWriteToMicrobenchmark() throws IOException public void testWriteToMicrobenchmark() throws IOException
{ {
int capacity = 1024 * 1024; int capacity = 1024 * 128;
int iterations = 30; int iterations = 100;
int testRuns = 10;
byte[] bytes = new byte[capacity]; byte[] bytes = new byte[capacity];
new Random().nextBytes(bytes); new Random().nextBytes(bytes);
ByteBuffer buffer = BufferUtil.allocate(capacity); ByteBuffer buffer = BufferUtil.allocate(capacity);
BufferUtil.append(buffer, bytes, 0, capacity); BufferUtil.append(buffer, bytes, 0, capacity);
long startTest = System.nanoTime();
for (int i = 0; i < testRuns; i++)
{
long start = System.nanoTime(); long start = System.nanoTime();
for (int i = 0; i < iterations; i++) for (int j = 0; j < iterations; j++)
{ {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
long startRun = System.nanoTime(); long startRun = System.nanoTime();
BufferUtil.writeTo(buffer.asReadOnlyBuffer(), out); BufferUtil.writeTo(buffer.asReadOnlyBuffer(), out);
long elapsedRun = System.nanoTime() - startRun; long elapsedRun = System.nanoTime() - startRun;
LOG.warn("run elapsed={}ms", elapsedRun / 1000); // LOG.warn("run elapsed={}ms", elapsedRun / 1000);
assertThat("Bytes in out equal bytes in buffer", Arrays.equals(bytes, out.toByteArray()), is(true)); assertThat("Bytes in out equal bytes in buffer", Arrays.equals(bytes, out.toByteArray()), is(true));
} }
long elapsed = System.nanoTime() - start; long elapsed = System.nanoTime() - start;
LOG.warn("elapsed={}ms average={}ms", elapsed / 1000, elapsed/iterations/1000); LOG.warn("elapsed={}ms average={}ms", elapsed / 1000, elapsed/iterations/1000);
} }
LOG.warn("overall average: {}ms", (System.nanoTime() - startTest) / testRuns / iterations / 1000);
}
@Test @Test
public void testWriteToWithBufferThatDoesNotExposeArrayAndSmallContent() throws IOException public void testWriteToWithBufferThatDoesNotExposeArrayAndSmallContent() throws IOException