Avoid resizing of fixed-size HashMap/LinkedHashMap variants

Closes gh-25349
This commit is contained in:
Juergen Hoeller
2020-08-25 19:26:18 +02:00
parent 241afeb1b7
commit ff11467a0c
58 changed files with 195 additions and 149 deletions

View File

@@ -55,6 +55,7 @@ import org.springframework.jdbc.support.KeyHolder;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.StringUtils;
@@ -1333,7 +1334,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
protected Map<String, Object> extractOutputParameters(CallableStatement cs, List<SqlParameter> parameters)
throws SQLException {
Map<String, Object> results = new LinkedHashMap<>(parameters.size());
Map<String, Object> results = CollectionUtils.newLinkedHashMap(parameters.size());
int sqlColIndex = 1;
for (SqlParameter param : parameters) {
if (param instanceof SqlOutParameter) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -18,7 +18,6 @@ package org.springframework.jdbc.core.metadata;
import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
@@ -42,6 +41,7 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
@@ -369,7 +369,7 @@ public class CallMetaDataContext {
return workParams;
}
Map<String, String> limitedInParamNamesMap = new HashMap<>(this.limitedInParameterNames.size());
Map<String, String> limitedInParamNamesMap = CollectionUtils.newHashMap(this.limitedInParameterNames.size());
for (String limitedParamName : this.limitedInParameterNames) {
limitedInParamNamesMap.put(lowerCase(provider.parameterNameToUse(limitedParamName)), limitedParamName);
}
@@ -482,8 +482,8 @@ public class CallMetaDataContext {
Map<String, String> caseInsensitiveParameterNames =
SqlParameterSourceUtils.extractCaseInsensitiveParameterNames(parameterSource);
Map<String, String> callParameterNames = new HashMap<>(this.callParameters.size());
Map<String, Object> matchedParameters = new HashMap<>(this.callParameters.size());
Map<String, String> callParameterNames = CollectionUtils.newHashMap(this.callParameters.size());
Map<String, Object> matchedParameters = CollectionUtils.newHashMap(this.callParameters.size());
for (SqlParameter parameter : this.callParameters) {
if (parameter.isInputValueProvided()) {
String parameterName = parameter.getName();
@@ -551,7 +551,7 @@ public class CallMetaDataContext {
return inParameters;
}
Map<String, String> callParameterNames = new HashMap<>(this.callParameters.size());
Map<String, String> callParameterNames = CollectionUtils.newHashMap(this.callParameters.size());
for (SqlParameter parameter : this.callParameters) {
if (parameter.isInputValueProvided()) {
String parameterName = parameter.getName();
@@ -562,7 +562,7 @@ public class CallMetaDataContext {
}
}
Map<String, Object> matchedParameters = new HashMap<>(inParameters.size());
Map<String, Object> matchedParameters = CollectionUtils.newHashMap(inParameters.size());
inParameters.forEach((parameterName, parameterValue) -> {
String parameterNameToMatch = provider.parameterNameToUse(parameterName);
String callParameterName = callParameterNames.get(lowerCase(parameterNameToMatch));
@@ -602,7 +602,7 @@ public class CallMetaDataContext {
}
public Map<String, ?> matchInParameterValuesWithCallParameters(Object[] parameterValues) {
Map<String, Object> matchedParameters = new HashMap<>(parameterValues.length);
Map<String, Object> matchedParameters = CollectionUtils.newHashMap(parameterValues.length);
int i = 0;
for (SqlParameter parameter : this.callParameters) {
if (parameter.isInputValueProvided()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -18,7 +18,6 @@ package org.springframework.jdbc.core.metadata;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@@ -36,6 +35,7 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* Class to manage context meta-data used for the configuration
@@ -319,7 +319,7 @@ public class TableMetaDataContext {
public int[] createInsertTypes() {
int[] types = new int[getTableColumns().size()];
List<TableParameterMetaData> parameters = obtainMetaDataProvider().getTableParameterMetaData();
Map<String, TableParameterMetaData> parameterMap = new LinkedHashMap<>(parameters.size());
Map<String, TableParameterMetaData> parameterMap = CollectionUtils.newLinkedHashMap(parameters.size());
for (TableParameterMetaData tpmd : parameters) {
parameterMap.put(tpmd.getParameterName().toUpperCase(), tpmd);
}

View File

@@ -19,7 +19,6 @@ package org.springframework.jdbc.datasource.lookup;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
@@ -28,6 +27,7 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* Abstract {@link javax.sql.DataSource} implementation that routes {@link #getConnection()}
@@ -119,7 +119,7 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple
if (this.targetDataSources == null) {
throw new IllegalArgumentException("Property 'targetDataSources' is required");
}
this.resolvedDataSources = new HashMap<>(this.targetDataSources.size());
this.resolvedDataSources = CollectionUtils.newHashMap(this.targetDataSources.size());
this.targetDataSources.forEach((key, value) -> {
Object lookupKey = resolveSpecifiedLookupKey(key);
DataSource dataSource = resolveSpecifiedDataSource(value);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -25,11 +25,11 @@ import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.jdbc.InvalidResultSetAccessException;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
/**
* The default implementation of Spring's {@link SqlRowSet} interface, wrapping a
@@ -97,7 +97,7 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
ResultSetMetaData rsmd = resultSet.getMetaData();
if (rsmd != null) {
int columnCount = rsmd.getColumnCount();
this.columnLabelMap = new HashMap<>(columnCount);
this.columnLabelMap = CollectionUtils.newHashMap(columnCount);
for (int i = 1; i <= columnCount; i++) {
String key = rsmd.getColumnLabel(i);
// Make sure to preserve first matching column for any given name,