aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Becker2013-06-06 06:59:18 (EDT)
committerThomas Becker2013-06-06 06:59:18 (EDT)
commitd3b96328f9e3d30e7c4318fad2b949cad2425675 (patch)
tree4e6c8c171de6a4a8b70d64fcac80e9ff34672661
parent4a10f4296c2d9e7be34e64bfa9198c6933f2f60c (diff)
downloadorg.eclipse.jetty.project-d3b96328f9e3d30e7c4318fad2b949cad2425675.zip
org.eclipse.jetty.project-d3b96328f9e3d30e7c4318fad2b949cad2425675.tar.gz
org.eclipse.jetty.project-d3b96328f9e3d30e7c4318fad2b949cad2425675.tar.bz2
409953 More efficient implementation of BufferUtil.writeTo(...)
-rw-r--r--jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java17
-rw-r--r--jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java30
2 files changed, 23 insertions, 24 deletions
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java
index bff42a4..85ee7a6 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java
@@ -84,7 +84,7 @@ import java.nio.charset.Charset;
*/
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 MINUS = '-';
static final byte[] DIGIT =
@@ -439,18 +439,11 @@ public class BufferUtil
else
{
byte[] bytes = new byte[TEMP_BUFFER_SIZE];
- int j = 0;
- for (int i = buffer.position(); i < buffer.limit(); i++)
- {
- bytes[j] = buffer.get(i);
- j++;
- if (j == TEMP_BUFFER_SIZE)
- {
- out.write(bytes);
- j = 0;
- }
+ while(buffer.hasRemaining()){
+ int byteCountToWrite = Math.min(buffer.remaining(), TEMP_BUFFER_SIZE);
+ buffer.get(bytes, 0, byteCountToWrite);
+ out.write(bytes,0 , byteCountToWrite);
}
- out.write(bytes, 0, j);
}
}
diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java
index 77372d8..886474e 100644
--- a/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java
+++ b/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java
@@ -241,24 +241,30 @@ public class BufferUtilTest
"ignored.")
public void testWriteToMicrobenchmark() throws IOException
{
- int capacity = 1024 * 1024;
- int iterations = 30;
+ int capacity = 1024 * 128;
+ int iterations = 100;
+ int testRuns = 10;
byte[] bytes = new byte[capacity];
new Random().nextBytes(bytes);
ByteBuffer buffer = BufferUtil.allocate(capacity);
BufferUtil.append(buffer, bytes, 0, capacity);
- long start = System.nanoTime();
- for (int i = 0; i < iterations; i++)
+ long startTest = System.nanoTime();
+ for (int i = 0; i < testRuns; i++)
{
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- long startRun = System.nanoTime();
- BufferUtil.writeTo(buffer.asReadOnlyBuffer(), out);
- long elapsedRun = System.nanoTime() - startRun;
- LOG.warn("run elapsed={}ms", elapsedRun / 1000);
- assertThat("Bytes in out equal bytes in buffer", Arrays.equals(bytes, out.toByteArray()), is(true));
+ long start = System.nanoTime();
+ for (int j = 0; j < iterations; j++)
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ long startRun = System.nanoTime();
+ BufferUtil.writeTo(buffer.asReadOnlyBuffer(), out);
+ long elapsedRun = System.nanoTime() - startRun;
+// LOG.warn("run elapsed={}ms", elapsedRun / 1000);
+ assertThat("Bytes in out equal bytes in buffer", Arrays.equals(bytes, out.toByteArray()), is(true));
+ }
+ long elapsed = System.nanoTime() - start;
+ LOG.warn("elapsed={}ms average={}ms", elapsed / 1000, elapsed/iterations/1000);
}
- long elapsed = System.nanoTime() - start;
- LOG.warn("elapsed={}ms average={}ms", elapsed / 1000, elapsed/iterations/1000);
+ LOG.warn("overall average: {}ms", (System.nanoTime() - startTest) / testRuns / iterations / 1000);
}
@Test