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:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user