From a9abb40fbd198c7fd754c4161ba4d4d0c6c466b2 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 17 May 2013 13:58:14 +0200 Subject: [PATCH] DATACMNS-332 - Performance improvements in conversion subsystem hotspots. Added caching to DefaultTypeMapper, SimpleTypeInformationMapper, BasicPersistentEntity and PreferredConstructor as these seem to be performance hotspots on the reading side of object conversion. --- .../data/convert/DefaultTypeMapper.java | 34 +++++++-- .../convert/SimpleTypeInformationMapper.java | 19 ++++- .../data/mapping/PreferredConstructor.java | 15 +++- .../mapping/model/BasicPersistentEntity.java | 14 ++-- .../convert/DefaultTypeMapperUnitTests.java | 75 +++++++++++++++++++ .../SimpleTypeInformationMapperUnitTests.java | 3 +- 6 files changed, 140 insertions(+), 20 deletions(-) create mode 100644 src/test/java/org/springframework/data/convert/DefaultTypeMapperUnitTests.java diff --git a/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java b/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java index 87eca65eb..6e0aa4fb1 100644 --- a/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java +++ b/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java @@ -19,6 +19,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.context.MappingContext; @@ -38,6 +40,7 @@ public class DefaultTypeMapper implements TypeMapper { private final TypeAliasAccessor accessor; private final List mappers; + private final Map> typeCache; /** * Creates a new {@link DefaultTypeMapper} using the given {@link TypeAliasAccessor}. It will use a @@ -84,6 +87,7 @@ public class DefaultTypeMapper implements TypeMapper { this.mappers = Collections.unmodifiableList(mappers); this.accessor = accessor; + this.typeCache = new ConcurrentHashMap>(); } /* @@ -93,21 +97,37 @@ public class DefaultTypeMapper implements TypeMapper { public TypeInformation readType(S source) { Assert.notNull(source); - Object alias = accessor.readAliasFrom(source); - if (alias == null) { - return null; + Object alias = accessor.readAliasFrom(source); + return alias == null ? null : getFromCacheOrCreate(alias); + } + + /** + * Tries to lookup a {@link TypeInformation} for the given alias from the cache and return it if found. If none is + * found it'll consult the {@link TypeInformationMapper}s and cache the value found. + * + * @param alias + * @return + */ + private TypeInformation getFromCacheOrCreate(Object alias) { + + TypeInformation typeInformation = typeCache.get(alias); + + if (typeInformation != null) { + return typeInformation; } for (TypeInformationMapper mapper : mappers) { - TypeInformation type = mapper.resolveTypeFrom(alias); - if (type != null) { - return type; + typeInformation = mapper.resolveTypeFrom(alias); + + if (typeInformation != null) { + typeCache.put(alias, typeInformation); + return typeInformation; } } - return null; + return typeInformation; } /* diff --git a/src/main/java/org/springframework/data/convert/SimpleTypeInformationMapper.java b/src/main/java/org/springframework/data/convert/SimpleTypeInformationMapper.java index 7ee24a622..6beb8270e 100644 --- a/src/main/java/org/springframework/data/convert/SimpleTypeInformationMapper.java +++ b/src/main/java/org/springframework/data/convert/SimpleTypeInformationMapper.java @@ -15,6 +15,9 @@ */ package org.springframework.data.convert; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.TypeInformation; import org.springframework.util.ClassUtils; @@ -30,6 +33,7 @@ import org.springframework.util.StringUtils; public class SimpleTypeInformationMapper implements TypeInformationMapper { public static final SimpleTypeInformationMapper INSTANCE = new SimpleTypeInformationMapper(); + private static final Map> cache = new ConcurrentHashMap>(); /** * Returns the {@link TypeInformation} that shall be used when the given {@link String} value is found as type hint. @@ -52,11 +56,23 @@ public class SimpleTypeInformationMapper implements TypeInformationMapper { return null; } + TypeInformation information = cache.get(value); + + if (information != null) { + return information; + } + try { - return ClassTypeInformation.from(ClassUtils.forName(value, null)); + information = ClassTypeInformation.from(ClassUtils.forName(value, null)); } catch (ClassNotFoundException e) { return null; } + + if (information != null) { + cache.put(value, information); + } + + return information; } /** @@ -67,7 +83,6 @@ public class SimpleTypeInformationMapper implements TypeInformationMapper { * @return the String representation to be stored or {@literal null} if no type information shall be stored. */ public String createAliasFor(TypeInformation type) { - return type.getType().getName(); } } diff --git a/src/main/java/org/springframework/data/mapping/PreferredConstructor.java b/src/main/java/org/springframework/data/mapping/PreferredConstructor.java index c41fbd19a..83de3c7ff 100644 --- a/src/main/java/org/springframework/data/mapping/PreferredConstructor.java +++ b/src/main/java/org/springframework/data/mapping/PreferredConstructor.java @@ -1,11 +1,11 @@ /* - * Copyright 2011-2013 by the original author(s). + * Copyright 2011-2013 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. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -21,6 +21,8 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.util.Arrays; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.annotation.PersistenceConstructor; @@ -39,6 +41,7 @@ public class PreferredConstructor> { private final Constructor constructor; private final List> parameters; + private final Map, Boolean> isPropertyParameterCache = new ConcurrentHashMap, Boolean>(); /** * Creates a new {@link PreferredConstructor} from the given {@link Constructor} and {@link Parameter}s. @@ -114,12 +117,20 @@ public class PreferredConstructor> { Assert.notNull(property); + Boolean cached = isPropertyParameterCache.get(property); + + if (cached != null) { + return cached; + } + for (Parameter parameter : parameters) { if (parameter.maps(property)) { + isPropertyParameterCache.put(property, true); return true; } } + isPropertyParameterCache.put(property, false); return false; } diff --git a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java index 5825ee98a..83f13d81a 100644 --- a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java @@ -17,7 +17,9 @@ package org.springframework.data.mapping.model; import java.io.Serializable; import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import java.util.TreeSet; @@ -46,6 +48,8 @@ public class BasicPersistentEntity> implement private final Set

properties; private final Set> associations; + private final Map propertyCache = new HashMap(); + private P idProperty; private P versionProperty; @@ -157,6 +161,7 @@ public class BasicPersistentEntity> implement Assert.notNull(property); properties.add(property); + propertyCache.put(property.getName(), property); if (property.isIdProperty()) { @@ -193,14 +198,7 @@ public class BasicPersistentEntity> implement * @see org.springframework.data.mapping.PersistentEntity#getPersistentProperty(java.lang.String) */ public P getPersistentProperty(String name) { - - for (P property : properties) { - if (property.getName().equals(name)) { - return property; - } - } - - return null; + return propertyCache.get(name); } /* diff --git a/src/test/java/org/springframework/data/convert/DefaultTypeMapperUnitTests.java b/src/test/java/org/springframework/data/convert/DefaultTypeMapperUnitTests.java new file mode 100644 index 000000000..e8a27a0f5 --- /dev/null +++ b/src/test/java/org/springframework/data/convert/DefaultTypeMapperUnitTests.java @@ -0,0 +1,75 @@ +/* + * Copyright 2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.convert; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.util.ClassTypeInformation; +import org.springframework.data.util.TypeInformation; + +/** + * Unit tests for {@link DefaultTypeMapper}. + * + * @author Oliver Gierke + */ +@RunWith(MockitoJUnitRunner.class) +public class DefaultTypeMapperUnitTests { + + static final String STRING = String.class.getName(); + + @Mock + TypeAliasAccessor> accessor; + + @Mock + TypeInformationMapper mapper; + + TypeMapper> typeMapper; + Map source; + + @Before + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void setUp() { + + this.typeMapper = new DefaultTypeMapper>(accessor, Arrays.asList(mapper)); + this.source = Collections.singletonMap("key", STRING); + + when(accessor.readAliasFrom(source)).thenReturn(STRING); + when(mapper.resolveTypeFrom(STRING)).thenReturn((TypeInformation) ClassTypeInformation.from(String.class)); + } + + @Test + @SuppressWarnings("rawtypes") + public void cachesResolvedTypeInformation() { + + TypeInformation information = typeMapper.readType(source); + assertThat(information, is((TypeInformation) ClassTypeInformation.from(String.class))); + verify(mapper, times(1)).resolveTypeFrom(STRING); + + typeMapper.readType(source); + verify(mapper, times(1)).resolveTypeFrom(STRING); + } +} diff --git a/src/test/java/org/springframework/data/convert/SimpleTypeInformationMapperUnitTests.java b/src/test/java/org/springframework/data/convert/SimpleTypeInformationMapperUnitTests.java index b401b5794..5454a4489 100644 --- a/src/test/java/org/springframework/data/convert/SimpleTypeInformationMapperUnitTests.java +++ b/src/test/java/org/springframework/data/convert/SimpleTypeInformationMapperUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2013 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. @@ -23,6 +23,7 @@ import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.TypeInformation; /** + * Unit tests for {@link SimpleTypeInformationMapper}. * * @author Oliver Gierke */