-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encoder: Maybe speed up stream row buffer
I thought that ArrayBuffer in Scala is smart enough to NOT allocate things if it still has existing capacity, but somehow in the profiler I'm reading a lot of allocations there. Let's try a custom implementation of the row buffer – as simple as possible. If this works, I will finish the docstrings. If not, I will revert it.
- Loading branch information
1 parent
c632da4
commit 4421bb1
Showing
4 changed files
with
66 additions
and
25 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
core/src/main/java/eu/ostrzyciel/jelly/core/FastBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package eu.ostrzyciel.jelly.core; | ||
|
||
class FastBuffer<T> { | ||
private static final int CAPACITY_INCREASE = 16; | ||
|
||
private T[] buffer; | ||
private int size; | ||
private int capacity; | ||
|
||
public FastBuffer(int capacity) { | ||
this.capacity = capacity; | ||
this.size = 0; | ||
this.buffer = (T[]) new Object[capacity]; | ||
} | ||
|
||
public FastBuffer<T> append(T element) { | ||
if (size == capacity) { | ||
capacity += CAPACITY_INCREASE; | ||
T[] newBuffer = (T[]) new Object[capacity]; | ||
System.arraycopy(buffer, 0, newBuffer, 0, size); | ||
buffer = newBuffer; | ||
} | ||
buffer[size++] = element; | ||
return this; | ||
} | ||
|
||
public T get(int index) { | ||
return buffer[index]; | ||
} | ||
|
||
public FastBuffer<T> clear() { | ||
size = 0; | ||
return this; | ||
} | ||
|
||
public T[] getBufferCopy() { | ||
T[] copy = (T[]) new Object[size]; | ||
System.arraycopy(buffer, 0, copy, 0, size); | ||
return copy; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters