Skip to content

Commit f52bc25

Browse files
committed
Performance improvements in ReactiveWrappers and ConvertingPropertyAccessor.
We now cache whether types are reactive wrappers and bypass the ConversionService for assignable primitive values. Closes #2546
1 parent 2bf30b6 commit f52bc25

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/main/java/org/springframework/data/mapping/model/ConvertingPropertyAccessor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import org.springframework.data.mapping.PersistentPropertyPath;
2222
import org.springframework.lang.Nullable;
2323
import org.springframework.util.Assert;
24+
import org.springframework.util.ClassUtils;
2425

2526
/**
2627
* {@link PersistentPropertyAccessor} that potentially converts the value handed to
@@ -115,7 +116,7 @@ private <S> S convertIfNecessary(@Nullable Object source, Class<S> type) {
115116

116117
return (S) (source == null //
117118
? null //
118-
: type.isAssignableFrom(source.getClass()) //
119+
: ClassUtils.resolvePrimitiveIfNecessary(type).isAssignableFrom(source.getClass()) //
119120
? source //
120121
: conversionService.convert(source, type));
121122
}

src/main/java/org/springframework/data/repository/util/ReactiveWrappers.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.util.Arrays;
2222
import java.util.Collection;
2323
import java.util.Collections;
24+
import java.util.Map;
2425
import java.util.Optional;
2526

2627
import org.springframework.core.ReactiveAdapter;
@@ -30,6 +31,7 @@
3031
import org.springframework.data.util.ReflectionUtils;
3132
import org.springframework.util.Assert;
3233
import org.springframework.util.ClassUtils;
34+
import org.springframework.util.ConcurrentReferenceHashMap;
3335

3436
/**
3537
* Utility class to expose details about reactive wrapper types. This class exposes whether a reactive wrapper is
@@ -89,6 +91,11 @@ public abstract class ReactiveWrappers {
8991
private static final boolean MUTINY_PRESENT = ClassUtils.isPresent("io.smallrye.mutiny.Multi",
9092
ReactiveWrappers.class.getClassLoader());
9193

94+
private static final Map<Class<?>, Boolean> IS_REACTIVE_TYPE = new ConcurrentReferenceHashMap<>();
95+
96+
private static final boolean IS_REACTIVE_AVAILABLE = Arrays.stream(ReactiveLibrary.values())
97+
.anyMatch(ReactiveWrappers::isAvailable);
98+
9299
private ReactiveWrappers() {}
93100

94101
/**
@@ -120,7 +127,7 @@ public enum ReactiveLibrary {
120127
* @return {@literal true} if reactive support is available.
121128
*/
122129
public static boolean isAvailable() {
123-
return Arrays.stream(ReactiveLibrary.values()).anyMatch(ReactiveWrappers::isAvailable);
130+
return IS_REACTIVE_AVAILABLE;
124131
}
125132

126133
/**
@@ -158,7 +165,7 @@ public static boolean isAvailable(ReactiveLibrary reactiveLibrary) {
158165
* @return {@literal true} if the {@code type} is a supported reactive wrapper type.
159166
*/
160167
public static boolean supports(Class<?> type) {
161-
return isAvailable() && isWrapper(ProxyUtils.getUserClass(type));
168+
return isAvailable() && IS_REACTIVE_TYPE.computeIfAbsent(type, key -> isWrapper(ProxyUtils.getUserClass(key)));
162169
}
163170

164171
/**

0 commit comments

Comments
 (0)