From 5e60beff91319ec11946d27558e65483f143b0cb Mon Sep 17 00:00:00 2001 From: tswstarplanet Date: Sun, 21 Oct 2018 23:04:30 +0800 Subject: [PATCH] Optimize the doSelect method of RandomLoadBalance to reduce the times of invoke of the getWeight method of the AbstractLoadBalance (#2597) --- .../dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java index 90bc60cf91c..eea0ca319db 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java @@ -34,13 +34,13 @@ public class RandomLoadBalance extends AbstractLoadBalance { @Override protected Invoker doSelect(List> invokers, URL url, Invocation invocation) { int length = invokers.size(); // Number of invokers - int totalWeight = 0; // The sum of weights boolean sameWeight = true; // Every invoker has the same weight? - for (int i = 0; i < length; i++) { + int firstWeight = getWeight(invokers.get(0), invocation); + int totalWeight = firstWeight; // The sum of weights + for (int i = 1; i < length; i++) { int weight = getWeight(invokers.get(i), invocation); totalWeight += weight; // Sum - if (sameWeight && i > 0 - && weight != getWeight(invokers.get(i - 1), invocation)) { + if (sameWeight && weight != firstWeight) { sameWeight = false; } }