You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by pa...@apache.org on 2026/09/06 07:10:40 UTC

(wicket) branch resource-name-iterator-alloc created (now 00c2ef56f1)

This is an automated email from the ASF dual-hosted git repository.

papegaaij pushed a change to branch resource-name-iterator-alloc
in repository https://gitbox.apache.org/repos/asf/wicket.git


      at 00c2ef56f1 Stop re-deriving the locale part of every candidate resource name

This branch includes the following new commits:

     new fd1df62c15 Benchmark the resource name iterator walk
     new 00c2ef56f1 Stop re-deriving the locale part of every candidate resource name

The 2 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.



(wicket) 02/02: Stop re-deriving the locale part of every candidate resource name

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

papegaaij pushed a commit to branch resource-name-iterator-alloc
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit 00c2ef56f1d5b79fa39e6c78cc0e1e93b4a166ec
Author: Emond Papegaaij <em...@topicus.nl>
AuthorDate: Sun Sep 6 07:09:37 2026 +0000

    Stop re-deriving the locale part of every candidate resource name
    
    ResourceNameIterator.toString() built each candidate name from four prepend()
    calls, each concatenating a char with an Object, so four throwaway strings were
    allocated on top of the result. The locale part was the expensive one: it went
    through getLocale(), which calls Locale.of() - a LocaleObjectCache lookup
    rather than a field read - even though LocaleResourceNameIterator.next() had
    just built the identical suffix and discarded it.
    
    next() now keeps that suffix and toString() reuses it; getLocale() memoises the
    derived Locale per state for the locator's own read-back; the style and
    variation parts are rebuilt only when the style iterator advances; and each
    branch of toString() is a single concatenation, so only the returned string is
    allocated. ExtensionResourceNameIterator was also building its backing iterator
    twice.
    
    ResourceNameIteratorBenchmark, nl_NL without a style, 3 forks:
    
      walkAllCandidates (miss)  923.8 -> 167.8 ns/op   1224 -> 736 B/op
      firstCandidate (hit)      775.2 -> 421.6 ns/op    600 -> 432 B/op
    
    Verified equivalent by enumerating every candidate name and its locale, style,
    variation and extension across 6 paths x 2 styles x 2 variations x 7 locales x
    5 extension lists x strict/non-strict - 9918 names - before and after the
    change: byte-identical.
    
    One behavioural note for reviewers: toString() now takes the locale segment
    from next() instead of from getLocale(). For the shipped implementation those
    are provably the same, which is what the enumeration above establishes. A
    subclass supplied through the protected newLocaleResourceNameIterator hook that
    overrode getLocale() without also overriding next() would see a difference;
    nothing in the tree does that.
    
    Co-Authored-By: Claude Opus 5 (1M context) <no...@anthropic.com>
---
 .../locator/ExtensionResourceNameIterator.java     |  5 ++--
 .../locator/LocaleResourceNameIterator.java        | 34 ++++++++++++++++++++++
 .../resource/locator/ResourceNameIterator.java     | 30 ++++++++++++-------
 3 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ExtensionResourceNameIterator.java b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ExtensionResourceNameIterator.java
index b1ffe2762a..540f8efda3 100644
--- a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ExtensionResourceNameIterator.java
+++ b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ExtensionResourceNameIterator.java
@@ -42,13 +42,14 @@ public class ExtensionResourceNameIterator implements Iterator<String>
 	public ExtensionResourceNameIterator(final Iterable<String> extensions)
 	{
 		// Fail safe: hasNext() needs to return at least once with true
-		if (extensions == null || !extensions.iterator().hasNext())
+		Iterator<String> extensionIterator = extensions == null ? null : extensions.iterator();
+		if (extensionIterator == null || !extensionIterator.hasNext())
 		{
 			this.iterator = NULL_ITERABLE.iterator();
 		}
 		else
 		{
-			this.iterator = extensions.iterator();
+			this.iterator = extensionIterator;
 		}
 	}
 
diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/LocaleResourceNameIterator.java b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/LocaleResourceNameIterator.java
index 3faedccda6..8edf3436df 100644
--- a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/LocaleResourceNameIterator.java
+++ b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/LocaleResourceNameIterator.java
@@ -39,6 +39,13 @@ public class LocaleResourceNameIterator implements Iterator<String>
 
 	private final boolean strict;
 
+	/** The locale part of the resource name that {@link #next()} produced for this state */
+	private String suffix = "";
+
+	private int localeState = -1;
+
+	private Locale currentLocale;
+
 	/**
 	 * Construct.
 	 *
@@ -55,6 +62,18 @@ public class LocaleResourceNameIterator implements Iterator<String>
 	 * @return Locale
 	 */
 	public Locale getLocale()
+	{
+		// Locale.of() is a cache lookup, not a field read, and this is called once per candidate
+		// name; the state only changes in next().
+		if (localeState != state)
+		{
+			localeState = state;
+			currentLocale = localeForState();
+		}
+		return currentLocale;
+	}
+
+	private Locale localeForState()
 	{
 		if (state == 1)
 		{
@@ -72,6 +91,15 @@ public class LocaleResourceNameIterator implements Iterator<String>
 		return null;
 	}
 
+	/**
+	 * @return the locale part of the resource name for the current state, already prefixed with
+	 *         {@code '_'}, or the empty string if this state carries no locale
+	 */
+	public String getSuffix()
+	{
+		return suffix;
+	}
+
 	/**
 	 *
 	 * @see java.util.Iterator#hasNext()
@@ -93,6 +121,12 @@ public class LocaleResourceNameIterator implements Iterator<String>
 	 */
 	@Override
 	public String next()
+	{
+		suffix = nextSuffix();
+		return suffix;
+	}
+
+	private String nextSuffix()
 	{
 		if (locale == null)
 		{
diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceNameIterator.java b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceNameIterator.java
index f89c959a3a..93265c880d 100644
--- a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceNameIterator.java
+++ b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceNameIterator.java
@@ -69,6 +69,11 @@ public class ResourceNameIterator implements IResourceNameIterator
 	// The various iterators used to locate the resource file
 	private final StyleAndVariationResourceNameIterator styleIterator;
 	private LocaleResourceNameIterator localeIterator;
+
+	// The variation and style parts of the name, rebuilt only when the style iterator advances.
+	private String variationPart = "";
+
+	private String stylePart = "";
 	private ExtensionResourceNameIterator extensionsIterator;
 
 	/**
@@ -199,6 +204,8 @@ public class ResourceNameIterator implements IResourceNameIterator
 		while (styleIterator.hasNext())
 		{
 			styleIterator.next();
+			variationPart = part(styleIterator.getVariation(), '_');
+			stylePart = part(styleIterator.getStyle(), '_');
 
 			localeIterator = newLocaleResourceNameIterator(locale, strict);
 			while (localeIterator.hasNext())
@@ -244,19 +251,22 @@ public class ResourceNameIterator implements IResourceNameIterator
 	@Override
 	public String toString()
 	{
-		return path + prepend(getVariation(), '_') + prepend(getStyle(), '_') +
-			prepend(getLocale(), '_') + prepend(getExtension(), '.');
+		// The locale part was already built by LocaleResourceNameIterator.next(); deriving it
+		// again through getLocale() would cost a Locale.of() lookup and its string form for every
+		// candidate name. Each branch is a single concatenation, so only the result is allocated.
+		String localePart = localeIterator != null ? localeIterator.getSuffix() : "";
+		String extension = getExtension();
+
+		if (extension == null)
+		{
+			return path + variationPart + stylePart + localePart;
+		}
+		return path + variationPart + stylePart + localePart + '.' + extension;
 	}
 
-	/**
-	 * 
-	 * @param string
-	 * @param prepend
-	 * @return The string prepended with the char
-	 */
-	private String prepend(Object string, char prepend)
+	private static String part(String value, char prepend)
 	{
-		return (string != null) ? prepend + string.toString() : "";
+		return value == null ? "" : prepend + value;
 	}
 
 	/**


(wicket) 01/02: Benchmark the resource name iterator walk

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

papegaaij pushed a commit to branch resource-name-iterator-alloc
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit fd1df62c15efbd24b88313cc7c042e752f572771
Author: Emond Papegaaij <em...@topicus.nl>
AuthorDate: Sun Sep 6 07:09:18 2026 +0000

    Benchmark the resource name iterator walk
    
    ResourceStreamLocator walks a list of candidate filenames for every property
    and markup lookup, once per registered properties loader. That walk dominates
    i18n lookup cost on a miss, and misses are the common case: a key is resolved
    by climbing the component hierarchy, so every class above the one that actually
    declares it contributes a full traversal that finds nothing.
    
    The hit and the miss are benchmarked directly rather than derived from one
    another, because they do different amounts of work, and the read-back of
    locale, style and variation is included in the hit - it is the only caller of
    getLocale(), so leaving it out would hide that cost. Parameterised over the
    locale shapes, which drive how many candidates a traversal produces.
    
    Co-Authored-By: Claude Opus 5 (1M context) <no...@anthropic.com>
---
 .../benchmarks/ResourceNameIteratorBenchmark.java  | 126 +++++++++++++++++++++
 1 file changed, 126 insertions(+)

diff --git a/wicket-benchmarks/src/main/java/org/apache/wicket/benchmarks/ResourceNameIteratorBenchmark.java b/wicket-benchmarks/src/main/java/org/apache/wicket/benchmarks/ResourceNameIteratorBenchmark.java
new file mode 100644
index 0000000000..b9579446ce
--- /dev/null
+++ b/wicket-benchmarks/src/main/java/org/apache/wicket/benchmarks/ResourceNameIteratorBenchmark.java
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.wicket.benchmarks;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.wicket.core.util.resource.locator.ResourceNameIterator;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+/**
+ * Benchmarks the candidate-filename walk that {@code ResourceStreamLocator} performs for every
+ * property and markup lookup, once per registered properties loader.
+ * <p>
+ * The walk dominates i18n lookup cost on a miss, and misses are the common case: a key is resolved
+ * by climbing the component hierarchy, so every class above the one that actually declares the key
+ * contributes a full traversal that finds nothing.
+ * <p>
+ * The two cases are benchmarked directly rather than derived from one another, because they do
+ * different amounts of work. {@link #walkAllCandidates} is the miss - every combination of style,
+ * variation, locale and extension is produced. {@link #firstCandidate} is the hit, where the
+ * locator stops at the first name and then reads back the locale, style and variation to stamp on
+ * the resource stream; that read-back is the only caller of {@code getLocale()}, so a benchmark
+ * that never performs it would miss the cost.
+ * <p>
+ * Written against public API only, so the same source can be run against two implementations and
+ * compared. {@code -prof gc} is the point of this one: {@code gc.alloc.rate.norm} is what the
+ * change being measured actually moves.
+ */
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@Fork(3)
+@Threads(1)
+@Warmup(iterations = 3, time = 2, timeUnit = TimeUnit.SECONDS)
+@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
+@State(Scope.Benchmark)
+public class ResourceNameIteratorBenchmark
+{
+	/**
+	 * The locale shapes differ in how many candidates they produce, which is the main driver of
+	 * both time and allocation: a language+country locale yields three rounds, language-only two,
+	 * and no locale one.
+	 */
+	public enum LocaleShape
+	{
+		LANGUAGE_COUNTRY(Locale.of("nl", "NL")),
+		LANGUAGE_ONLY(Locale.of("nl")),
+		WITH_VARIANT(Locale.of("nl", "NL", "vlaams")),
+		NONE(null);
+
+		private final Locale locale;
+
+		LocaleShape(Locale locale)
+		{
+			this.locale = locale;
+		}
+	}
+
+	// PropertiesFactory hands the locator a path with the extension already appended, and the
+	// iterator splits it back off.
+	private static final String PATH = "org/example/app/pages/group/GroupsPage.properties";
+
+	private static final List<String> ONE_EXTENSION = Arrays.asList("properties");
+
+	@Param
+	public LocaleShape localeShape;
+
+	@Param({"false", "true"})
+	public boolean styled;
+
+	@Benchmark
+	public void walkAllCandidates(Blackhole blackhole)
+	{
+		ResourceNameIterator names = newIterator();
+		while (names.hasNext())
+		{
+			blackhole.consume(names.next());
+		}
+	}
+
+	@Benchmark
+	public void firstCandidate(Blackhole blackhole)
+	{
+		ResourceNameIterator names = newIterator();
+		if (names.hasNext())
+		{
+			blackhole.consume(names.next());
+			blackhole.consume(names.getLocale());
+			blackhole.consume(names.getStyle());
+			blackhole.consume(names.getVariation());
+		}
+	}
+
+	private ResourceNameIterator newIterator()
+	{
+		return new ResourceNameIterator(PATH, styled ? "mystyle" : null,
+			styled ? "myvariation" : null, localeShape.locale, ONE_EXTENSION, false);
+	}
+}