changeset 575:8bf184c9d733

Issue 43: poor performance with InflaterDataAccess. Phase 1: test existing code, fix defects found
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 16 Apr 2013 16:59:59 +0200
parents 88afffd39899
children 3c4db86e8c1f
files src/org/tmatesoft/hg/internal/InflaterDataAccess.java test/org/tmatesoft/hg/test/TestInflaterDataAccess.java
diffstat 2 files changed, 136 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/InflaterDataAccess.java	Tue Apr 16 14:44:57 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/InflaterDataAccess.java	Tue Apr 16 16:59:59 2013 +0200
@@ -84,20 +84,23 @@
 		int c = 0;
 		int oldPos = decompressedPos;
 		byte[] dummy = new byte[buffer.length];
-		int toRead;
-		while ((toRead = super.available()) > 0) {
-			if (toRead > buffer.length) {
-				toRead = buffer.length;
-			}
-			super.readBytes(buffer, 0, toRead);
-			inflater.setInput(buffer, 0, toRead);
-			try {
+		try {
+			int toRead = -1;
+			do {
 				while (!inflater.needsInput()) {
 					c += inflater.inflate(dummy, 0, dummy.length);
 				}
-			} catch (DataFormatException ex) {
-				throw new IOException(ex.toString());
-			}
+				if (inflater.needsInput() && (toRead = super.available()) > 0) {
+					// fill:
+					if (toRead > buffer.length) {
+						toRead = buffer.length;
+					}
+					super.readBytes(buffer, 0, toRead);
+					inflater.setInput(buffer, 0, toRead);
+				}
+			} while(toRead > 0);
+		} catch (DataFormatException ex) {
+			throw new IOException(ex.toString());
 		}
 		decompressedLength = c + oldPos;
 		reset();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/org/tmatesoft/hg/test/TestInflaterDataAccess.java	Tue Apr 16 16:59:59 2013 +0200
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2013 TMate Software Ltd
+ *  
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * For information on how to redistribute this software under
+ * the terms of a license other than GNU General Public License
+ * contact TMate Software at support@hg4j.com
+ */
+package org.tmatesoft.hg.test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.zip.DeflaterOutputStream;
+import java.util.zip.Inflater;
+
+import org.junit.Test;
+import org.tmatesoft.hg.internal.ByteArrayDataAccess;
+import org.tmatesoft.hg.internal.DataAccess;
+import org.tmatesoft.hg.internal.InflaterDataAccess;
+
+/**
+ * 
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class TestInflaterDataAccess {
+	private final ErrorCollectorExt errorCollector = new ErrorCollectorExt();
+	
+	private final byte[] testContent1 = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.".getBytes();
+	
+	private DataAccess zip(byte[] source) throws IOException {
+		ByteArrayOutputStream bos = new ByteArrayOutputStream();
+		DeflaterOutputStream dos = new DeflaterOutputStream(bos);
+		dos.write(source);
+		dos.flush();
+		dos.close();
+		return new ByteArrayDataAccess(bos.toByteArray());
+	}
+	
+	@Test
+	public void testSeek() throws Exception {
+		DataAccess zip = zip(testContent1);
+		InflaterDataAccess ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25]);
+		ida.seek(20);
+		final int bufferCapacity = 10;
+		ByteBuffer chunk1 = ByteBuffer.allocate(bufferCapacity);
+		ida.readBytes(chunk1);
+		errorCollector.assertTrue(new ByteArraySlice(testContent1, 20, bufferCapacity).equalsTo(chunk1.array()));
+		ida.skip(-bufferCapacity);
+		ByteBuffer chunk2 = ByteBuffer.allocate(bufferCapacity);
+		ida.readBytes(chunk2);
+		errorCollector.assertEquals(chunk1, chunk2);
+	}
+	
+	@Test
+	public void testLength() throws Exception {
+		DataAccess zip = zip(testContent1);
+		InflaterDataAccess ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25]);
+		errorCollector.assertEquals("Plain #length()", testContent1.length, ida.length());
+		//
+		ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25]);
+		byte[] dummy = new byte[30];
+		ida.readBytes(dummy, 0, dummy.length);
+		int lll = ida.length();
+		errorCollector.assertEquals("#length() after readBytes()", testContent1.length, ida.length());
+		//
+		ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25]);
+		// consume most of the stream, so that all original compressed data is already read
+		ida.readBytes(ByteBuffer.allocate(testContent1.length - 1));
+		errorCollector.assertEquals("#length() after origin was completely read", testContent1.length, ida.length());
+	}
+
+	@Test
+	public void testReadBytes() throws Exception {
+		DataAccess zip = zip(testContent1);
+		InflaterDataAccess ida = new InflaterDataAccess(zip, 0, zip.length(), -1, new Inflater(), new byte[25]);
+		ida.skip(10);
+		byte[] chunk1 = new byte[22];
+		ida.readBytes(chunk1, 0, 20);
+		chunk1[20] = ida.readByte();
+		chunk1[21] = ida.readByte();
+		ida.skip(5);
+		byte[] chunk2 = new byte[12];
+		chunk2[0] = ida.readByte();
+		chunk2[1] = ida.readByte();
+		ida.readBytes(chunk2, 2, 10);
+		errorCollector.assertTrue(new ByteArraySlice(testContent1, 10, 22).equalsTo(chunk1));
+		errorCollector.assertTrue(new ByteArraySlice(testContent1, 10+22+5, 12).equalsTo(chunk2));
+	}
+
+	private static class ByteArraySlice {
+		public final byte[] array;
+		public final int offset, length;
+
+		public ByteArraySlice(byte[] array, int offset, int length) {
+			this.array = array;
+			this.offset = offset;
+			this.length = length;
+		}
+		
+		public boolean equalsTo(byte[] another) {
+			if (another == null || another.length != length) {
+				return false;
+			}
+			for (int i = 0; i < length; i++) {
+				if (array[offset + i] != another[i]) {
+					return false;
+				}
+			}
+			return true;
+		}
+	}
+}