From 3f2f2626bce57a7430a5243f2e71a59fe6c3be05 Mon Sep 17 00:00:00 2001 From: jiandy666 Date: Tue, 12 Sep 2023 17:44:32 +0800 Subject: [PATCH] Update ContextRefresher.java Optimizing ContextRefresher#extract to reduce array copy behaviors --- .../cloud/context/refresh/ContextRefresher.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java index 981493929..45f90637f 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java @@ -163,11 +163,12 @@ private boolean equal(Object one, Object two) { private Map extract(MutablePropertySources propertySources) { Map result = new HashMap<>(); - List> sources = new ArrayList<>(); + List> sources = new ArrayList<>(propertySources.size()); for (PropertySource source : propertySources) { - sources.add(0, source); + sources.add(source); } - for (PropertySource source : sources) { + for (int i = sources.size() - 1; i >= 0; i--) { + PropertySource source = sources.get(i); if (!this.standardSources.contains(source.getName())) { extract(source, result); } @@ -180,10 +181,10 @@ private void extract(PropertySource parent, Map result) { try { List> sources = new ArrayList<>(); for (PropertySource source : ((CompositePropertySource) parent).getPropertySources()) { - sources.add(0, source); + sources.add(source); } - for (PropertySource source : sources) { - extract(source, result); + for (int i = sources.size() - 1; i >= 0; i--) { + extract(sources.get(i), result); } } catch (Exception e) {