This is an automated email from the ASF dual-hosted git repository.
mimaison pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git
The following commit(s) were added to refs/heads/trunk by this push:
new e0f32977ca3 MINOR: Cache immutable voterKeys in VoterSet (#22198)
e0f32977ca3 is described below
commit e0f32977ca31bdb44c6496debac0c96815e48619
Author: Paolo Patierno <[email protected]>
AuthorDate: Wed Jun 17 10:47:39 2026 +0200
MINOR: Cache immutable voterKeys in VoterSet (#22198)
The `voters` field within the `VoterSet` is final an initialized within
the constructor only and never changes. The `voterKeys()` method is
called several times and it always computes the mapping and the
collection from the `voters` field on each call creating stream overhead
and allocating a new HashSet. Unless I am missing anything it seems to
be useless. This PR changes such behaviour computing an immutable
HashSet `voterKeys` one time in the constructor.
Signed-off-by: Paolo Patierno <[email protected]>
Reviewers: Mickael Maison <[email protected]>
---
raft/src/main/java/org/apache/kafka/raft/VoterSet.java | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/raft/src/main/java/org/apache/kafka/raft/VoterSet.java
b/raft/src/main/java/org/apache/kafka/raft/VoterSet.java
index 23f968cdeeb..1c0c67e31c3 100644
--- a/raft/src/main/java/org/apache/kafka/raft/VoterSet.java
+++ b/raft/src/main/java/org/apache/kafka/raft/VoterSet.java
@@ -47,9 +47,13 @@ import java.util.stream.Stream;
*/
public final class VoterSet {
private final Map<Integer, VoterNode> voters;
+ private final Set<ReplicaKey> voterKeys;
private VoterSet(Map<Integer, VoterNode> voters) {
this.voters = voters;
+ this.voterKeys = voters.values().stream()
+ .map(VoterNode::voterKey)
+ .collect(Collectors.toUnmodifiableSet());
}
/**
@@ -144,11 +148,7 @@ public final class VoterSet {
* Returns all of the voters.
*/
public Set<ReplicaKey> voterKeys() {
- return voters
- .values()
- .stream()
- .map(VoterNode::voterKey)
- .collect(Collectors.toSet());
+ return voterKeys;
}
/**