Skip to content

Commit

Permalink
More efficient allocation of array in MessageDispatcher.cast() (https…
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Dec 4, 2024
1 parent 4f55b35 commit 21e1a9b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/org/jgroups/blocks/MessageDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import org.jgroups.util.*;

import java.io.*;
import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
Expand Down Expand Up @@ -259,11 +262,12 @@ protected <T> GroupRequest<T> cast(final Collection<Address> dests, Message msg,

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

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



/**
* Sends a unicast message and - depending on the options - returns a result
* @param msg the payload to send
Expand Down
5 changes: 5 additions & 0 deletions src/org/jgroups/util/FastArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public FastArray(T[] elements, int index) {
this.size=count();
}

public FastArray(Collection<? extends T> c) {
this(c != null? c.size() : 16);
addAll(c);
}

public int capacity() {return elements.length;}
public int index() {return index;}
public int size() {return size;}
Expand Down
2 changes: 1 addition & 1 deletion tests/perf/org/jgroups/tests/perf/UPerf.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ private class Invoker implements Runnable {
private final List<Address> dests=new ArrayList<>();
private final CountDownLatch latch;
private final AverageMinMax avg_gets=new AverageMinMax(), avg_puts=new AverageMinMax(); // in ns
private final List<Address> targets=new ArrayList<>(anycast_count);
private final List<Address> targets=new FastArray<>(anycast_count);
private volatile boolean running=true;


Expand Down

0 comments on commit 21e1a9b

Please sign in to comment.