Skip to content

Commit 859d074

Browse files
committed
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.
1 parent 83725f8 commit 859d074

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

Diff for: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.HashSet;
2626
import java.util.List;
2727
import java.util.Map;
28-
import java.util.Objects;
2928
import java.util.Set;
3029
import java.util.function.Consumer;
3130
import java.util.function.Function;
@@ -490,12 +489,13 @@ private Object bindDataObject(ConfigurationPropertyName name, Bindable<?> target
490489
}
491490

492491
private Object fromDataObjectBinders(BindMethod bindMethod, Function<DataObjectBinder, Object> operation) {
493-
return this.dataObjectBinders.get(bindMethod)
494-
.stream()
495-
.map(operation)
496-
.filter(Objects::nonNull)
497-
.findFirst()
498-
.orElse(null);
492+
for (DataObjectBinder dataObjectBinder : this.dataObjectBinders.get(bindMethod)) {
493+
Object bound = operation.apply(dataObjectBinder);
494+
if (bound != null) {
495+
return bound;
496+
}
497+
}
498+
return null;
499499
}
500500

501501
private boolean isUnbindableBean(ConfigurationPropertyName name, Bindable<?> target, Context context) {

Diff for: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedYamlLoader.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -120,8 +120,11 @@ protected Object constructObject(Node node) {
120120
}
121121

122122
private void replaceMappingNodeKeys(MappingNode node) {
123-
List<NodeTuple> newValue = new ArrayList<>();
124-
node.getValue().stream().map(KeyScalarNode::get).forEach(newValue::add);
123+
List<NodeTuple> value = node.getValue();
124+
List<NodeTuple> newValue = new ArrayList<>(value.size());
125+
for (NodeTuple tuple : value) {
126+
newValue.add(KeyScalarNode.get(tuple));
127+
}
125128
node.setValue(newValue);
126129
}
127130

0 commit comments

Comments
 (0)