Skip to content

Commit 8b600de

Browse files
committed
Optimize LruMultimap remove
1 parent 2424797 commit 8b600de

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/main/java/io/github/nstdio/http/ext/LruMultimap.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
package io.github.nstdio.http.ext;
1818

19-
import java.util.*;
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.Iterator;
22+
import java.util.LinkedHashMap;
23+
import java.util.List;
24+
import java.util.Map;
2025
import java.util.function.Consumer;
2126
import java.util.function.ToIntFunction;
2227

@@ -60,7 +65,7 @@ V getSingle(K k, ToIntFunction<List<V>> idxFn) {
6065
}
6166

6267
List<V> putSingle(K key, V value, ToIntFunction<List<V>> idxFn) {
63-
List<V> vs = m.computeIfAbsent(key, k -> new ArrayList<>());
68+
List<V> vs = m.computeIfAbsent(key, k -> new ArrayList<>(1));
6469

6570
int i;
6671
if (!vs.isEmpty() && (i = idxFn.applyAsInt(vs)) != -1) {
@@ -107,10 +112,12 @@ private V removeEldest(List<V> vs) {
107112
void evictAll(K k) {
108113
List<V> old = m.remove(k);
109114
if (old != null) {
110-
while (!old.isEmpty()) {
111-
notifyEvicted(removeEldest(old));
112-
size--;
115+
int len = old.size();
116+
for (int i = len - 1; i >= 0; i--) {
117+
notifyEvicted(old.get(i));
113118
}
119+
120+
this.size -= len;
114121
}
115122
}
116123

0 commit comments

Comments
 (0)