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.
This commit is contained in:
Phillip Webb
2025-03-22 19:43:47 -07:00
parent 83725f8080
commit 859d074764
2 changed files with 13 additions and 10 deletions

View File

@@ -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<DataObjectBinder, Object> 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) {

View File

@@ -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<NodeTuple> newValue = new ArrayList<>();
node.getValue().stream().map(KeyScalarNode::get).forEach(newValue::add);
List<NodeTuple> value = node.getValue();
List<NodeTuple> newValue = new ArrayList<>(value.size());
for (NodeTuple tuple : value) {
newValue.add(KeyScalarNode.get(tuple));
}
node.setValue(newValue);
}