mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 06:37:19 -04:00
PERFORMANCE: More efficiently store offsets in AbstractByteBufferPageIO
Fixes #8333
This commit is contained in:
parent
416339f3c9
commit
08b93005a1
3 changed files with 66 additions and 2 deletions
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue