You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2026/07/24 11:51:26 UTC

(myfaces) branch 4.1.x updated (5838811d6 -> f2ba02f82)

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

asf-gitbox-commits pushed a change to branch 4.1.x
in repository https://gitbox.apache.org/repos/asf/myfaces.git


    from 5838811d6 Java compatibility
     new 82f41dcac [perf] improve standard perf
     new f2ba02f82 [perf] optimize ELResolver impls

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.


Summary of changes:
 .../config/DefaultFacesConfigResourceProvider.java |  35 ++--
 .../myfaces/config/MetaInfResourceCache.java       |  91 ++++++++++
 .../config/impl/FacesConfigUnmarshallerImpl.java   |  63 +++++--
 .../el/resolver/ResourceBundleResolver.java        |  18 +-
 .../el/resolver/ScopedAttributeResolver.java       |  59 ++++++-
 .../DefaultResourceLibraryContractsProvider.java   |  23 ++-
 .../DefaultFaceletConfigResourceProvider.java      |  27 +--
 .../myfaces/view/facelets/util/Classpath.java      | 191 ++++++++++++++++++++-
 8 files changed, 434 insertions(+), 73 deletions(-)
 create mode 100644 impl/src/main/java/org/apache/myfaces/config/MetaInfResourceCache.java


(myfaces) 01/02: [perf] improve standard perf

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 4.1.x
in repository https://gitbox.apache.org/repos/asf/myfaces.git

commit 82f41dcac9955d07df394bbcceedc944edec07f4
Author: tandraschko <ta...@apache.org>
AuthorDate: Fri Jul 24 13:48:13 2026 +0200

    [perf] improve standard perf
---
 .../config/DefaultFacesConfigResourceProvider.java |  35 ++--
 .../myfaces/config/MetaInfResourceCache.java       |  91 ++++++++++
 .../config/impl/FacesConfigUnmarshallerImpl.java   |  63 +++++--
 .../DefaultResourceLibraryContractsProvider.java   |  23 ++-
 .../DefaultFaceletConfigResourceProvider.java      |  27 +--
 .../myfaces/view/facelets/util/Classpath.java      | 191 ++++++++++++++++++++-
 6 files changed, 364 insertions(+), 66 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/config/DefaultFacesConfigResourceProvider.java b/impl/src/main/java/org/apache/myfaces/config/DefaultFacesConfigResourceProvider.java
index 7bb92cca7..0d8fa7a47 100644
--- a/impl/src/main/java/org/apache/myfaces/config/DefaultFacesConfigResourceProvider.java
+++ b/impl/src/main/java/org/apache/myfaces/config/DefaultFacesConfigResourceProvider.java
@@ -22,27 +22,22 @@
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
 
 import jakarta.faces.context.ExternalContext;
 
-import org.apache.myfaces.util.lang.ClassUtils;
 import org.apache.myfaces.spi.FacesConfigResourceProvider;
-import org.apache.myfaces.view.facelets.util.Classpath;
 
 /**
- * 
+ *
  * @since 2.0.2
  * @author Leonardo Uribe
  */
 public class DefaultFacesConfigResourceProvider extends FacesConfigResourceProvider
 {
-    private static final String META_INF_PREFIX = "META-INF/";
-
     private static final String FACES_CONFIG_SUFFIX = ".faces-config.xml";
-    
+
     /**
      * <p>Resource path used to acquire implicit resources buried
      * inside application JARs.</p>
@@ -59,21 +54,23 @@ public DefaultFacesConfigResourceProvider()
     public Collection<URL> getMetaInfConfigurationResources(ExternalContext context) throws IOException
     {
         List<URL> urlSet = new ArrayList<>();
-        
-        ClassLoader cl = ClassUtils.getCurrentLoader(this);
-        
-        //This usually happens when maven-jetty-plugin is used
-        //Scan jars looking for paths including META-INF/faces-config.xml
-        Enumeration<URL> resources = cl.getResources(FACES_CONFIG_IMPLICIT);
-        while (resources.hasMoreElements())
+
+        // Filter the shared, cached META-INF/ name scan instead of walking every jar again:
+        // - META-INF/faces-config.xml (the standard implicit name), and
+        // - META-INF/*.faces-config.xml (custom names, e.g. maven-jetty-plugin layouts).
+        // Only the handful of matches are resolved to URLs via getResources (as before).
+        ClassLoader loader = MetaInfResourceCache.getClassLoader();
+        for (String name : MetaInfResourceCache.getMetaInfEntryNames(context))
         {
-            urlSet.add(resources.nextElement());
+            if (name.equals(FACES_CONFIG_IMPLICIT) || name.endsWith(FACES_CONFIG_SUFFIX))
+            {
+                for (Enumeration<URL> resources = loader.getResources(name); resources.hasMoreElements();)
+                {
+                    urlSet.add(resources.nextElement());
+                }
+            }
         }
 
-        //Scan files inside META-INF ending with .faces-config.xml
-        URL[] urls = Classpath.search(cl, META_INF_PREFIX, FACES_CONFIG_SUFFIX);
-        Collections.addAll(urlSet, urls);
-        
         return urlSet;
     }
 }
diff --git a/impl/src/main/java/org/apache/myfaces/config/MetaInfResourceCache.java b/impl/src/main/java/org/apache/myfaces/config/MetaInfResourceCache.java
new file mode 100644
index 000000000..7c011b3a5
--- /dev/null
+++ b/impl/src/main/java/org/apache/myfaces/config/MetaInfResourceCache.java
@@ -0,0 +1,91 @@
+/*
+ * 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.myfaces.config;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Set;
+
+import jakarta.faces.FacesException;
+import jakarta.faces.context.ExternalContext;
+
+import org.apache.myfaces.util.lang.ClassUtils;
+import org.apache.myfaces.view.facelets.util.Classpath;
+
+/**
+ * Caches, per web application, the names of all classpath resource entries under {@code META-INF/}
+ * found in a single scan.
+ *
+ * <p>At startup several independent providers each scan {@code META-INF/} for a different suffix
+ * ({@code *.faces-config.xml}, {@code *.taglib.xml}, {@code contracts/.../jakarta.faces.contract.xml}).
+ * Each scan opens and fully enumerates every jar on the classpath, so doing the walk once and letting
+ * the providers filter the shared name set by suffix avoids walking every jar multiple times. Only the
+ * few matching names are then resolved to URLs via {@link ClassLoader#getResources(String)} (which is
+ * what the providers did previously anyway). The cache lives in the {@link ExternalContext} application
+ * map, so it is scoped to the web application and released on undeploy - no static state.</p>
+ */
+public final class MetaInfResourceCache
+{
+    public static final String META_INF_PREFIX = "META-INF/";
+
+    private static final String CACHE_KEY = MetaInfResourceCache.class.getName() + ".META_INF_NAMES";
+
+    private MetaInfResourceCache()
+    {
+    }
+
+    /**
+     * Returns the names of all classpath resource entries under {@code META-INF/}, scanning the
+     * classpath once per web application and caching the result. Callers filter the returned names by
+     * suffix and resolve matches via {@link ClassLoader#getResources(String)}.
+     */
+    @SuppressWarnings("unchecked")
+    public static Set<String> getMetaInfEntryNames(ExternalContext externalContext)
+    {
+        Map<String, Object> applicationMap = externalContext.getApplicationMap();
+        Set<String> names = (Set<String>) applicationMap.get(CACHE_KEY);
+        if (names == null)
+        {
+            try
+            {
+                names = Classpath.searchResourceNames(getClassLoader(), META_INF_PREFIX);
+            }
+            catch (IOException e)
+            {
+                throw new FacesException(e);
+            }
+            applicationMap.put(CACHE_KEY, names);
+        }
+        return names;
+    }
+
+    /**
+     * The classloader used both to scan and (by the providers) to resolve matches, kept consistent so
+     * the cached names line up with what {@link ClassLoader#getResources(String)} will return.
+     */
+    public static ClassLoader getClassLoader()
+    {
+        ClassLoader loader = ClassUtils.getContextClassLoader();
+        if (loader == null)
+        {
+            loader = MetaInfResourceCache.class.getClassLoader();
+        }
+        return loader;
+    }
+}
diff --git a/impl/src/main/java/org/apache/myfaces/config/impl/FacesConfigUnmarshallerImpl.java b/impl/src/main/java/org/apache/myfaces/config/impl/FacesConfigUnmarshallerImpl.java
index 6fe088f4f..bed0437dd 100755
--- a/impl/src/main/java/org/apache/myfaces/config/impl/FacesConfigUnmarshallerImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/config/impl/FacesConfigUnmarshallerImpl.java
@@ -78,6 +78,14 @@ public class FacesConfigUnmarshallerImpl implements FacesConfigUnmarshaller<Face
 {
     private static final Logger log = Logger.getLogger(FacesConfigUnmarshallerImpl.class.getName());
 
+    /**
+     * Shared, immutably-configured factory reused across all faces-config parses to skip the
+     * JAXP provider lookup ({@link DocumentBuilderFactory#newInstance()}) on every startup file.
+     * Access to {@link DocumentBuilderFactory#newDocumentBuilder()} is synchronized on this
+     * instance since the factory is not guaranteed thread-safe.
+     */
+    private static final DocumentBuilderFactory FACTORY = createFactory();
+
     private ExternalContext externalContext;
     
     public FacesConfigUnmarshallerImpl(ExternalContext externalContext)
@@ -85,6 +93,29 @@ public FacesConfigUnmarshallerImpl(ExternalContext externalContext)
         this.externalContext = externalContext;
     }
 
+    private static DocumentBuilderFactory createFactory()
+    {
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setValidating(false);
+        factory.setNamespaceAware(true);
+
+        try
+        {
+            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
+            factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+            factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+            factory.setXIncludeAware(false);
+            factory.setExpandEntityReferences(false);
+        }
+        catch (Throwable e)
+        {
+            log.log(Level.WARNING, "DocumentBuilderFactory#setFeature not implemented. Skipping...", e);
+        }
+
+        return factory;
+    }
+
     @Override
     public FacesConfigImpl getFacesConfig(String s) throws IOException, SAXException
     {
@@ -98,25 +129,16 @@ public FacesConfigImpl getFacesConfig(InputStream in, String systemId) throws IO
 
         try
         {
-            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-            factory.setValidating(false);
-            factory.setNamespaceAware(true);
-            
-            try
-            {
-                factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
-                factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
-                factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
-                factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
-                factory.setXIncludeAware(false);
-                factory.setExpandEntityReferences(false);
-            }
-            catch (Throwable e)
+            // The factory is configured identically for every faces-config, so build it once and
+            // reuse it to avoid the JAXP provider lookup (DocumentBuilderFactory#newInstance) on
+            // every file parsed at startup. newDocumentBuilder() is still called per parse.
+            DocumentBuilderFactory factory = FACTORY;
+
+            DocumentBuilder builder;
+            synchronized (factory)
             {
-                log.log(Level.WARNING, "DocumentBuilderFactory#setFeature not implemented. Skipping...", e);
+                builder = factory.newDocumentBuilder();
             }
-
-            DocumentBuilder builder = factory.newDocumentBuilder();
             builder.setEntityResolver(new FacesConfigEntityResolver(externalContext));
             Document document;
             if (systemId == null)
@@ -127,8 +149,11 @@ public FacesConfigImpl getFacesConfig(InputStream in, String systemId) throws IO
             {
                 document = builder.parse(in, systemId);
             }
-            document.getDocumentElement().normalize();
-            
+            // Note: intentionally no Element#normalize() here. Text is always read via
+            // Node#getTextContent() (see getTextContent()), which concatenates all descendant
+            // text regardless of whether adjacent text nodes were coalesced, so normalizing the
+            // whole document tree would only add a full-tree walk at startup with no effect.
+
             onAttribute("metadata-complete", document.getDocumentElement(),
                     (v) -> { facesConfig.setMetadataComplete(v); });
             onAttribute("version", document.getDocumentElement(),
diff --git a/impl/src/main/java/org/apache/myfaces/resource/DefaultResourceLibraryContractsProvider.java b/impl/src/main/java/org/apache/myfaces/resource/DefaultResourceLibraryContractsProvider.java
index f2475a0bc..d13e889e5 100644
--- a/impl/src/main/java/org/apache/myfaces/resource/DefaultResourceLibraryContractsProvider.java
+++ b/impl/src/main/java/org/apache/myfaces/resource/DefaultResourceLibraryContractsProvider.java
@@ -19,15 +19,13 @@
 package org.apache.myfaces.resource;
 
 import java.io.IOException;
-import java.net.URL;
 import java.util.HashSet;
 import java.util.Set;
 import jakarta.faces.application.ResourceHandler;
 import jakarta.faces.context.ExternalContext;
-import org.apache.myfaces.util.lang.ClassUtils;
+import org.apache.myfaces.config.MetaInfResourceCache;
 import org.apache.myfaces.util.WebConfigParamUtils;
 import org.apache.myfaces.spi.ResourceLibraryContractsProvider;
-import org.apache.myfaces.view.facelets.util.Classpath;
 
 /**
  *
@@ -78,17 +76,18 @@ public Set<String> getClassloaderResourceLibraryContracts(ExternalContext contex
     {
         Set<String> contracts = new HashSet<>();
 
-        URL[] urls = Classpath.search(ClassUtils.getCurrentLoader(this),
-                META_INF_CONTRACTS_PREFIX,
-                META_INF_CONTRACTS_SUFFIX);
-        for (int i = 0; i < urls.length; i++)
+        // Reuse the shared cached META-INF/ name scan and pick entries under META-INF/contracts/ ending
+        // with jakarta.faces.contract.xml, deriving the contract name from the entry path.
+        for (String name : MetaInfResourceCache.getMetaInfEntryNames(context))
         {
-            String urlString = urls[i].toExternalForm();
-            int suffixPos = urlString.lastIndexOf(META_INF_CONTRACTS_FILE);
-            int slashPos = urlString.lastIndexOf('/', suffixPos-1);
-            if (suffixPos > 0 && slashPos > 0)
+            if (name.startsWith(META_INF_CONTRACTS_PREFIX) && name.endsWith(META_INF_CONTRACTS_SUFFIX))
             {
-                contracts.add(urlString.substring(slashPos+1, suffixPos));
+                int suffixPos = name.lastIndexOf(META_INF_CONTRACTS_FILE);
+                int slashPos = name.lastIndexOf('/', suffixPos - 1);
+                if (suffixPos > 0 && slashPos > 0)
+                {
+                    contracts.add(name.substring(slashPos + 1, suffixPos));
+                }
             }
         }
 
diff --git a/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/DefaultFaceletConfigResourceProvider.java b/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/DefaultFaceletConfigResourceProvider.java
index f5e9015bd..888cbedfa 100644
--- a/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/DefaultFaceletConfigResourceProvider.java
+++ b/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/DefaultFaceletConfigResourceProvider.java
@@ -22,24 +22,21 @@
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
+import java.util.Enumeration;
 import java.util.List;
 
 import jakarta.faces.context.ExternalContext;
 
-import org.apache.myfaces.util.lang.ClassUtils;
+import org.apache.myfaces.config.MetaInfResourceCache;
 import org.apache.myfaces.spi.FaceletConfigResourceProvider;
-import org.apache.myfaces.view.facelets.util.Classpath;
 
 /**
- * 
+ *
  * @since 2.0.2
  * @author Leonardo Uribe
  */
 public class DefaultFaceletConfigResourceProvider extends FaceletConfigResourceProvider
 {
-    private static final String META_INF_PREFIX = "META-INF/";
-
     private static final String FACELET_TAGLIB_SUFFIX = ".taglib.xml";
 
     public DefaultFaceletConfigResourceProvider()
@@ -52,10 +49,20 @@ public Collection<URL> getFaceletTagLibConfigurationResources(
             ExternalContext context) throws IOException
     {
         List<URL> urlSet = new ArrayList<>();
- 
-        //Scan files inside META-INF ending with .faces-config.xml
-        URL[] urls = Classpath.search(ClassUtils.getCurrentLoader(this), META_INF_PREFIX, FACELET_TAGLIB_SUFFIX);
-        Collections.addAll(urlSet, urls);
+
+        // Files inside META-INF ending with .taglib.xml, reusing the shared cached META-INF/ name scan;
+        // only the matches are resolved to URLs via getResources (as before).
+        ClassLoader loader = MetaInfResourceCache.getClassLoader();
+        for (String name : MetaInfResourceCache.getMetaInfEntryNames(context))
+        {
+            if (name.endsWith(FACELET_TAGLIB_SUFFIX))
+            {
+                for (Enumeration<URL> resources = loader.getResources(name); resources.hasMoreElements();)
+                {
+                    urlSet.add(resources.nextElement());
+                }
+            }
+        }
 
         return urlSet;
     }
diff --git a/impl/src/main/java/org/apache/myfaces/view/facelets/util/Classpath.java b/impl/src/main/java/org/apache/myfaces/view/facelets/util/Classpath.java
index 46946dc85..0c8b14919 100644
--- a/impl/src/main/java/org/apache/myfaces/view/facelets/util/Classpath.java
+++ b/impl/src/main/java/org/apache/myfaces/view/facelets/util/Classpath.java
@@ -64,30 +64,209 @@ public static URL[] search(ClassLoader loader, String prefix, String suffix) thr
     {
         Set<URL> all = new LinkedHashSet<>();
 
-        _searchResource(all, loader, prefix, prefix, suffix);
-        _searchResource(all, loader, prefix + "MANIFEST.MF", prefix, suffix);
+        // The two passes below (directory entry + MANIFEST.MF) are both needed because some
+        // classloaders/jars expose only one of them, but for any jar exposed by both we would
+        // otherwise open and fully enumerate its entries twice. Track the jars already scanned
+        // (keyed by the jar file portion of the URL) so each jar is opened and walked only once
+        // per search - startup calls this for every jar on the classpath, several times over.
+        Set<String> scannedJars = new HashSet<>();
+
+        _searchResource(all, loader, prefix, prefix, suffix, scannedJars);
+        _searchResource(all, loader, prefix + "MANIFEST.MF", prefix, suffix, scannedJars);
 
         URL[] urlArray = all.toArray(new URL[all.size()]);
 
         return urlArray;
     }
 
+    /**
+     * Collect, in a single classpath walk, the <em>names</em> of every resource entry whose name
+     * starts with the given {@code prefix} (e.g. {@code "META-INF/"}). Unlike
+     * {@link #search(ClassLoader, String, String)} this neither filters by suffix nor resolves URLs;
+     * it only enumerates jar/directory entries once and returns their names.
+     *
+     * <p>Callers that repeatedly search the same prefix for different suffixes (the startup
+     * config/taglib/contract providers all scan {@code META-INF/}) can walk the classpath once, filter
+     * the returned names by suffix, and resolve the handful of matches via
+     * {@link ClassLoader#getResources(String)} - instead of re-opening and re-enumerating every jar
+     * once per suffix. Keeping only names (not eagerly-built URLs) avoids allocating a URL per entry.</p>
+     */
+    public static Set<String> searchResourceNames(ClassLoader loader, String prefix) throws IOException
+    {
+        Set<String> names = new HashSet<>();
+        Set<String> scannedJars = new HashSet<>();
+
+        _collectResourceNames(names, loader, prefix, prefix, scannedJars);
+        _collectResourceNames(names, loader, prefix + "MANIFEST.MF", prefix, scannedJars);
+
+        return names;
+    }
+
+    private static void _collectResourceNames(Set<String> names, ClassLoader loader, String resource,
+                                              String prefix, Set<String> scannedJars) throws IOException
+    {
+        for (Enumeration<URL> urls = loader.getResources(resource); urls.hasMoreElements();)
+        {
+            URL url = urls.nextElement();
+
+            String externalForm = url.toExternalForm();
+            int jarSeparator = externalForm.indexOf("!/");
+            String jarKey = jarSeparator != -1 ? externalForm.substring(0, jarSeparator) : null;
+            if (jarKey != null && scannedJars.contains(jarKey))
+            {
+                continue;
+            }
+
+            URLConnection conn = url.openConnection();
+            conn.setUseCaches(false);
+            conn.setDefaultUseCaches(false);
+
+            try (JarFile jar = (conn instanceof JarURLConnection jurlc) ?
+                jurlc.getJarFile() : _getAlternativeJarFile(url))
+            {
+                if (jar != null)
+                {
+                    Enumeration<JarEntry> entries = jar.entries();
+                    while (entries.hasMoreElements())
+                    {
+                        String name = entries.nextElement().getName();
+                        if (name.startsWith(prefix))
+                        {
+                            names.add(name);
+                        }
+                    }
+                    if (jarKey != null)
+                    {
+                        scannedJars.add(jarKey);
+                    }
+                }
+                else
+                {
+                    File dir = new File(URLDecoder.decode(url.getFile(), StandardCharsets.UTF_8));
+                    if (!_collectDirNames(names, dir, prefix))
+                    {
+                        _collectNamesFromURL(names, prefix, url);
+                    }
+                }
+            }
+            catch (Throwable e)
+            {
+                // See _searchResource: a classloader may report a resource that cannot actually be
+                // opened. Ignore and let the other pass (or other classpath entries) cover it.
+                continue;
+            }
+        }
+    }
+
+    private static boolean _collectDirNames(Set<String> names, File dir, String currentPrefix)
+    {
+        // dir corresponds to `currentPrefix` itself (e.g. .../META-INF for prefix "META-INF/")
+        if (dir.exists() && dir.isDirectory())
+        {
+            File[] files = dir.listFiles();
+            if (files != null)
+            {
+                for (File file : files)
+                {
+                    if (file.isDirectory())
+                    {
+                        _collectDirNames(names, file, currentPrefix + file.getName() + '/');
+                    }
+                    else
+                    {
+                        names.add(currentPrefix + file.getName());
+                    }
+                }
+            }
+            return true;
+        }
+        return false;
+    }
+
+    private static void _collectNamesFromURL(Set<String> names, String prefix, URL url) throws IOException
+    {
+        boolean done = false;
+
+        try (InputStream is = _getInputStream(url))
+        {
+            if (is != null)
+            {
+                try
+                {
+                    ZipInputStream zis = (is instanceof ZipInputStream stream) ? stream : new ZipInputStream(is);
+                    try
+                    {
+                        ZipEntry entry = zis.getNextEntry();
+                        done = entry != null;
+                        while (entry != null)
+                        {
+                            String entryName = entry.getName();
+                            if (entryName.startsWith(prefix))
+                            {
+                                names.add(entryName);
+                            }
+                            entry = zis.getNextEntry();
+                        }
+                    }
+                    finally
+                    {
+                        zis.close();
+                    }
+                }
+                catch (Exception ignore)
+                {
+                }
+            }
+        }
+
+        if (!done && prefix.length() > 0)
+        {
+            String urlString = url.toExternalForm() + '/';
+            String[] split = prefix.split("/");
+            String parentPrefix = _join(split, true);
+            String end = _join(split, false);
+            urlString = urlString.substring(0, urlString.lastIndexOf(end));
+            if (isExcludedPrefix(urlString))
+            {
+                return;
+            }
+            _collectNamesFromURL(names, parentPrefix, new URL(urlString));
+        }
+    }
+
     private static void _searchResource(Set<URL> result, ClassLoader loader, String resource, String prefix,
-                                        String suffix) throws IOException
+                                        String suffix, Set<String> scannedJars) throws IOException
     {
         for (Enumeration<URL> urls = loader.getResources(resource); urls.hasMoreElements();)
         {
             URL url = urls.nextElement();
+
+            // If this URL points inside a jar we already scanned in a previous pass, skip it:
+            // the jar's entries are identical regardless of which META-INF resource led us here.
+            String externalForm = url.toExternalForm();
+            int jarSeparator = externalForm.indexOf("!/");
+            String jarKey = jarSeparator != -1 ? externalForm.substring(0, jarSeparator) : null;
+            if (jarKey != null && scannedJars.contains(jarKey))
+            {
+                continue;
+            }
+
             URLConnection conn = url.openConnection();
             conn.setUseCaches(false);
             conn.setDefaultUseCaches(false);
 
-            try (JarFile jar = (conn instanceof JarURLConnection) ? 
-                ((JarURLConnection) conn).getJarFile() : _getAlternativeJarFile(url))
+            try (JarFile jar = (conn instanceof JarURLConnection jurlc) ?
+                jurlc.getJarFile() : _getAlternativeJarFile(url))
             {
                 if (jar != null)
                 {
                     _searchJar(loader, result, jar, prefix, suffix);
+                    // Only mark as scanned once we have actually enumerated it, so that a jar which
+                    // fails the directory-entry pass can still be picked up by the MANIFEST.MF pass.
+                    if (jarKey != null)
+                    {
+                        scannedJars.add(jarKey);
+                    }
                 }
                 else
                 {
@@ -220,7 +399,7 @@ private static void _searchFromURL(Set<URL> result, String prefix, String suffix
     }
 
     /**
-     * Join tokens, exlude last if param equals true.
+     * Join tokens, exclude last if param equals true.
      * 
      * @param tokens
      *            the tokens


(myfaces) 02/02: [perf] optimize ELResolver impls

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 4.1.x
in repository https://gitbox.apache.org/repos/asf/myfaces.git

commit f2ba02f827fd3ef6b6cd95e8e2ad73bd54b9967a
Author: tandraschko <ta...@apache.org>
AuthorDate: Thu Jul 16 13:58:40 2026 +0200

    [perf] optimize ELResolver impls
    
    (cherry picked from commit b41d0d9f280a8650505daf90202351e834726d18)
---
 .../el/resolver/ResourceBundleResolver.java        | 18 +++++--
 .../el/resolver/ScopedAttributeResolver.java       | 59 ++++++++++++++++++++--
 2 files changed, 70 insertions(+), 7 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/el/resolver/ResourceBundleResolver.java b/impl/src/main/java/org/apache/myfaces/el/resolver/ResourceBundleResolver.java
index 29ccca2af..55418ba57 100644
--- a/impl/src/main/java/org/apache/myfaces/el/resolver/ResourceBundleResolver.java
+++ b/impl/src/main/java/org/apache/myfaces/el/resolver/ResourceBundleResolver.java
@@ -128,6 +128,13 @@ public Object getValue(final ELContext context, final Object base, final Object
         {
             return null;
         }
+        // fast path: when no <resource-bundle> var is configured in faces-config, this resolver can
+        // never resolve anything - skip the per-identifier Application.getResourceBundle/getBundleName
+        // chain that otherwise runs for every EL identifier (e.g. #{row} on every table cell).
+        if (runtimeConfig(context).getResourceBundles().isEmpty())
+        {
+            return null;
+        }
 
         final ResourceBundle bundle = getResourceBundle(context, (String)property);
         if (bundle != null)
@@ -155,6 +162,10 @@ public Class<?> getType(final ELContext context, final Object base, final Object
         {
             return null;
         }
+        if (runtimeConfig(context).getResourceBundles().isEmpty())
+        {
+            return null;
+        }
 
         final ResourceBundle bundle = getResourceBundle(context, (String)property);
         if (bundle != null)
@@ -217,12 +228,11 @@ private static ResourceBundle getResourceBundle(final ELContext context, final S
 
     protected RuntimeConfig runtimeConfig(ELContext context)
     {
-        final FacesContext facesContext = facesContext(context);
-
-        // application-level singleton - we can safely cache this
+        // application-level singleton - we can safely cache this (only resolve the FacesContext on
+        // the first call; this runs per EL identifier so avoid the lookup once cached)
         if (this.runtimeConfig == null)
         {
-            this.runtimeConfig = RuntimeConfig.getCurrentInstance(facesContext.getExternalContext());
+            this.runtimeConfig = RuntimeConfig.getCurrentInstance(facesContext(context).getExternalContext());
         }
 
         return runtimeConfig;
diff --git a/impl/src/main/java/org/apache/myfaces/el/resolver/ScopedAttributeResolver.java b/impl/src/main/java/org/apache/myfaces/el/resolver/ScopedAttributeResolver.java
index e5fc6f6ad..5e551d41b 100644
--- a/impl/src/main/java/org/apache/myfaces/el/resolver/ScopedAttributeResolver.java
+++ b/impl/src/main/java/org/apache/myfaces/el/resolver/ScopedAttributeResolver.java
@@ -104,10 +104,63 @@ public Object getValue(final ELContext context, final Object base, final Object
 
         context.setPropertyResolved(true);
 
-        final Map<String, Object> scopedMap = findScopedMap(facesContext(context), property);
-        if (scopedMap != null)
+        final FacesContext facesContext = facesContext(context);
+        if (facesContext == null)
+        {
+            return null;
+        }
+        final ExternalContext extContext = facesContext.getExternalContext();
+        if (extContext == null)
+        {
+            return null;
+        }
+
+        final boolean startup = (extContext instanceof StartupServletExternalContextImpl);
+
+        // A single get() per scope instead of findScopedMap()'s containsKey()+get(): servlet
+        // request/session/application attributes are never null, so a non-null get() means the key
+        // is present. This halves the attribute-map lookups for the very hot case of resolving a
+        // UIData/UIRepeat row var (e.g. #{row}) once per cell. Mirrors the reference
+        // ScopedAttributeELResolver. The view map is a plain Map that may legitimately hold a null
+        // value, so it keeps the containsKey() check.
+        Object value;
+
+        // request scope (not available at startup)
+        if (!startup)
+        {
+            value = extContext.getRequestMap().get(property);
+            if (value != null)
+            {
+                return value;
+            }
+        }
+
+        // jsf 2.0 view scope
+        UIViewRoot root = facesContext.getViewRoot();
+        if (root != null)
+        {
+            Map<String, Object> viewMap = root.getViewMap(false);
+            if (viewMap != null && viewMap.containsKey(property))
+            {
+                return viewMap.get(property);
+            }
+        }
+
+        // session scope (not available at startup)
+        if (!startup)
+        {
+            value = extContext.getSessionMap().get(property);
+            if (value != null)
+            {
+                return value;
+            }
+        }
+
+        // application scope
+        value = extContext.getApplicationMap().get(property);
+        if (value != null)
         {
-            return scopedMap.get(property);
+            return value;
         }
 
         return null;