Optimize synchronized in PartitionedDispatcher (#8640)

Even if the `PartitionedDispatcher.populatedPartitions()`
is fast, in-memory, non-blocking operation, its active call from the `dispatch()`
on every message sent to the channel may pin the virtual thread.

* Optimize the `populatedPartitions()` for double `if`
where we will step into a `synchronized` block only for first several concurrent messages

**Cherry-pick to `6.1.x`**
This commit is contained in:
Artem Bilan
2023-06-08 11:23:09 -04:00
committed by GitHub
parent 8b8a7a4c16
commit 33f0b8ec4a

View File

@@ -151,10 +151,16 @@ public class PartitionedDispatcher extends AbstractDispatcher {
return partitionDispatcher.dispatch(message);
}
private synchronized void populatedPartitions() {
private void populatedPartitions() {
if (this.partitions.isEmpty()) {
for (int i = 0; i < this.partitionCount; i++) {
this.partitions.put(i, newPartition());
synchronized (this.partitions) {
if (this.partitions.isEmpty()) {
Map<Integer, UnicastingDispatcher> partitionsToUse = new HashMap<>();
for (int i = 0; i < this.partitionCount; i++) {
partitionsToUse.put(i, newPartition());
}
this.partitions.putAll(partitionsToUse);
}
}
}
}