Browse Source

Remove Needless Wrapping of IndexRoutingTable.shards (#83584)

The unmodifiable wrapper here is needless since `shards` is mostly an immutable
list already anyway. I kept the `List.copyOf` as a safety measure but it's a noop
in production code.
This is surprisingly helpful for the performance of some operations like the batched
shard started updates because the shards list is generally short but is iterated over
by very hot loops upstream.
Armin Braun 3 years ago
parent
commit
4893b87a6d

+ 3 - 3
server/src/main/java/org/elasticsearch/cluster/routing/IndexShardRoutingTable.java

@@ -62,7 +62,7 @@ public class IndexShardRoutingTable implements Iterable<ShardRouting> {
     IndexShardRoutingTable(ShardId shardId, List<ShardRouting> shards) {
         this.shardId = shardId;
         this.shuffler = new RotationShardShuffler(Randomness.get().nextInt());
-        this.shards = Collections.unmodifiableList(shards);
+        this.shards = List.copyOf(shards);
 
         ShardRouting primary = null;
         List<ShardRouting> replicas = new ArrayList<>();
@@ -566,7 +566,7 @@ public class IndexShardRoutingTable implements Iterable<ShardRouting> {
 
     public static class Builder {
 
-        private ShardId shardId;
+        private final ShardId shardId;
         private final List<ShardRouting> shards;
 
         public Builder(IndexShardRoutingTable indexShard) {
@@ -592,7 +592,7 @@ public class IndexShardRoutingTable implements Iterable<ShardRouting> {
         public IndexShardRoutingTable build() {
             // don't allow more than one shard copy with same id to be allocated to same node
             assert distinctNodes(shards) : "more than one shard with same id assigned to same node (shards: " + shards + ")";
-            return new IndexShardRoutingTable(shardId, List.copyOf(shards));
+            return new IndexShardRoutingTable(shardId, shards);
         }
 
         static boolean distinctNodes(List<ShardRouting> shards) {