Improve DefaultExtractorInput's peek buffer sizing.

- Don't resize the peek buffer to be twice as large as a
  large amount!
- Trim the peek buffer, to allow large peek buffer allocations
  to be reclaimed.

Issue: #2553

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=150184291
This commit is contained in:
olly 2017-03-15 06:24:12 -07:00 committed by Oliver Woodman
parent 70926057c5
commit d077e23daa

View File

@ -18,6 +18,7 @@ package com.google.android.exoplayer2.extractor;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@ -27,6 +28,8 @@ import java.util.Arrays;
*/ */
public final class DefaultExtractorInput implements ExtractorInput { public final class DefaultExtractorInput implements ExtractorInput {
private static final int PEEK_MIN_FREE_SPACE_AFTER_RESIZE = 64 * 1024;
private static final int PEEK_MAX_FREE_SPACE = 512 * 1024;
private static final byte[] SCRATCH_SPACE = new byte[4096]; private static final byte[] SCRATCH_SPACE = new byte[4096];
private final DataSource dataSource; private final DataSource dataSource;
@ -46,7 +49,7 @@ public final class DefaultExtractorInput implements ExtractorInput {
this.dataSource = dataSource; this.dataSource = dataSource;
this.position = position; this.position = position;
this.streamLength = length; this.streamLength = length;
peekBuffer = new byte[8 * 1024]; peekBuffer = new byte[PEEK_MIN_FREE_SPACE_AFTER_RESIZE];
} }
@Override @Override
@ -176,7 +179,9 @@ public final class DefaultExtractorInput implements ExtractorInput {
private void ensureSpaceForPeek(int length) { private void ensureSpaceForPeek(int length) {
int requiredLength = peekBufferPosition + length; int requiredLength = peekBufferPosition + length;
if (requiredLength > peekBuffer.length) { if (requiredLength > peekBuffer.length) {
peekBuffer = Arrays.copyOf(peekBuffer, Math.max(peekBuffer.length * 2, requiredLength)); int newPeekCapacity = Util.constrainValue(peekBuffer.length * 2,
requiredLength + PEEK_MIN_FREE_SPACE_AFTER_RESIZE, requiredLength + PEEK_MAX_FREE_SPACE);
peekBuffer = Arrays.copyOf(peekBuffer, newPeekCapacity);
} }
} }
@ -218,7 +223,12 @@ public final class DefaultExtractorInput implements ExtractorInput {
private void updatePeekBuffer(int bytesConsumed) { private void updatePeekBuffer(int bytesConsumed) {
peekBufferLength -= bytesConsumed; peekBufferLength -= bytesConsumed;
peekBufferPosition = 0; peekBufferPosition = 0;
System.arraycopy(peekBuffer, bytesConsumed, peekBuffer, 0, peekBufferLength); byte[] newPeekBuffer = peekBuffer;
if (peekBufferLength < peekBuffer.length - PEEK_MAX_FREE_SPACE) {
newPeekBuffer = new byte[peekBufferLength + PEEK_MIN_FREE_SPACE_AFTER_RESIZE];
}
System.arraycopy(peekBuffer, bytesConsumed, newPeekBuffer, 0, peekBufferLength);
peekBuffer = newPeekBuffer;
} }
/** /**