From 44b0891d16aea49807c51bc5a37add6ee1a3ed6c Mon Sep 17 00:00:00 2001 From: JorringHsiao <39626963+JorringHsiao@users.noreply.github.com> Date: Fri, 24 Nov 2023 15:48:54 +0800 Subject: [PATCH] [ISSUE #11414]Optimize ServiceInfo.validate() to reduce memory usage (#11415) --- .../alibaba/nacos/api/naming/pojo/ServiceInfo.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java index 4d8b13da19d..f10a7513aa0 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java @@ -188,18 +188,14 @@ public boolean validate() { return false; } - List validHosts = new ArrayList<>(); + boolean existValidHosts = false; for (Instance host : hosts) { - if (!host.isHealthy()) { - continue; - } - - if (host.getWeight() > 0) { - validHosts.add(host); + if (host.isHealthy() && host.getWeight() > 0) { + existValidHosts = true; + break; } } - //No valid hosts, return false. - return !validHosts.isEmpty(); + return existValidHosts; } @JsonIgnore