From 9f93220a12cb9aec55e83cc6b8b6c24a2068c092 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Wed, 29 Nov 2023 17:27:22 +0100 Subject: [PATCH] Fix performance of mappedColumnNames.contains If the number of columns is very large, this is quite a performance hit, due to O(n) vs. O(1) runtime on List vs. Set. --- .../executor/resultset/DefaultResultSetHandler.java | 4 ++-- .../ibatis/executor/resultset/ResultSetWrapper.java | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java b/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java index c1b67f1cec3..68ceb4d6bee 100644 --- a/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java +++ b/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java @@ -480,7 +480,7 @@ private boolean shouldApplyAutomaticMappings(ResultMap resultMap, boolean isNest private boolean applyPropertyMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException { - final List mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix); + final Set mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix); boolean foundValues = false; final List propertyMappings = resultMap.getPropertyResultMappings(); for (ResultMapping propertyMapping : propertyMappings) { @@ -1174,7 +1174,7 @@ private void createRowKeyForMappedProperties(ResultMap resultMap, ResultSetWrapp if (resultMapping.isSimple()) { final String column = prependPrefix(resultMapping.getColumn(), columnPrefix); final TypeHandler th = resultMapping.getTypeHandler(); - List mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix); + Set mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix); // Issue #114 if (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH))) { final Object value = th.getResult(rsw.getResultSet(), column); diff --git a/src/main/java/org/apache/ibatis/executor/resultset/ResultSetWrapper.java b/src/main/java/org/apache/ibatis/executor/resultset/ResultSetWrapper.java index ae826baa44a..40ac1a5f247 100644 --- a/src/main/java/org/apache/ibatis/executor/resultset/ResultSetWrapper.java +++ b/src/main/java/org/apache/ibatis/executor/resultset/ResultSetWrapper.java @@ -47,7 +47,7 @@ public class ResultSetWrapper { private final List classNames = new ArrayList<>(); private final List jdbcTypes = new ArrayList<>(); private final Map, TypeHandler>> typeHandlerMap = new HashMap<>(); - private final Map> mappedColumnNamesMap = new HashMap<>(); + private final Map> mappedColumnNamesMap = new HashMap<>(); private final Map> unMappedColumnNamesMap = new HashMap<>(); public ResultSetWrapper(ResultSet rs, Configuration configuration) throws SQLException { @@ -144,7 +144,7 @@ private Class resolveClass(String className) { } private void loadMappedAndUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException { - List mappedColumnNames = new ArrayList<>(); + Set mappedColumnNames = new HashSet<>(); List unmappedColumnNames = new ArrayList<>(); final String upperColumnPrefix = columnPrefix == null ? null : columnPrefix.toUpperCase(Locale.ENGLISH); final Set mappedColumns = prependPrefixes(resultMap.getMappedColumns(), upperColumnPrefix); @@ -160,8 +160,8 @@ private void loadMappedAndUnmappedColumnNames(ResultMap resultMap, String column unMappedColumnNamesMap.put(getMapKey(resultMap, columnPrefix), unmappedColumnNames); } - public List getMappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException { - List mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix)); + public Set getMappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException { + Set mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix)); if (mappedColumnNames == null) { loadMappedAndUnmappedColumnNames(resultMap, columnPrefix); mappedColumnNames = mappedColumnNamesMap.get(getMapKey(resultMap, columnPrefix));