From 6ee7cdf968e4e62894ee4eb3580055e0628b2668 Mon Sep 17 00:00:00 2001 From: aquilescanta Date: Thu, 23 Dec 2021 17:40:14 +0000 Subject: [PATCH] Pre-allocate availableAllocations to prevent a re-size in release PiperOrigin-RevId: 418022431 --- .../media3/exoplayer/upstream/DefaultAllocator.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/DefaultAllocator.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/DefaultAllocator.java index fcbc485922..1426986b0f 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/DefaultAllocator.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/DefaultAllocator.java @@ -104,6 +104,12 @@ public final class DefaultAllocator implements Allocator { availableAllocations[availableCount] = null; } else { allocation = new Allocation(new byte[individualAllocationSize], 0); + if (allocatedCount > availableAllocations.length) { + // Make availableAllocations be large enough to contain all allocations made by this + // allocator so that release() does not need to grow the availableAllocations array. See + // [Internal ref: b/209801945]. + availableAllocations = Arrays.copyOf(availableAllocations, availableAllocations.length * 2); + } } return allocation; } @@ -116,12 +122,6 @@ public final class DefaultAllocator implements Allocator { @Override public synchronized void release(Allocation[] allocations) { - if (availableCount + allocations.length >= availableAllocations.length) { - availableAllocations = - Arrays.copyOf( - availableAllocations, - max(availableAllocations.length * 2, availableCount + allocations.length)); - } for (Allocation allocation : allocations) { availableAllocations[availableCount++] = allocation; }