Skip to content

Commit

Permalink
Initialise fields used for bundling as String directly
Browse files Browse the repository at this point in the history
Initialising the fields as Integer and then getting a String on compute time is slow. Instead we directly initialise these fields as String. Improves the time taken in bundling PlayerInfo further to less than 200ms from ~300ms.

Also modified a test to improve productive coverage.

PiperOrigin-RevId: 500003935
  • Loading branch information
rohitjoins authored and christosts committed Jan 10, 2023
1 parent bdd6818 commit d49437c
Show file tree
Hide file tree
Showing 29 changed files with 801 additions and 1,522 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import static com.google.android.exoplayer2.util.Assertions.checkState;
import static com.google.android.exoplayer2.util.Util.msToUs;
import static com.google.android.exoplayer2.util.Util.usToMs;
import static java.lang.annotation.ElementType.TYPE_USE;

import android.content.Context;
import android.net.Uri;
Expand All @@ -39,7 +38,6 @@
import android.os.Looper;
import android.util.Pair;
import android.view.ViewGroup;
import androidx.annotation.IntDef;
import androidx.annotation.MainThread;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
Expand Down Expand Up @@ -97,10 +95,6 @@
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -326,13 +320,7 @@ public int hashCode() {

// Bundleable implementation.

@Documented
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({FIELD_AD_PLAYBACK_STATES})
private @interface FieldNumber {}

private static final int FIELD_AD_PLAYBACK_STATES = 1;
private static final String FIELD_AD_PLAYBACK_STATES = Util.intToStringMaxRadix(1);

@Override
public Bundle toBundle() {
Expand All @@ -341,7 +329,7 @@ public Bundle toBundle() {
for (Map.Entry<String, AdPlaybackState> entry : adPlaybackStates.entrySet()) {
adPlaybackStatesBundle.putBundle(entry.getKey(), entry.getValue().toBundle());
}
bundle.putBundle(keyForField(FIELD_AD_PLAYBACK_STATES), adPlaybackStatesBundle);
bundle.putBundle(FIELD_AD_PLAYBACK_STATES, adPlaybackStatesBundle);
return bundle;
}

Expand All @@ -352,8 +340,7 @@ private static State fromBundle(Bundle bundle) {
@Nullable
ImmutableMap.Builder<String, AdPlaybackState> adPlaybackStateMap =
new ImmutableMap.Builder<>();
Bundle adPlaybackStateBundle =
checkNotNull(bundle.getBundle(keyForField(FIELD_AD_PLAYBACK_STATES)));
Bundle adPlaybackStateBundle = checkNotNull(bundle.getBundle(FIELD_AD_PLAYBACK_STATES));
for (String key : adPlaybackStateBundle.keySet()) {
AdPlaybackState adPlaybackState =
AdPlaybackState.CREATOR.fromBundle(
Expand All @@ -363,10 +350,6 @@ private static State fromBundle(Bundle bundle) {
}
return new State(adPlaybackStateMap.buildOrThrow());
}

private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
}

private final ImaUtil.ServerSideAdInsertionConfiguration configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.os.Bundle;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -85,37 +86,26 @@ public int hashCode() {

// Bundleable implementation.

@Documented
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({FIELD_PLAYBACK_TYPE, FIELD_MIN_VOLUME, FIELD_MAX_VOLUME})
private @interface FieldNumber {}

private static final int FIELD_PLAYBACK_TYPE = 0;
private static final int FIELD_MIN_VOLUME = 1;
private static final int FIELD_MAX_VOLUME = 2;
private static final String FIELD_PLAYBACK_TYPE = Util.intToStringMaxRadix(0);
private static final String FIELD_MIN_VOLUME = Util.intToStringMaxRadix(1);
private static final String FIELD_MAX_VOLUME = Util.intToStringMaxRadix(2);

@Override
public Bundle toBundle() {
Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_PLAYBACK_TYPE), playbackType);
bundle.putInt(keyForField(FIELD_MIN_VOLUME), minVolume);
bundle.putInt(keyForField(FIELD_MAX_VOLUME), maxVolume);
bundle.putInt(FIELD_PLAYBACK_TYPE, playbackType);
bundle.putInt(FIELD_MIN_VOLUME, minVolume);
bundle.putInt(FIELD_MAX_VOLUME, maxVolume);
return bundle;
}

/** Object that can restore {@link DeviceInfo} from a {@link Bundle}. */
public static final Creator<DeviceInfo> CREATOR =
bundle -> {
int playbackType =
bundle.getInt(
keyForField(FIELD_PLAYBACK_TYPE), /* defaultValue= */ PLAYBACK_TYPE_LOCAL);
int minVolume = bundle.getInt(keyForField(FIELD_MIN_VOLUME), /* defaultValue= */ 0);
int maxVolume = bundle.getInt(keyForField(FIELD_MAX_VOLUME), /* defaultValue= */ 0);
bundle.getInt(FIELD_PLAYBACK_TYPE, /* defaultValue= */ PLAYBACK_TYPE_LOCAL);
int minVolume = bundle.getInt(FIELD_MIN_VOLUME, /* defaultValue= */ 0);
int maxVolume = bundle.getInt(FIELD_MAX_VOLUME, /* defaultValue= */ 0);
return new DeviceInfo(playbackType, minVolume, maxVolume);
};

private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX);
}
}
Loading

0 comments on commit d49437c

Please sign in to comment.