# HG changeset patch # User Artem Tikhomirov # Date 1315606719 -7200 # Node ID b11f6a08f7488d69aacfc2b2d3f3daaf376226a5 # Parent ed6b74a58c66c1b16eb420fe1a1c65bcbd083e3d Avoid boxing int values and list resizes on revlog read diff -r ed6b74a58c66 -r b11f6a08f748 src/org/tmatesoft/hg/internal/IntVector.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/tmatesoft/hg/internal/IntVector.java Sat Sep 10 00:18:39 2011 +0200 @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2011 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.internal; + +/** + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +class IntVector { + + private int[] data; + private final int increment; + private int count; + + + public IntVector() { + this(16, -1); + } + + // increment == -1: grow by power of two. + // increment == 0: no resize (Exception will be thrown on attempt to add past capacity) + public IntVector(int initialCapacity, int increment) { + data = new int[initialCapacity]; + this.increment = increment; + } + + public void add(int v) { + if (count == data.length) { + grow(); + } + data[count++] = v; + } + + public int get(int i) { + if (i < 0 || i >= count) { + throw new IndexOutOfBoundsException(String.format("Index: %d, size: %d", i, count)); + } + return data[i]; + } + + public int size() { + return count; + } + + public int[] toArray() { + int[] rv = new int[count]; + System.arraycopy(data, 0, rv, 0, count); + return rv; + } + + /** + * Use only when this instance won't be used any longer + */ + @Experimental + int[] toArray(boolean internalIfSizeMatchCapacity) { + if (count == data.length) { + return data; + } + return toArray(); + } + + private void grow() { + if (increment == 0) { + // throw specific exception right away + return; + } + int newCapacity = increment < 0 ? data.length << 1 : data.length + increment; + assert newCapacity > 0 && newCapacity != data.length : newCapacity; + int[] newData = new int[newCapacity]; + System.arraycopy(data, 0, newData, 0, count); + data = newData; + } +} diff -r ed6b74a58c66 -r b11f6a08f748 src/org/tmatesoft/hg/internal/RevlogStream.java --- a/src/org/tmatesoft/hg/internal/RevlogStream.java Wed Sep 07 09:33:27 2011 +0200 +++ b/src/org/tmatesoft/hg/internal/RevlogStream.java Sat Sep 10 00:18:39 2011 +0200 @@ -269,8 +269,6 @@ if (baseRevisions != null && baseRevisions.length > 0) { return; } - ArrayList resBases = new ArrayList(); - ArrayList resOffsets = new ArrayList(); DataAccess da = getIndexStream(); try { if (da.isEmpty()) { @@ -282,6 +280,14 @@ da.readInt(); // just to skip next 4 bytes of offset + flags final int INLINEDATA = 1 << 16; inline = (versionField & INLINEDATA) != 0; + IntVector resBases, resOffsets = null; + int entryCountGuess = da.length() / REVLOGV1_RECORD_SIZE; + if (inline) { + entryCountGuess >>>= 2; // pure guess, assume useful data takes 3/4 of total space + resOffsets = new IntVector(entryCountGuess, 5000); + } + resBases = new IntVector(entryCountGuess, 5000); + long offset = 0; // first offset is always 0, thus Hg uses it for other purposes while(true) { int compressedLen = da.readInt(); @@ -309,9 +315,9 @@ } if (da.isEmpty()) { // fine, done then - baseRevisions = toArray(resBases); + baseRevisions = resBases.toArray(true); if (inline) { - indexRecordOffset = toArray(resOffsets); + indexRecordOffset = resOffsets.toArray(true); } break; } else {