summaryrefslogtreecommitdiffstats
path: root/core/java/android/util
diff options
context:
space:
mode:
authorAndreas Gampe <agampe@google.com>2015-03-04 17:14:10 -0800
committerAndreas Gampe <agampe@google.com>2015-03-04 17:14:10 -0800
commitf9345e93db82adf8eaa2afc731933462d7876b13 (patch)
tree4a68fc43b2f851f52537a07fd94a4acf0a8c3ed8 /core/java/android/util
parenta891d08dad829c9aacd29bd2c3b36debe3fc1cc3 (diff)
downloadframeworks_base-f9345e93db82adf8eaa2afc731933462d7876b13.zip
frameworks_base-f9345e93db82adf8eaa2afc731933462d7876b13.tar.gz
frameworks_base-f9345e93db82adf8eaa2afc731933462d7876b13.tar.bz2
Frameworks/base: Add removeAll for ArraySet
Add a simple ArraySet.removeAll(ArraySet) method. This avoids two allocations, a MapCollections helper and an Iterator object, over the removeAll(Collection) code. KeySetManagerService heavily calls removeAll during boot (about 9K times in AOSP). This reduces GC stress and optimizes the removal (about half the time the removed collection has only one element). The removal method in KeySetManagerService is also done under a lock, so that it gates parallelization efforts in PackageManagerService. Bug: 19498314 Change-Id: Ib0e483adfd09831cd66ab19a820ebf6544a2b66f
Diffstat (limited to 'core/java/android/util')
-rw-r--r--core/java/android/util/ArraySet.java20
1 files changed, 20 insertions, 0 deletions
diff --git a/core/java/android/util/ArraySet.java b/core/java/android/util/ArraySet.java
index 423e48b..3214b22 100644
--- a/core/java/android/util/ArraySet.java
+++ b/core/java/android/util/ArraySet.java
@@ -468,6 +468,26 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
}
/**
+ * Perform a {@link #remove(Object)} of all values in <var>array</var>
+ * @param array The array whose contents are to be removed.
+ */
+ public boolean removeAll(ArraySet<? extends E> array) {
+ // TODO: If array is sufficiently large, a marking approach might be beneficial. In a first
+ // pass, use the property that the sets are sorted by hash to make this linear passes
+ // (except for hash collisions, which means worst case still n*m), then do one
+ // collection pass into a new array. This avoids binary searches and excessive memcpy.
+ final int N = array.mSize;
+
+ // Note: ArraySet does not make thread-safety guarantees. So instead of OR-ing together all
+ // the single results, compare size before and after.
+ final int originalSize = mSize;
+ for (int i = 0; i < N; i++) {
+ remove(array.valueAt(i));
+ }
+ return originalSize != mSize;
+ }
+
+ /**
* Return the number of items in this array map.
*/
@Override