From f44e3603f6af49295e7e7d1a8ec281405b024052 Mon Sep 17 00:00:00 2001 From: Kamil Krzywanski Date: Mon, 6 Oct 2025 22:55:06 +0200 Subject: [PATCH] Optimize PropertyMatch: use precompiled regex for namePattern Replaced String.matches(..) with a precompiled Pattern to avoid repeated regex compilation and improve startup performance. Issue: https://github.com/spring-projects/spring-data-commons/issues/3375 Signed-off-by: Kamil Krzywanski --- .../mapping/context/AbstractMappingContext.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index 6623a9eda7..dc5282d063 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -29,6 +29,7 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.function.Predicate; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.commons.logging.Log; @@ -93,6 +94,7 @@ * @author Mark Paluch * @author Mikael Klamra * @author Christoph Strobl + * @author Kamil KrzywaƄski */ public abstract class AbstractMappingContext, P extends PersistentProperty

> implements MappingContext, ApplicationEventPublisherAware, ApplicationContextAware, BeanFactoryAware, @@ -819,8 +821,8 @@ public boolean matches(Property property) { */ static class PropertyMatch { - private final @Nullable String namePattern; - private final @Nullable String typeName; + private final @Nullable Pattern namePatternRegex; + private final @Nullable String typeName; /** * Creates a new {@link PropertyMatch} for the given name pattern and type name. At least one of the parameters @@ -833,8 +835,8 @@ public PropertyMatch(@Nullable String namePattern, @Nullable String typeName) { Assert.isTrue(!((namePattern == null) && (typeName == null)), "Either name pattern or type name must be given"); - this.namePattern = namePattern; - this.typeName = typeName; + this.namePatternRegex = namePattern == null ? null : Pattern.compile(namePattern); + this.typeName = typeName; } /** @@ -869,9 +871,9 @@ public boolean matches(String name, Class type) { Assert.notNull(name, "Name must not be null"); Assert.notNull(type, "Type must not be null"); - if ((namePattern != null) && !name.matches(namePattern)) { - return false; - } + if (namePatternRegex != null && !namePatternRegex.matcher(name).matches()) { + return false; + } if ((typeName != null) && !type.getName().equals(typeName)) { return false;