Commit e02ca775 by olly Committed by Oliver Woodman

Optimize release of multiple allocations.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=126799524
parent 1a3de408
...@@ -550,9 +550,8 @@ public final class DefaultTrackOutput implements TrackOutput { ...@@ -550,9 +550,8 @@ public final class DefaultTrackOutput implements TrackOutput {
private void clearSampleData() { private void clearSampleData() {
infoQueue.clearSampleData(); infoQueue.clearSampleData();
while (!dataQueue.isEmpty()) { allocator.release(dataQueue.toArray(new Allocation[dataQueue.size()]));
allocator.release(dataQueue.remove()); dataQueue.clear();
}
allocator.trim(); allocator.trim();
totalBytesDropped = 0; totalBytesDropped = 0;
totalBytesWritten = 0; totalBytesWritten = 0;
......
...@@ -38,6 +38,13 @@ public interface Allocator { ...@@ -38,6 +38,13 @@ public interface Allocator {
void release(Allocation allocation); void release(Allocation allocation);
/** /**
* Return an array of {@link Allocation}s.
*
* @param allocations The array of {@link Allocation}s being returned.
*/
void release(Allocation[] allocations);
/**
* Hints to the {@link Allocator} that it should make a best effort to release any memory that it * Hints to the {@link Allocator} that it should make a best effort to release any memory that it
* has allocated beyond the target buffer size. * has allocated beyond the target buffer size.
*/ */
......
...@@ -29,6 +29,7 @@ public final class DefaultAllocator implements Allocator { ...@@ -29,6 +29,7 @@ public final class DefaultAllocator implements Allocator {
private final int individualAllocationSize; private final int individualAllocationSize;
private final byte[] initialAllocationBlock; private final byte[] initialAllocationBlock;
private final Allocation[] singleAllocationReleaseHolder;
private int targetBufferSize; private int targetBufferSize;
private int allocatedCount; private int allocatedCount;
...@@ -67,6 +68,7 @@ public final class DefaultAllocator implements Allocator { ...@@ -67,6 +68,7 @@ public final class DefaultAllocator implements Allocator {
} else { } else {
initialAllocationBlock = null; initialAllocationBlock = null;
} }
singleAllocationReleaseHolder = new Allocation[1];
} }
public synchronized void setTargetBufferSize(int targetBufferSize) { public synchronized void setTargetBufferSize(int targetBufferSize) {
...@@ -92,14 +94,23 @@ public final class DefaultAllocator implements Allocator { ...@@ -92,14 +94,23 @@ public final class DefaultAllocator implements Allocator {
@Override @Override
public synchronized void release(Allocation allocation) { public synchronized void release(Allocation allocation) {
singleAllocationReleaseHolder[0] = allocation;
release(singleAllocationReleaseHolder);
}
@Override
public synchronized void release(Allocation[] allocations) {
if (availableCount + allocations.length >= availableAllocations.length) {
availableAllocations = Arrays.copyOf(availableAllocations,
Math.max(availableAllocations.length * 2, availableCount + allocations.length));
}
for (Allocation allocation : allocations) {
// Weak sanity check that the allocation probably originated from this pool. // Weak sanity check that the allocation probably originated from this pool.
Assertions.checkArgument(allocation.data == initialAllocationBlock Assertions.checkArgument(allocation.data == initialAllocationBlock
|| allocation.data.length == individualAllocationSize); || allocation.data.length == individualAllocationSize);
allocatedCount--;
if (availableCount == availableAllocations.length) {
availableAllocations = Arrays.copyOf(availableAllocations, availableAllocations.length * 2);
}
availableAllocations[availableCount++] = allocation; availableAllocations[availableCount++] = allocation;
}
allocatedCount -= allocations.length;
// Wake up threads waiting for the allocated size to drop. // Wake up threads waiting for the allocated size to drop.
notifyAll(); notifyAll();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment