gpu: optimize ensureCapacity()

Prevent unnecessary allocations by updating the ensureCapacity() methods to not
create intermediate buffers, just create a single new buffer with the final
size.

Co-authored-by: Jonathon Reesor <jonathon.reesor@gmail.com>
This commit is contained in:
Adam
2020-08-30 18:06:26 -04:00
parent 2a15cbde8a
commit f2498d445f
2 changed files with 20 additions and 4 deletions

View File

@@ -49,9 +49,17 @@ class GpuFloatBuffer
void ensureCapacity(int size)
{
while (buffer.remaining() < size)
int capacity = buffer.capacity();
final int position = buffer.position();
if ((capacity - position) < size)
{
FloatBuffer newB = allocateDirect(buffer.capacity() * 2);
do
{
capacity *= 2;
}
while ((capacity - position) < size);
FloatBuffer newB = allocateDirect(capacity);
buffer.flip();
newB.put(buffer);
buffer = newB;

View File

@@ -54,9 +54,17 @@ class GpuIntBuffer
void ensureCapacity(int size)
{
while (buffer.remaining() < size)
int capacity = buffer.capacity();
final int position = buffer.position();
if ((capacity - position) < size)
{
IntBuffer newB = allocateDirect(buffer.capacity() * 2);
do
{
capacity *= 2;
}
while ((capacity - position) < size);
IntBuffer newB = allocateDirect(capacity);
buffer.flip();
newB.put(buffer);
buffer = newB;