Bug 1510664 - Replace `HashMap` with `SparseArray` r=agi

This PR replaces `HashMap` with `SparseArray` from the Android API to
improve memory efficiency and performance.

Differential Revision: https://phabricator.services.mozilla.com/D121484
This commit is contained in:
cschanaj 2021-08-18 14:40:26 +00:00
Родитель 1da5651ba2
Коммит f44ca48708
4 изменённых файлов: 26 добавлений и 24 удалений

Просмотреть файл

@ -9,9 +9,10 @@ import android.graphics.SurfaceTexture;
import android.os.Build;
import androidx.annotation.RequiresApi;
import android.util.Log;
import android.util.LongSparseArray;
import android.util.SparseArray;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.HashMap;
import java.util.LinkedList;
import org.mozilla.gecko.annotation.WrapForJNI;
@ -21,11 +22,11 @@ import org.mozilla.gecko.mozglue.JNIObject;
private static final String LOGTAG = "GeckoSurfaceTexture";
private static final int MAX_SURFACE_TEXTURES = 200;
private static volatile int sNextHandle = 1;
private static final HashMap<Integer, GeckoSurfaceTexture> sSurfaceTextures = new HashMap<Integer, GeckoSurfaceTexture>();
private static final SparseArray<GeckoSurfaceTexture> sSurfaceTextures = new SparseArray<GeckoSurfaceTexture>();
private static HashMap<Long, LinkedList<GeckoSurfaceTexture>> sUnusedTextures =
new HashMap<Long, LinkedList<GeckoSurfaceTexture>>();
private static LongSparseArray<LinkedList<GeckoSurfaceTexture>> sUnusedTextures =
new LongSparseArray<LinkedList<GeckoSurfaceTexture>>();
private int mHandle;
private boolean mIsSingleBuffer;
@ -204,7 +205,8 @@ import org.mozilla.gecko.mozglue.JNIObject;
public static void destroyUnused(final long context) {
final LinkedList<GeckoSurfaceTexture> list;
synchronized (sUnusedTextures) {
list = sUnusedTextures.remove(context);
list = sUnusedTextures.get(context);
sUnusedTextures.delete(context);
}
if (list == null) {
@ -259,7 +261,7 @@ import org.mozilla.gecko.mozglue.JNIObject;
gst = new GeckoSurfaceTexture(resolvedHandle);
}
if (sSurfaceTextures.containsKey(resolvedHandle)) {
if (sSurfaceTextures.indexOfKey(resolvedHandle) >= 0) {
gst.release();
throw new IllegalArgumentException("Already have a GeckoSurfaceTexture with that handle");
}

Просмотреть файл

@ -12,6 +12,7 @@ import android.os.Build;
import android.os.DeadObjectException;
import android.os.RemoteException;
import android.util.Log;
import android.util.SparseArray;
import androidx.annotation.RequiresApi;
@ -21,8 +22,6 @@ import org.mozilla.gecko.mozglue.JNIObject;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -42,8 +41,8 @@ public final class CodecProxy {
private Queue<Sample> mSurfaceOutputs = new ConcurrentLinkedQueue<>();
private boolean mFlushed = true;
private Map<Integer, SampleBuffer> mInputBuffers = new HashMap<>();
private Map<Integer, SampleBuffer> mOutputBuffers = new HashMap<>();
private SparseArray<SampleBuffer> mInputBuffers = new SparseArray<>();
private SparseArray<SampleBuffer> mOutputBuffers = new SparseArray<>();
public interface Callbacks {
void onInputStatus(long timestamp, boolean processed);
@ -320,12 +319,12 @@ public final class CodecProxy {
}
private void resetBuffers() {
for (final SampleBuffer b : mInputBuffers.values()) {
b.dispose();
for (int i = 0; i < mInputBuffers.size(); ++i) {
mInputBuffers.valueAt(i).dispose();
}
mInputBuffers.clear();
for (final SampleBuffer b : mOutputBuffers.values()) {
b.dispose();
for (int i = 0; i < mOutputBuffers.size(); ++i) {
mOutputBuffers.valueAt(i).dispose();
}
mOutputBuffers.clear();
}

Просмотреть файл

@ -5,14 +5,13 @@
package org.mozilla.gecko.media;
import android.media.MediaCodec;
import android.util.SparseArray;
import org.mozilla.gecko.mozglue.SharedMemory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
final class SamplePool {
private static final class Impl {
@ -22,7 +21,7 @@ final class SamplePool {
private final boolean mBufferless;
private int mNextBufferId = Sample.NO_BUFFER + 1;
private Map<Integer, SampleBuffer> mBuffers = new HashMap<>();
private SparseArray<SampleBuffer> mBuffers = new SparseArray<>();
private Impl(final String name, final boolean bufferless) {
mName = name;
@ -57,7 +56,7 @@ final class SamplePool {
s.bufferId = id;
return s;
} catch (final NoSuchMethodException | IOException e) {
mBuffers.remove(id);
mBuffers.delete(id);
throw new UnsupportedOperationException(e);
}
}
@ -84,15 +83,16 @@ final class SamplePool {
}
mRecycledSamples.clear();
for (final SampleBuffer b: mBuffers.values()) {
b.dispose();
for (int i = 0; i < mBuffers.size(); ++i) {
mBuffers.valueAt(i).dispose();
}
mBuffers.clear();
}
private void disposeSample(final Sample sample) {
if (sample.bufferId != Sample.NO_BUFFER) {
mBuffers.remove(sample.bufferId).dispose();
mBuffers.get(sample.bufferId).dispose();
mBuffers.delete(sample.bufferId);
}
sample.dispose();
}

Просмотреть файл

@ -12,6 +12,7 @@ import androidx.annotation.UiThread;
import android.os.Build;
import android.util.Log;
import android.util.SparseArray;
import org.json.JSONException;
import org.mozilla.gecko.EventDispatcher;
@ -43,7 +44,7 @@ public class WebExtensionController {
private final MultiMap<String, Message> mPendingBrowsingData;
private final MultiMap<String, Message> mPendingDownload;
private final HashMap<Integer, WebExtension.Download> mDownloads;
private final SparseArray<WebExtension.Download> mDownloads;
private static class Message {
final GeckoBundle bundle;
@ -694,7 +695,7 @@ public class WebExtensionController {
mPendingBrowsingData = new MultiMap<>();
mPendingDownload = new MultiMap<>();
mExtensions.setObserver(mInternals);
mDownloads = new HashMap<>();
mDownloads = new SparseArray<>();
}
/* package */ WebExtension registerWebExtension(final WebExtension webExtension) {
@ -1288,7 +1289,7 @@ public class WebExtensionController {
@Nullable
@UiThread
public WebExtension.Download createDownload(final int id) {
if (mDownloads.containsKey(id)) {
if (mDownloads.indexOfKey(id) >= 0) {
throw new IllegalArgumentException("Download with this id already exists");
} else {
final WebExtension.Download download = new WebExtension.Download(id);