From 859d074764989a09911eeb3cba474ffcf1b26fac Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sat, 22 Mar 2025 19:43:47 -0700 Subject: [PATCH] Replace streams on hot paths with for loops Replace a few usages of stream with simple for loops. Although this doesn't seem to make much difference to performance, it does help when profiling applications since it reduces the stack depth. --- .../boot/context/properties/bind/Binder.java | 14 +++++++------- .../boot/env/OriginTrackedYamlLoader.java | 9 ++++++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java index 3844dae084..b1b495d388 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java @@ -25,7 +25,6 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; @@ -490,12 +489,13 @@ public class Binder { } private Object fromDataObjectBinders(BindMethod bindMethod, Function operation) { - return this.dataObjectBinders.get(bindMethod) - .stream() - .map(operation) - .filter(Objects::nonNull) - .findFirst() - .orElse(null); + for (DataObjectBinder dataObjectBinder : this.dataObjectBinders.get(bindMethod)) { + Object bound = operation.apply(dataObjectBinder); + if (bound != null) { + return bound; + } + } + return null; } private boolean isUnbindableBean(ConfigurationPropertyName name, Bindable target, Context context) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedYamlLoader.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedYamlLoader.java index 9082c5034f..cc47c4f880 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedYamlLoader.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedYamlLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -120,8 +120,11 @@ class OriginTrackedYamlLoader extends YamlProcessor { } private void replaceMappingNodeKeys(MappingNode node) { - List newValue = new ArrayList<>(); - node.getValue().stream().map(KeyScalarNode::get).forEach(newValue::add); + List value = node.getValue(); + List newValue = new ArrayList<>(value.size()); + for (NodeTuple tuple : value) { + newValue.add(KeyScalarNode.get(tuple)); + } node.setValue(newValue); }