PERFORMANCE: More efficiently store offsets in AbstractByteBufferPageIO

Fixes #8333
This commit is contained in:
Armin 2017-09-20 10:46:08 +02:00 committed by Armin Braun
parent 416339f3c9
commit 08b93005a1
3 changed files with 66 additions and 2 deletions

View file

@ -42,7 +42,7 @@ public abstract class AbstractByteBufferPageIO implements PageIO {
protected int head; // head is the write position and is an int per ByteBuffer class position
protected byte version;
private CRC32 checkSummer;
private final List<Integer> offsetMap; // has to be extendable
private final IntVector offsetMap;
public AbstractByteBufferPageIO(int pageNum, int capacity) {
this.minSeqNum = 0;
@ -51,7 +51,7 @@ public abstract class AbstractByteBufferPageIO implements PageIO {
this.head = 0;
this.pageNum = pageNum;
this.capacity = capacity;
this.offsetMap = new ArrayList<>();
this.offsetMap = new IntVector();
this.checkSummer = new CRC32();
}

View file

@ -0,0 +1,42 @@
package org.logstash.ackedqueue.io;
final class IntVector {
private int count;
private int[] data;
IntVector() {
data = new int[1024];
count = 0;
}
/**
* Store the {@code int} to the underlying {@code int[]}, resizing it if necessary.
* @param num Int to store
*/
public void add(final int num) {
if (data.length < count + 1) {
final int[] old = data;
data = new int[data.length << 1];
System.arraycopy(old, 0, data, 0, old.length);
}
data[count++] = num;
}
/**
* Get value stored at given index.
* @param index Array index (only values < {@link IntVector#count} are valid)
* @return Int
*/
public int get(final int index) {
return data[index];
}
/**
* @return Number of elements stored in this instance
*/
public int size() {
return count;
}
}

View file

@ -0,0 +1,22 @@
package org.logstash.ackedqueue.io;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class IntVectorTest {
@Test
public void storesAndResizes() {
final int count = 10_000;
final IntVector vector = new IntVector();
for (int i = 0; i < count; ++i) {
vector.add(i);
}
assertThat(vector.size(), is(count));
for (int i = 0; i < count; ++i) {
assertThat(i, is(vector.get(i)));
}
}
}