Skip to content

Commit 91e39d5

Browse files
marschalljhoeller
authored andcommitted
Use ArrayList instead of LinkedList for known size
Spring JDBC unlike other modules uses LinkedList instead of ArrayList in several places. There is a large body of evidence suggesting that on contemporary hardware ArrayList is both faster and has less overhead than even in degenerate cases of empty lists [3] or unknown size. There are two places in Spring JDBC where the size of the list is known in advance and an ArrayList of the correct final size can be created [1] https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8011200 [2] http://cliffc.org/blog/2017/11/05/modern-hardware-performance-cache-lines/ [3] https://bugs.openjdk.java.net/browse/JDK-8011200 Issue: SPR-16378
1 parent c88f11f commit 91e39d5

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/SqlParameter.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.jdbc.core;
1818

19+
import java.util.ArrayList;
1920
import java.util.LinkedList;
2021
import java.util.List;
2122

@@ -184,12 +185,16 @@ public boolean isResultsParameter() {
184185
* to a List of SqlParameter objects as used in this package.
185186
*/
186187
public static List<SqlParameter> sqlTypesToAnonymousParameterList(@Nullable int... types) {
187-
List<SqlParameter> result = new LinkedList<>();
188+
List<SqlParameter> result;
188189
if (types != null) {
190+
result = new ArrayList<>(types.length);
189191
for (int type : types) {
190192
result.add(new SqlParameter(type));
191193
}
192194
}
195+
else {
196+
result = new LinkedList<>();
197+
}
193198
return result;
194199
}
195200

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Collection;
2121
import java.util.HashSet;
2222
import java.util.Iterator;
23-
import java.util.LinkedList;
2423
import java.util.List;
2524
import java.util.Map;
2625
import java.util.Set;
@@ -423,7 +422,7 @@ public static int[] buildSqlTypeArray(ParsedSql parsedSql, SqlParameterSource pa
423422
*/
424423
public static List<SqlParameter> buildSqlParameterList(ParsedSql parsedSql, SqlParameterSource paramSource) {
425424
List<String> paramNames = parsedSql.getParameterNames();
426-
List<SqlParameter> params = new LinkedList<>();
425+
List<SqlParameter> params = new ArrayList<>(paramNames.size());
427426
for (String paramName : paramNames) {
428427
params.add(new SqlParameter(
429428
paramName, paramSource.getSqlType(paramName), paramSource.getTypeName(paramName)));

0 commit comments

Comments
 (0)