You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by ta...@apache.org on 2026/07/24 23:32:18 UTC
(bval) branch master updated (05ef736 -> 7917434)
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/bval.git
from 05ef736 Merge pull request #249 from apache/dependabot/maven/version.tomcat-11.0.24
new 5493f86 Defer ExpressionFactory.newInstance(); −25 classes at bootstrap, EL init avoided unless ${} used
new a19c9bb Cache CDI availability; no repeated NoClassDefFoundError in SE
new e6b3a06 Cache extractor lookups; ~39% faster container validation, −38% allocations
new ec65309 reduce allocations
new 559981a Add single-copy child(Consumer<PathImpl>, value), add no-copy pathReference() for read-only callers, and compare the shared path directly in equals()
new b39a149 Add container-element children via the single-copy child(NodeImpl, value) overload instead of getPath() + child(path)
new 0be5faa Build the property child via child(p -> p.addProperty(...)) to copy the path once instead of twice
new d182f8f Cascade beans via child(PathImpl::addBean, ...) (one copy) and key the constraint-dedup map on pathReference() (no copy)
new 7917434 Visit unwrapped frames via a consumer instead of a Stream per constraint (avoids Stream.of(this) on the common no-unwrapping path)
The 9 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../src/main/java/org/apache/bval/el/ELFacade.java | 9 +++-
.../java/org/apache/bval/jsr/GraphContext.java | 23 ++++++++-
.../org/apache/bval/jsr/ParticipantFactory.java | 28 +++++++++--
.../org/apache/bval/jsr/descriptor/PropertyD.java | 5 +-
.../org/apache/bval/jsr/job/ValidationJob.java | 54 ++++++++++++++--------
.../java/org/apache/bval/jsr/util/NodeImpl.java | 48 +++++++++++++------
.../bval/jsr/valueextraction/ExtractValues.java | 10 ++--
.../bval/jsr/valueextraction/ValueExtractors.java | 23 ++++++++-
8 files changed, 150 insertions(+), 50 deletions(-)
(bval) 08/09: Cascade beans via child(PathImpl::addBean, ...) (one copy) and key the constraint-dedup map on pathReference() (no copy)
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bval.git
commit d182f8f7609ca7d5ec6fa99ce8d9b840f604f187
Author: tandraschko <ta...@apache.org>
AuthorDate: Sat Jul 25 01:02:37 2026 +0200
Cascade beans via child(PathImpl::addBean, ...) (one copy) and key the constraint-dedup map on pathReference() (no copy)
---
bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java b/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java
index 12747bb..4da1f30 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java
@@ -144,8 +144,10 @@ public abstract class ValidationJob<T> {
// Use the same path ordering/equality as before (ConcurrentSkipListMap): comparator-based, not Path#equals.
final Map<Path, Set<Object>> pathMap = completedValidations.computeIfAbsent(constraint,
k -> new TreeMap<>(PathImpl.PATH_COMPARATOR));
+ // Read-only use as a comparator-based map key for the duration of this validation; no copy needed.
final Set<Object> objectSet =
- pathMap.computeIfAbsent(context.getPath(), p -> Collections.newSetFromMap(new IdentityHashMap<>()));
+ pathMap.computeIfAbsent(context.pathReference(),
+ p -> Collections.newSetFromMap(new IdentityHashMap<>()));
if (!objectSet.add(context.getValue())) {
return true;
}
@@ -249,7 +251,7 @@ public abstract class ValidationJob<T> {
BeanFrame(Frame<?> parent, GraphContext context) {
super(parent, getBeanDescriptor(context.getValue()),
- context.child(context.getPath().addBean(), context.getValue()));
+ context.child(PathImpl::addBean, context.getValue()));
this.realContext = context;
}
(bval) 07/09: Build the property child via child(p -> p.addProperty(...)) to copy the path once instead of twice
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bval.git
commit 0be5faafb2dd091744f8a59136621b362024ed87
Author: tandraschko <ta...@apache.org>
AuthorDate: Sat Jul 25 01:02:28 2026 +0200
Build the property child via child(p -> p.addProperty(...)) to copy the path once instead of twice
---
bval-jsr/src/main/java/org/apache/bval/jsr/descriptor/PropertyD.java | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/descriptor/PropertyD.java b/bval-jsr/src/main/java/org/apache/bval/jsr/descriptor/PropertyD.java
index 21d7bfb..3a89c6c 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/descriptor/PropertyD.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/descriptor/PropertyD.java
@@ -27,7 +27,6 @@ import jakarta.validation.metadata.PropertyDescriptor;
import org.apache.bval.jsr.GraphContext;
import org.apache.bval.jsr.util.Methods;
-import org.apache.bval.jsr.util.PathImpl;
import org.apache.bval.util.reflection.Reflection;
import org.apache.commons.weaver.privilizer.Privilizing;
import org.apache.commons.weaver.privilizer.Privilizing.CallTo;
@@ -90,9 +89,7 @@ public abstract class PropertyD<E extends AnnotatedElement> extends CascadableCo
}
try {
final Object value = getValue(context.getValue());
- final PathImpl p = context.getPath();
- p.addProperty(getPropertyName());
- return Stream.of(context.child(p, value));
+ return Stream.of(context.child(p -> p.addProperty(getPropertyName()), value));
} catch (Exception e) {
throw e instanceof ValidationException ? (ValidationException) e : new ValidationException(e);
}
(bval) 02/09: Cache CDI availability; no repeated NoClassDefFoundError in SE
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bval.git
commit a19c9bbb4b280c77d01d3393e88a20859ea97220
Author: tandraschko <ta...@apache.org>
AuthorDate: Fri Jul 24 18:04:40 2026 +0200
Cache CDI availability; no repeated NoClassDefFoundError in SE
---
.../org/apache/bval/jsr/ParticipantFactory.java | 28 ++++++++++++++++++----
1 file changed, 23 insertions(+), 5 deletions(-)
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/ParticipantFactory.java b/bval-jsr/src/main/java/org/apache/bval/jsr/ParticipantFactory.java
index 3a5e475..1cbdb3d 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/ParticipantFactory.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/ParticipantFactory.java
@@ -54,6 +54,11 @@ class ParticipantFactory implements Closeable {
private final Collection<BValExtension.Releasable<?>> releasables = new CopyOnWriteArrayList<>();
private final List<ClassLoader> loaders;
+ // Cache whether CDI is available to avoid repeatedly triggering the (slow) NoClassDefFoundError /
+ // IllegalStateException thrown by CDI.current() on every instantiation when running outside a CDI
+ // container. Mirrors the guard already used by DefaultConstraintValidatorFactory.
+ private volatile Boolean useCdi;
+
ParticipantFactory(ClassLoader... loaders) {
super();
this.loaders = Arrays.asList(loaders).stream().filter(Objects::nonNull).collect(ToUnmodifiable.list());
@@ -111,11 +116,24 @@ class ParticipantFactory implements Closeable {
@Privileged
private <T> T newInstance(final Class<T> cls) {
- try {
- final BValExtension.Releasable<T> releasable = BValExtension.inject(cls);
- releasables.add(releasable);
- return releasable.getInstance();
- } catch (Exception | NoClassDefFoundError e) {
+ if (useCdi == null) {
+ synchronized (this) {
+ if (useCdi == null) {
+ try {
+ useCdi = BValExtension.getBeanManager() != null;
+ } catch (final NoClassDefFoundError | Exception error) {
+ useCdi = Boolean.FALSE;
+ }
+ }
+ }
+ }
+ if (useCdi.booleanValue()) {
+ try {
+ final BValExtension.Releasable<T> releasable = BValExtension.inject(cls);
+ releasables.add(releasable);
+ return releasable.getInstance();
+ } catch (Exception | NoClassDefFoundError e) {
+ }
}
try {
return cls.getConstructor().newInstance();
(bval) 03/09: Cache extractor lookups; ~39% faster container validation, −38% allocations
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bval.git
commit e6b3a06917f76b1a149ca1ead690453639678b15
Author: tandraschko <ta...@apache.org>
AuthorDate: Fri Jul 24 18:04:56 2026 +0200
Cache extractor lookups; ~39% faster container validation, −38% allocations
---
.../bval/jsr/valueextraction/ValueExtractors.java | 23 ++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/valueextraction/ValueExtractors.java b/bval-jsr/src/main/java/org/apache/bval/jsr/valueextraction/ValueExtractors.java
index 1066cc0..d6522e4 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/valueextraction/ValueExtractors.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/valueextraction/ValueExtractors.java
@@ -222,6 +222,12 @@ public class ValueExtractors {
private final ValueExtractors parent;
private final Map<ContainerElementKey, ValueExtractor<?>> valueExtractors = new ConcurrentHashMap<>();
private final Map<ContainerElementKey, ValueExtractor<?>> searchCache = new ConcurrentHashMap<>();
+ // Memoize findUnwrappingInfo() per container class: it is queried once per constraint per validated
+ // value at runtime and otherwise re-walks the whole extractor set (map + streams) only to usually
+ // return an empty result. Separate maps avoid allocating a composite key on each lookup. Invalidated
+ // together with searchCache whenever an extractor is added.
+ private final Map<Class<?>, Optional<UnwrappingInfo>> unwrapDefaultCache = new ConcurrentHashMap<>();
+ private final Map<Class<?>, Optional<UnwrappingInfo>> unwrapForcedCache = new ConcurrentHashMap<>();
private final OnDuplicateContainerElementKey onDuplicateContainerElementKey;
public ValueExtractors() {
@@ -271,6 +277,8 @@ public class ValueExtractors {
m.put(key, extractor);
}
searchCache.clear();
+ unwrapDefaultCache.clear();
+ unwrapForcedCache.clear();
}
public Map<ContainerElementKey, ValueExtractor<?>> getValueExtractors() {
@@ -285,8 +293,10 @@ public class ValueExtractors {
return cacheHit;
}
final Map<ContainerElementKey, ValueExtractor<?>> allValueExtractors = getValueExtractors();
- if (allValueExtractors.containsKey(key)) {
- return allValueExtractors.get(key);
+ final ValueExtractor<?> directHit = allValueExtractors.get(key);
+ if (directHit != null) {
+ searchCache.put(key, directHit);
+ return directHit;
}
final Map<ValueExtractor<?>, ContainerElementKey> candidates = Stream
.concat(Stream.of(key), key.getAssignableKeys().stream()).filter(allValueExtractors::containsKey).collect(
@@ -307,6 +317,15 @@ public class ValueExtractors {
if (valueUnwrapping == ValidateUnwrappedValue.SKIP) {
return Optional.empty();
}
+ final Map<Class<?>, Optional<UnwrappingInfo>> cache =
+ valueUnwrapping == ValidateUnwrappedValue.UNWRAP ? unwrapForcedCache : unwrapDefaultCache;
+ // computeIfAbsent does not cache a thrown exception, so the UNWRAP "not found" error below is
+ // re-evaluated on each call for that (rare, misconfigured) case rather than being memoized.
+ return cache.computeIfAbsent(containerClass, k -> computeUnwrappingInfo(k, valueUnwrapping));
+ }
+
+ private Optional<UnwrappingInfo> computeUnwrappingInfo(Class<?> containerClass,
+ ValidateUnwrappedValue valueUnwrapping) {
final Map<ContainerElementKey, ValueExtractor<?>> allValueExtractors = getValueExtractors();
final Set<UnwrappingInfo> unwrapping = allValueExtractors.entrySet().stream()
(bval) 06/09: Add container-element children via the single-copy child(NodeImpl, value) overload instead of getPath() + child(path)
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bval.git
commit b39a149e1274fcd1f055942bc3c74d9c6c8ed62a
Author: tandraschko <ta...@apache.org>
AuthorDate: Sat Jul 25 01:02:15 2026 +0200
Add container-element children via the single-copy child(NodeImpl, value) overload instead of getPath() + child(path)
---
.../org/apache/bval/jsr/valueextraction/ExtractValues.java | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/valueextraction/ExtractValues.java b/bval-jsr/src/main/java/org/apache/bval/jsr/valueextraction/ExtractValues.java
index 6949981..137e68f 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/valueextraction/ExtractValues.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/valueextraction/ExtractValues.java
@@ -28,7 +28,6 @@ import jakarta.validation.valueextraction.ValueExtractor;
import org.apache.bval.jsr.GraphContext;
import org.apache.bval.jsr.metadata.ContainerElementKey;
import org.apache.bval.jsr.util.NodeImpl;
-import org.apache.bval.jsr.util.PathImpl;
import org.apache.bval.util.Exceptions;
import org.apache.bval.util.Lazy;
import org.apache.bval.util.Validate;
@@ -76,10 +75,11 @@ public final class ExtractValues {
}
private void addChild(NodeImpl node, Object value) {
- final PathImpl path = context.getPath();
- path.addNode(
- node.inContainer(containerElementKey.getContainerClass(), containerElementKey.getTypeArgumentIndex()));
- result.get().add(context.child(path, value));
+ // child(NodeImpl, ...) copies the path exactly once; building the path here and passing it to
+ // child(Path, ...) would copy it a second time.
+ result.get().add(context.child(
+ node.inContainer(containerElementKey.getContainerClass(), containerElementKey.getTypeArgumentIndex()),
+ value));
}
}
(bval) 09/09: Visit unwrapped frames via a consumer instead of a Stream per constraint (avoids Stream.of(this) on the common no-unwrapping path)
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bval.git
commit 7917434c4ba33ef6c57f12166cc05735dd784e7f
Author: tandraschko <ta...@apache.org>
AuthorDate: Sat Jul 25 01:13:57 2026 +0200
Visit unwrapped frames via a consumer instead of a Stream per constraint (avoids Stream.of(this) on the common no-unwrapping path)
---
.../org/apache/bval/jsr/job/ValidationJob.java | 25 ++++++++++++----------
1 file changed, 14 insertions(+), 11 deletions(-)
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java b/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java
index 4da1f30..e57cc22 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java
@@ -119,24 +119,27 @@ public abstract class ValidationJob<T> {
abstract Object getBean();
void validateDescriptorConstraints(GroupStrategy groups, Consumer<ConstraintViolation<T>> sink) {
- constraintsFor(descriptor, groups)
- .forEach(c -> unwrap(c.getValueUnwrapping()).forEach(f -> f.validate(c, sink)));
+ constraintsFor(descriptor, groups).forEach(c -> validateUnwrapped(c, sink));
}
- private Stream<Frame<D>> unwrap(ValidateUnwrappedValue valueUnwrapping) {
+ // Visit each (possibly unwrapped) frame for this constraint without allocating a Stream per constraint;
+ // the common case is no unwrapping, i.e. a direct validate(this).
+ private void validateUnwrapped(ConstraintD<?> constraint, Consumer<ConstraintViolation<T>> sink) {
+ final ValidateUnwrappedValue valueUnwrapping = constraint.getValueUnwrapping();
if (valueUnwrapping != ValidateUnwrappedValue.SKIP && context.getValue() != null) {
- final Optional<ValueExtractors.UnwrappingInfo> valueExtractorAndAssociatedContainerElementKey =
+ final Optional<ValueExtractors.UnwrappingInfo> unwrappingInfo =
validatorContext.getValueExtractors().findUnwrappingInfo(context.getValue().getClass(),
valueUnwrapping);
-
- if (valueExtractorAndAssociatedContainerElementKey.isPresent()) {
- return ExtractValues
- .extract(context, valueExtractorAndAssociatedContainerElementKey.get().containerElementKey,
- valueExtractorAndAssociatedContainerElementKey.get().valueExtractor)
- .stream().map(child -> new UnwrappedElementConstraintValidationPseudoFrame<>(this, child));
+ if (unwrappingInfo.isPresent()) {
+ for (final GraphContext child : ExtractValues.extract(context,
+ unwrappingInfo.get().containerElementKey, unwrappingInfo.get().valueExtractor)) {
+ final Frame<D> frame = new UnwrappedElementConstraintValidationPseudoFrame<>(this, child);
+ frame.validate(constraint, sink);
+ }
+ return;
}
}
- return Stream.of(this);
+ validate(constraint, sink);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
(bval) 01/09: Defer ExpressionFactory.newInstance(); −25 classes at bootstrap, EL init avoided unless ${} used
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bval.git
commit 5493f867f954b82aa78bdde62eb84f7ef537defe
Author: tandraschko <ta...@apache.org>
AuthorDate: Fri Jul 24 18:04:31 2026 +0200
Defer ExpressionFactory.newInstance(); −25 classes at bootstrap, EL init avoided unless ${} used
---
bval-jsr/src/main/java/org/apache/bval/el/ELFacade.java | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/bval-jsr/src/main/java/org/apache/bval/el/ELFacade.java b/bval-jsr/src/main/java/org/apache/bval/el/ELFacade.java
index f9e3ee3..6e53509 100644
--- a/bval-jsr/src/main/java/org/apache/bval/el/ELFacade.java
+++ b/bval-jsr/src/main/java/org/apache/bval/el/ELFacade.java
@@ -35,6 +35,7 @@ import jakarta.el.ValueExpression;
import jakarta.el.VariableMapper;
import org.apache.bval.jsr.util.LookBehindRegexHolder;
+import org.apache.bval.util.Lazy;
// ELProcessor or JavaEE 7 would be perfect too but this impl can be used in javaee 6
public final class ELFacade implements MessageEvaluator {
@@ -56,7 +57,10 @@ public final class ELFacade implements MessageEvaluator {
private static final ELResolver RESOLVER = initResolver();
- private final ExpressionFactory expressionFactory = ExpressionFactory.newInstance();
+ // Lazily initialized: ExpressionFactory.newInstance() performs a relatively expensive
+ // ServiceLoader lookup, and most validations never evaluate an EL expression (default
+ // constraint messages use no ${...} syntax). Deferring it keeps bootstrap cheap.
+ private final Lazy<ExpressionFactory> expressionFactory = new Lazy<>(ExpressionFactory::newInstance);
@Override
public String interpolate(final String message, final Map<String, Object> annotationParameters,
@@ -64,6 +68,7 @@ public final class ELFacade implements MessageEvaluator {
// BVAL-170: simple pre-check to improve performance
if (message.contains("${")) {
try {
+ final ExpressionFactory expressionFactory = this.expressionFactory.get();
final BValELContext context = new BValELContext();
final VariableMapper variables = context.getVariableMapper();
annotationParameters.forEach(
@@ -126,7 +131,7 @@ public final class ELFacade implements MessageEvaluator {
@Override
public ValueExpression resolveVariable(final String variable) {
if ("formatter".equals(variable)) {
- return expressionFactory.createValueExpression(new BValFormatter(), Object.class);
+ return expressionFactory.get().createValueExpression(new BValFormatter(), Object.class);
}
return variables.get(variable);
}
(bval) 05/09: Add single-copy child(Consumer, value), add no-copy pathReference() for read-only callers, and compare the shared path directly in equals()
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bval.git
commit 559981ab99ab335bea9cab62e9b83e2e0bd9e408
Author: tandraschko <ta...@apache.org>
AuthorDate: Sat Jul 25 01:02:04 2026 +0200
Add single-copy child(Consumer<PathImpl>, value), add no-copy pathReference() for read-only callers, and compare the shared path directly in equals()
---
.../java/org/apache/bval/jsr/GraphContext.java | 23 +++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/GraphContext.java b/bval-jsr/src/main/java/org/apache/bval/jsr/GraphContext.java
index 380d78b..3eed746 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/GraphContext.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/GraphContext.java
@@ -22,6 +22,7 @@ import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Map;
import java.util.Objects;
+import java.util.function.Consumer;
import jakarta.validation.Path;
import jakarta.validation.ValidationException;
@@ -59,6 +60,16 @@ public class GraphContext {
return PathImpl.copy(path);
}
+ /**
+ * Return this context's own path instance <b>without copying</b>. A {@link GraphContext}'s path is never
+ * mutated after construction (all mutating operations go through {@link #getPath()} or {@link #child}, which
+ * copy first), so the returned instance is safe to read, compare, or use as a map key. Callers <b>must not</b>
+ * mutate it. Use this instead of {@link #getPath()} on hot read-only paths to avoid a defensive copy.
+ */
+ public PathImpl pathReference() {
+ return path;
+ }
+
public Object getValue() {
return value;
}
@@ -77,6 +88,16 @@ public class GraphContext {
return new GraphContext(validatorContext, impl == p ? PathImpl.copy(impl) : impl, value, this);
}
+ /**
+ * Create a child context whose path is this context's path with {@code pathMutation} applied. This copies
+ * the path exactly once, avoiding the copy-then-copy-again pattern of {@code child(getPath().mutate(), value)}.
+ */
+ public GraphContext child(Consumer<PathImpl> pathMutation, Object value) {
+ final PathImpl p = PathImpl.copy(path);
+ pathMutation.accept(p);
+ return new GraphContext(validatorContext, p, value, this);
+ }
+
public boolean isRoot() {
return parent == null;
}
@@ -110,7 +131,7 @@ public class GraphContext {
return false;
}
final GraphContext other = (GraphContext) obj;
- return other.validatorContext == validatorContext && other.value == value && other.getPath().equals(path);
+ return other.validatorContext == validatorContext && other.value == value && other.path.equals(path);
}
@Override
(bval) 04/09: reduce allocations
Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bval.git
commit ec653094b133d2008a03ff6730e2b1636a2eeaab
Author: tandraschko <ta...@apache.org>
AuthorDate: Fri Jul 24 18:24:26 2026 +0200
reduce allocations
---
.../org/apache/bval/jsr/job/ValidationJob.java | 23 ++++++++---
.../java/org/apache/bval/jsr/util/NodeImpl.java | 48 ++++++++++++++++------
2 files changed, 53 insertions(+), 18 deletions(-)
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java b/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java
index 670297f..12747bb 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/job/ValidationJob.java
@@ -559,12 +559,25 @@ public abstract class ValidationJob<T> {
protected static final TypeVariable<?> ITERABLE_ELEMENT = Iterable.class.getTypeParameters()[0];
private static Stream<ConstraintD<?>> constraintsFor(ElementD<?, ?> descriptor, GroupStrategy groups) {
+ // Resolve the target groups once per call rather than once per constraint: GroupStrategy.getGroups()
+ // may allocate (a singleton for a plain Group, a fully streamed-and-collected set for a Composite),
+ // and it is invariant across the constraints being filtered.
+ final Set<Group> targetGroups = groups.getGroups();
return descriptor.getConstraintDescriptors().stream().<ConstraintD<?>> map(ConstraintD.class::cast)
- .filter(c -> {
- final Set<Class<?>> constraintGroups = c.getGroups();
- return groups.getGroups().stream().map(Group::getGroup).anyMatch(g -> constraintGroups.contains(g)
- || constraintGroups.contains(Default.class) && c.getDeclaringClass().equals(g));
- });
+ .filter(c -> matchesGroups(c, targetGroups));
+ }
+
+ private static boolean matchesGroups(ConstraintD<?> constraint, Set<Group> targetGroups) {
+ final Set<Class<?>> constraintGroups = constraint.getGroups();
+ final boolean impliesDefault = constraintGroups.contains(Default.class);
+ for (final Group target : targetGroups) {
+ final Class<?> g = target.getGroup();
+ if (constraintGroups.contains(g)
+ || impliesDefault && constraint.getDeclaringClass().equals(g)) {
+ return true;
+ }
+ }
+ return false;
}
protected final ApacheFactoryContext validatorContext;
diff --git a/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeImpl.java b/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeImpl.java
index f0b182b..7437d79 100644
--- a/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeImpl.java
+++ b/bval-jsr/src/main/java/org/apache/bval/jsr/util/NodeImpl.java
@@ -78,6 +78,34 @@ public abstract class NodeImpl implements Path.Node, Serializable {
return 0;
}).thenComparing((Function<Object, String>) Objects::toString));
+ // Per-kind node comparators are stateless and constant; hoisted to static finals so they are not
+ // re-allocated on every compareSpecificNodeInfo/compareIterability call (both are hot during path
+ // comparison, e.g. violation de-duplication).
+ private static final Comparator<Node> KEY_NODE_COMPARATOR = comparing(Node::getKey, KEY_COMPARATOR);
+
+ private static final Comparator<Node> BEAN_NODE_COMPARATOR =
+ comparing(to(BeanNode.class), comparing(BeanNode::getContainerClass, CLASS_COMPARATOR)
+ .thenComparing(BeanNode::getTypeArgumentIndex, nullsFirst(naturalOrder())));
+
+ private static final Comparator<Node> PROPERTY_NODE_COMPARATOR =
+ comparing(to(PropertyNode.class), comparing(PropertyNode::getContainerClass, CLASS_COMPARATOR)
+ .thenComparing(PropertyNode::getTypeArgumentIndex, nullsFirst(naturalOrder())));
+
+ private static final Comparator<Node> CONTAINER_ELEMENT_NODE_COMPARATOR =
+ comparing(to(ContainerElementNode.class), comparing(ContainerElementNode::getContainerClass, CLASS_COMPARATOR)
+ .thenComparing(ContainerElementNode::getTypeArgumentIndex, nullsFirst(naturalOrder())));
+
+ private static final Comparator<Node> CONSTRUCTOR_NODE_COMPARATOR =
+ comparing(to(ConstructorNode.class).andThen(ConstructorNode::getParameterTypes),
+ Comparators.comparingIterables(CLASS_COMPARATOR));
+
+ private static final Comparator<Node> METHOD_NODE_COMPARATOR =
+ comparing(to(MethodNode.class).andThen(MethodNode::getParameterTypes),
+ Comparators.comparingIterables(CLASS_COMPARATOR));
+
+ private static final Comparator<Node> PARAMETER_NODE_COMPARATOR =
+ comparing(to(ParameterNode.class).andThen(ParameterNode::getParameterIndex));
+
private static final char INDEX_OPEN = '[';
private static final char INDEX_CLOSE = ']';
@@ -144,7 +172,7 @@ public abstract class NodeImpl implements Path.Node, Serializable {
if (quid.isInIterable()) {
if (quo.isInIterable()) {
if (quid.getKey() != null) {
- return Comparator.comparing(Node::getKey, KEY_COMPARATOR).compare(quid, quo);
+ return KEY_NODE_COMPARATOR.compare(quid, quo);
}
if (quo.getKey() != null) {
return -1;
@@ -173,28 +201,22 @@ public abstract class NodeImpl implements Path.Node, Serializable {
final Comparator<Node> cmp;
switch (kind) {
case BEAN:
- cmp = comparing(to(BeanNode.class), comparing(BeanNode::getContainerClass, CLASS_COMPARATOR)
- .thenComparing(BeanNode::getTypeArgumentIndex, nullsFirst(naturalOrder())));
+ cmp = BEAN_NODE_COMPARATOR;
break;
case PROPERTY:
- cmp = comparing(to(PropertyNode.class), comparing(PropertyNode::getContainerClass, CLASS_COMPARATOR)
- .thenComparing(PropertyNode::getTypeArgumentIndex, nullsFirst(naturalOrder())));
+ cmp = PROPERTY_NODE_COMPARATOR;
break;
case CONTAINER_ELEMENT:
- cmp = comparing(to(ContainerElementNode.class),
- comparing(ContainerElementNode::getContainerClass, CLASS_COMPARATOR)
- .thenComparing(ContainerElementNode::getTypeArgumentIndex, nullsFirst(naturalOrder())));
+ cmp = CONTAINER_ELEMENT_NODE_COMPARATOR;
break;
case CONSTRUCTOR:
- cmp = comparing(to(ConstructorNode.class).andThen(ConstructorNode::getParameterTypes),
- Comparators.comparingIterables(CLASS_COMPARATOR));
+ cmp = CONSTRUCTOR_NODE_COMPARATOR;
break;
case METHOD:
- cmp = comparing(to(MethodNode.class).andThen(MethodNode::getParameterTypes),
- Comparators.comparingIterables(CLASS_COMPARATOR));
+ cmp = METHOD_NODE_COMPARATOR;
break;
case PARAMETER:
- cmp = comparing(to(ParameterNode.class).andThen(ParameterNode::getParameterIndex));
+ cmp = PARAMETER_NODE_COMPARATOR;
break;
default:
return 0;