From e02ca775f3aa68acd59e780e1b1274d2bc8524ed Mon Sep 17 00:00:00 2001 From: olly Date: Thu, 7 Jul 2016 06:25:07 -0700 Subject: [PATCH] Optimize release of multiple allocations. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=126799524 --- .../extractor/DefaultTrackOutput.java | 5 ++-- .../android/exoplayer/upstream/Allocator.java | 7 ++++++ .../exoplayer/upstream/DefaultAllocator.java | 25 +++++++++++++------ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/library/src/main/java/com/google/android/exoplayer/extractor/DefaultTrackOutput.java b/library/src/main/java/com/google/android/exoplayer/extractor/DefaultTrackOutput.java index 67e4cd8c9f..2466dfbe9b 100644 --- a/library/src/main/java/com/google/android/exoplayer/extractor/DefaultTrackOutput.java +++ b/library/src/main/java/com/google/android/exoplayer/extractor/DefaultTrackOutput.java @@ -550,9 +550,8 @@ public final class DefaultTrackOutput implements TrackOutput { private void clearSampleData() { infoQueue.clearSampleData(); - while (!dataQueue.isEmpty()) { - allocator.release(dataQueue.remove()); - } + allocator.release(dataQueue.toArray(new Allocation[dataQueue.size()])); + dataQueue.clear(); allocator.trim(); totalBytesDropped = 0; totalBytesWritten = 0; diff --git a/library/src/main/java/com/google/android/exoplayer/upstream/Allocator.java b/library/src/main/java/com/google/android/exoplayer/upstream/Allocator.java index 3b86b99458..9b62fa6fc1 100644 --- a/library/src/main/java/com/google/android/exoplayer/upstream/Allocator.java +++ b/library/src/main/java/com/google/android/exoplayer/upstream/Allocator.java @@ -37,6 +37,13 @@ public interface Allocator { */ 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 * has allocated beyond the target buffer size. diff --git a/library/src/main/java/com/google/android/exoplayer/upstream/DefaultAllocator.java b/library/src/main/java/com/google/android/exoplayer/upstream/DefaultAllocator.java index e989f6f6d6..632ceb9a4a 100644 --- a/library/src/main/java/com/google/android/exoplayer/upstream/DefaultAllocator.java +++ b/library/src/main/java/com/google/android/exoplayer/upstream/DefaultAllocator.java @@ -29,6 +29,7 @@ public final class DefaultAllocator implements Allocator { private final int individualAllocationSize; private final byte[] initialAllocationBlock; + private final Allocation[] singleAllocationReleaseHolder; private int targetBufferSize; private int allocatedCount; @@ -67,6 +68,7 @@ public final class DefaultAllocator implements Allocator { } else { initialAllocationBlock = null; } + singleAllocationReleaseHolder = new Allocation[1]; } public synchronized void setTargetBufferSize(int targetBufferSize) { @@ -92,14 +94,23 @@ public final class DefaultAllocator implements Allocator { @Override public synchronized void release(Allocation allocation) { - // Weak sanity check that the allocation probably originated from this pool. - Assertions.checkArgument(allocation.data == initialAllocationBlock - || allocation.data.length == individualAllocationSize); - allocatedCount--; - if (availableCount == availableAllocations.length) { - availableAllocations = Arrays.copyOf(availableAllocations, availableAllocations.length * 2); + 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)); } - availableAllocations[availableCount++] = allocation; + for (Allocation allocation : allocations) { + // Weak sanity check that the allocation probably originated from this pool. + Assertions.checkArgument(allocation.data == initialAllocationBlock + || allocation.data.length == individualAllocationSize); + availableAllocations[availableCount++] = allocation; + } + allocatedCount -= allocations.length; // Wake up threads waiting for the allocated size to drop. notifyAll(); }