Mercurial > jhg
changeset 288:b11f6a08f748
Avoid boxing int values and list resizes on revlog read
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Sat, 10 Sep 2011 00:18:39 +0200 |
parents | ed6b74a58c66 |
children | 086a326f181f |
files | src/org/tmatesoft/hg/internal/IntVector.java src/org/tmatesoft/hg/internal/RevlogStream.java |
diffstat | 2 files changed, 98 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- /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; + } +}
--- 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<Integer> resBases = new ArrayList<Integer>(); - ArrayList<Integer> resOffsets = new ArrayList<Integer>(); 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 {