Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Unified Diff: base/android/java/src/org/chromium/base/ObserverList.java

Issue 943303005: [Android] Avoid unnecessray ObserverList compacting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Code review Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/java/src/org/chromium/base/ObserverList.java
diff --git a/base/android/java/src/org/chromium/base/ObserverList.java b/base/android/java/src/org/chromium/base/ObserverList.java
index e812b0de4341e0ac21fed05c71b1a268abb18c0d..7a2ab984feb2351ff8c786eceead74e832d2cc18 100644
--- a/base/android/java/src/org/chromium/base/ObserverList.java
+++ b/base/android/java/src/org/chromium/base/ObserverList.java
@@ -46,6 +46,7 @@ public class ObserverList<E> implements Iterable<E> {
public final List<E> mObservers = new ArrayList<E>();
private int mIterationDepth = 0;
private int mCount = 0;
+ private boolean mNeedsCompact = false;
public ObserverList() {}
@@ -91,6 +92,7 @@ public class ObserverList<E> implements Iterable<E> {
// No one is iterating over the list.
mObservers.remove(index);
} else {
+ mNeedsCompact = true;
mObservers.set(index, null);
}
--mCount;
@@ -112,6 +114,7 @@ public class ObserverList<E> implements Iterable<E> {
}
int size = mObservers.size();
+ mNeedsCompact |= size != 0;
for (int i = 0; i < size; i++) {
mObservers.set(i, null);
}
@@ -167,7 +170,10 @@ public class ObserverList<E> implements Iterable<E> {
private void decrementIterationDepthAndCompactIfNeeded() {
mIterationDepth--;
assert mIterationDepth >= 0;
- if (mIterationDepth == 0) compact();
+ if (mIterationDepth > 0) return;
+ if (!mNeedsCompact) return;
+ mNeedsCompact = false;
+ compact();
}
/**
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698