Skip to content

Commit 21e1a9b

Browse files
committed
More efficient allocation of array in MessageDispatcher.cast() (https://issues.redhat.com/browse/JGRP-2855)
1 parent 4f55b35 commit 21e1a9b

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/org/jgroups/blocks/MessageDispatcher.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
import org.jgroups.util.*;
1212

1313
import java.io.*;
14-
import java.util.*;
14+
import java.util.Collection;
15+
import java.util.Collections;
16+
import java.util.HashSet;
17+
import java.util.List;
1518
import java.util.concurrent.CompletableFuture;
1619
import java.util.concurrent.TimeoutException;
1720
import java.util.stream.Stream;
@@ -259,11 +262,12 @@ protected <T> GroupRequest<T> cast(final Collection<Address> dests, Message msg,
259262

260263
List<Address> real_dests;
261264
// we need to clone because we don't want to modify the original
262-
if(dests != null)
263-
real_dests=dests.stream().filter(dest -> dest instanceof SiteAddress || this.members.contains(dest))
264-
.collect(ArrayList::new, (list,dest) -> {if(!list.contains(dest)) list.add(dest);}, (l,r) -> {});
265+
if(dests != null) {
266+
real_dests=new FastArray<>(dests);
267+
real_dests.removeIf(addr -> !this.members.contains(addr) && !(addr instanceof SiteAddress));
268+
}
265269
else
266-
real_dests=new ArrayList<>(members);
270+
real_dests=new FastArray<>(members);
267271

268272
// Remove the local member from the target destination set if we should not deliver our own message
269273
JChannel tmp=channel;
@@ -292,7 +296,6 @@ protected <T> GroupRequest<T> cast(final Collection<Address> dests, Message msg,
292296
}
293297

294298

295-
296299
/**
297300
* Sends a unicast message and - depending on the options - returns a result
298301
* @param msg the payload to send

src/org/jgroups/util/FastArray.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public FastArray(T[] elements, int index) {
3232
this.size=count();
3333
}
3434

35+
public FastArray(Collection<? extends T> c) {
36+
this(c != null? c.size() : 16);
37+
addAll(c);
38+
}
39+
3540
public int capacity() {return elements.length;}
3641
public int index() {return index;}
3742
public int size() {return size;}

tests/perf/org/jgroups/tests/perf/UPerf.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ private class Invoker implements Runnable {
502502
private final List<Address> dests=new ArrayList<>();
503503
private final CountDownLatch latch;
504504
private final AverageMinMax avg_gets=new AverageMinMax(), avg_puts=new AverageMinMax(); // in ns
505-
private final List<Address> targets=new ArrayList<>(anycast_count);
505+
private final List<Address> targets=new FastArray<>(anycast_count);
506506
private volatile boolean running=true;
507507

508508

0 commit comments

Comments
 (0)