changeset 655:bcbcc318f250

Performance: reuse unzip output buffer
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 04 Jul 2013 18:36:38 +0200
parents 12a4f60ea972
children a937e63b6e02
files src/org/tmatesoft/hg/internal/InflaterDataAccess.java src/org/tmatesoft/hg/internal/RevlogStream.java test/org/tmatesoft/hg/test/TestInflaterDataAccess.java
diffstat 3 files changed, 14 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/InflaterDataAccess.java	Wed Jul 03 15:11:40 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/InflaterDataAccess.java	Thu Jul 04 18:36:38 2013 +0200
@@ -39,22 +39,22 @@
 	private int decompressedLength;
 
 	public InflaterDataAccess(DataAccess dataAccess, long offset, int compressedLength) {
-		this(dataAccess, offset, compressedLength, -1, new Inflater(), new byte[512]);
+		this(dataAccess, offset, compressedLength, -1, new Inflater(), new byte[512], null);
 	}
 
 	public InflaterDataAccess(DataAccess dataAccess, long offset, int compressedLength, int actualLength) {
-		this(dataAccess, offset, compressedLength, actualLength, new Inflater(), new byte[512]);
+		this(dataAccess, offset, compressedLength, actualLength, new Inflater(), new byte[512], null);
 	}
 
-	public InflaterDataAccess(DataAccess dataAccess, long offset, int compressedLength, int actualLength, Inflater inflater, byte[] buf) {
+	public InflaterDataAccess(DataAccess dataAccess, long offset, int compressedLength, int actualLength, Inflater inflater, byte[] inBuf, ByteBuffer outBuf) {
 		super(dataAccess, offset, compressedLength);
-		if (inflater == null || buf == null) {
+		if (inflater == null || inBuf == null) {
 			throw new IllegalArgumentException();
 		}
 		this.inflater = inflater;
 		this.decompressedLength = actualLength;
-		inBuffer = buf;
-		outBuffer = ByteBuffer.allocate(inBuffer.length * 2);
+		inBuffer = inBuf;
+		outBuffer = outBuf == null ? ByteBuffer.allocate(inBuffer.length * 2) : outBuf;
 		outBuffer.limit(0); // there's nothing to read in the buffer 
 	}
 	
--- a/src/org/tmatesoft/hg/internal/RevlogStream.java	Wed Jul 03 15:11:40 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/RevlogStream.java	Thu Jul 04 18:36:38 2013 +0200
@@ -26,6 +26,7 @@
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.SoftReference;
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.zip.Inflater;
@@ -632,6 +633,7 @@
 		private final Inflater inflater = new Inflater();
 		// can share buffer between instances of InflaterDataAccess as I never read any two of them in parallel
 		private final byte[] inflaterBuffer = new byte[10 * 1024]; // TODO [post-1.1] consider using DAP.DEFAULT_FILE_BUFFER
+		private final ByteBuffer inflaterOutBuffer = ByteBuffer.allocate(inflaterBuffer.length * 2);
 		private final byte[] nodeidBuf = new byte[20];
 		// revlog record fields
 		private long offset;
@@ -741,7 +743,7 @@
 				final byte firstByte = streamDataAccess.readByte();
 				if (firstByte == 0x78 /* 'x' */) {
 					inflater.reset();
-					userDataAccess = new InflaterDataAccess(streamDataAccess, streamOffset, compressedLen, isPatch(i) ? -1 : actualLen, inflater, inflaterBuffer);
+					userDataAccess = new InflaterDataAccess(streamDataAccess, streamOffset, compressedLen, isPatch(i) ? -1 : actualLen, inflater, inflaterBuffer, inflaterOutBuffer);
 				} else if (firstByte == 0x75 /* 'u' */) {
 					userDataAccess = new FilterDataAccess(streamDataAccess, streamOffset+1, compressedLen-1);
 				} else {
--- a/test/org/tmatesoft/hg/test/TestInflaterDataAccess.java	Wed Jul 03 15:11:40 2013 +0200
+++ b/test/org/tmatesoft/hg/test/TestInflaterDataAccess.java	Thu Jul 04 18:36:38 2013 +0200
@@ -51,7 +51,7 @@
 	@Test
 	public void testSeek() throws Exception {
 		DataAccess zip = zip(testContent1);
-		InflaterDataAccess ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25]);
+		InflaterDataAccess ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25], null);
 		ida.seek(20);
 		final int bufferCapacity = 10;
 		ByteBuffer chunk1 = ByteBuffer.allocate(bufferCapacity);
@@ -66,15 +66,15 @@
 	@Test
 	public void testLength() throws Exception {
 		DataAccess zip = zip(testContent1);
-		InflaterDataAccess ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25]);
+		InflaterDataAccess ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25], null);
 		errorCollector.assertEquals("Plain #length()", testContent1.length, ida.length());
 		//
-		ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25]);
+		ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25], null);
 		byte[] dummy = new byte[30];
 		ida.readBytes(dummy, 0, dummy.length);
 		errorCollector.assertEquals("#length() after readBytes()", testContent1.length, ida.length());
 		//
-		ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25]);
+		ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25], null);
 		// consume most of the stream, so that all original compressed data is already read
 		dummy = new byte[testContent1.length - 1];
 		ida.readBytes(dummy, 0, dummy.length);
@@ -86,7 +86,7 @@
 	@Test
 	public void testReadBytes() throws Exception {
 		DataAccess zip = zip(testContent1);
-		InflaterDataAccess ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25]);
+		InflaterDataAccess ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25], null);
 		ida.skip(10);
 		byte[] chunk1 = new byte[22];
 		ida.readBytes(chunk1, 0, 20);