You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "bkkothari2255 (via GitHub)" <gi...@apache.org> on 2026/04/04 07:59:32 UTC

[PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

bkkothari2255 opened a new pull request, #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86

   Issue : SLING-12217
   
   While SLING-12244 fixed the database side of these duplicate lookups, `AdapterImplementations.lookup()` was still hammering the CPU by re-evaluating search paths and querying the large `ConcurrentHashMap` hundreds of times per page render for the same exact resource type
   
   changes:
   - Request-scoped Cache: attach a cache directly to `ResourceResolver.getPropertyMap()` so it lives and dies with the HTTP request
   - IdentityHashMap: Used this to map the cache keys. This is critical to prevent Java from doing O(N) deep equality checks against the large map of registered Sling Models.
   - Remembering Not Found Results: We use a placeholder (Object.class) to remember when a resource type doesn't have a model. This lets us instantly abort future checks instead of endlessly re-walking the JCR tree looking for something that isn't there.
   
   Let me know what you think?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#issuecomment-4186898735

   ## [![Quality Gate Failed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-failed-20px.png 'Quality Gate Failed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86) **Quality Gate failed**  
   Failed conditions  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/failed-16px.png '') [B Reliability Rating on New Code](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86) (required ≥ A)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86)
   
   ##   
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/light_bulb-16px.png '') Catch issues before they fail your Quality Gate with our IDE extension ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/sonarlint-16px.png '') [SonarQube for IDE](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=pull-request)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "joerghoh (via GitHub)" <gi...@apache.org>.
joerghoh commented on code in PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#discussion_r3035743013


##########
src/main/java/org/apache/sling/models/impl/AdapterImplementations.java:
##########
@@ -314,39 +316,71 @@ public Class<?> getModelClassForResource(final Resource resource) {
         return getModelClassForResource(resource, resourceTypeMappingsForResources);
     }
 
+    @SuppressWarnings("unchecked")
+    private static Map<Map<String, Class<?>>, Map<String, Object>> getModelClassCache(final ResourceResolver resolver) {
+        return (Map<Map<String, Class<?>>, Map<String, Object>>)
+                resolver.getPropertyMap().get(CACHE_KEY);
+    }
+
     protected static Class<?> getModelClassForResource(final Resource resource, final Map<String, Class<?>> map) {
         if (resource == null) {
             return null;
         }
         ResourceResolver resolver = resource.getResourceResolver();
         final String originalResourceType = resource.getResourceType();
+        if (originalResourceType.isEmpty()) {
+            return null;
+        }
+
+        Map<Map<String, Class<?>>, Map<String, Object>> cache = getModelClassCache(resolver);
+
+        if (cache == null) {
+            // Thread-safe Identity map to prevent O(N) deep equality checks on the massive map
+            cache = java.util.Collections.synchronizedMap(new java.util.IdentityHashMap<>());

Review Comment:
   A ``ResourceResolver`` is not thread-safe, so this cache does not need to be thread-safe as well.



##########
src/main/java/org/apache/sling/models/impl/AdapterImplementations.java:
##########
@@ -314,39 +316,71 @@ public Class<?> getModelClassForResource(final Resource resource) {
         return getModelClassForResource(resource, resourceTypeMappingsForResources);
     }
 
+    @SuppressWarnings("unchecked")
+    private static Map<Map<String, Class<?>>, Map<String, Object>> getModelClassCache(final ResourceResolver resolver) {
+        return (Map<Map<String, Class<?>>, Map<String, Object>>)
+                resolver.getPropertyMap().get(CACHE_KEY);
+    }
+
     protected static Class<?> getModelClassForResource(final Resource resource, final Map<String, Class<?>> map) {
         if (resource == null) {
             return null;
         }
         ResourceResolver resolver = resource.getResourceResolver();
         final String originalResourceType = resource.getResourceType();
+        if (originalResourceType.isEmpty()) {
+            return null;
+        }
+
+        Map<Map<String, Class<?>>, Map<String, Object>> cache = getModelClassCache(resolver);
+
+        if (cache == null) {
+            // Thread-safe Identity map to prevent O(N) deep equality checks on the massive map
+            cache = java.util.Collections.synchronizedMap(new java.util.IdentityHashMap<>());

Review Comment:
   Also, why is an IdentityHashMap used? This would be only effective, if the same ``map`` reference is used multiple times when calling this method. Is this really the case? 



##########
src/main/java/org/apache/sling/models/impl/AdapterImplementations.java:
##########
@@ -314,39 +316,71 @@ public Class<?> getModelClassForResource(final Resource resource) {
         return getModelClassForResource(resource, resourceTypeMappingsForResources);
     }
 
+    @SuppressWarnings("unchecked")
+    private static Map<Map<String, Class<?>>, Map<String, Object>> getModelClassCache(final ResourceResolver resolver) {
+        return (Map<Map<String, Class<?>>, Map<String, Object>>)
+                resolver.getPropertyMap().get(CACHE_KEY);
+    }
+
     protected static Class<?> getModelClassForResource(final Resource resource, final Map<String, Class<?>> map) {
         if (resource == null) {
             return null;
         }
         ResourceResolver resolver = resource.getResourceResolver();
         final String originalResourceType = resource.getResourceType();
+        if (originalResourceType.isEmpty()) {
+            return null;
+        }
+
+        Map<Map<String, Class<?>>, Map<String, Object>> cache = getModelClassCache(resolver);

Review Comment:
   That's a very complex type definition and hard to reason about. Would it make sense to encapsulate some parts of it in an inner class, just to simplify this expression?



##########
src/main/java/org/apache/sling/models/impl/AdapterImplementations.java:
##########
@@ -314,39 +316,71 @@ public Class<?> getModelClassForResource(final Resource resource) {
         return getModelClassForResource(resource, resourceTypeMappingsForResources);
     }
 
+    @SuppressWarnings("unchecked")
+    private static Map<Map<String, Class<?>>, Map<String, Object>> getModelClassCache(final ResourceResolver resolver) {
+        return (Map<Map<String, Class<?>>, Map<String, Object>>)
+                resolver.getPropertyMap().get(CACHE_KEY);
+    }
+
     protected static Class<?> getModelClassForResource(final Resource resource, final Map<String, Class<?>> map) {
         if (resource == null) {
             return null;
         }
         ResourceResolver resolver = resource.getResourceResolver();
         final String originalResourceType = resource.getResourceType();
+        if (originalResourceType.isEmpty()) {
+            return null;
+        }
+
+        Map<Map<String, Class<?>>, Map<String, Object>> cache = getModelClassCache(resolver);
+
+        if (cache == null) {
+            // Thread-safe Identity map to prevent O(N) deep equality checks on the massive map
+            cache = java.util.Collections.synchronizedMap(new java.util.IdentityHashMap<>());

Review Comment:
   And a third aspect: We probably should avoid using an unbounded map, as it could lead to massive memory usage or even a memory leak.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "bkkothari2255 (via GitHub)" <gi...@apache.org>.
bkkothari2255 commented on code in PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#discussion_r3035952322


##########
src/main/java/org/apache/sling/models/impl/AdapterImplementations.java:
##########
@@ -314,39 +316,71 @@ public Class<?> getModelClassForResource(final Resource resource) {
         return getModelClassForResource(resource, resourceTypeMappingsForResources);
     }
 
+    @SuppressWarnings("unchecked")
+    private static Map<Map<String, Class<?>>, Map<String, Object>> getModelClassCache(final ResourceResolver resolver) {
+        return (Map<Map<String, Class<?>>, Map<String, Object>>)
+                resolver.getPropertyMap().get(CACHE_KEY);
+    }
+
     protected static Class<?> getModelClassForResource(final Resource resource, final Map<String, Class<?>> map) {
         if (resource == null) {
             return null;
         }
         ResourceResolver resolver = resource.getResourceResolver();
         final String originalResourceType = resource.getResourceType();
+        if (originalResourceType.isEmpty()) {
+            return null;
+        }
+
+        Map<Map<String, Class<?>>, Map<String, Object>> cache = getModelClassCache(resolver);
+
+        if (cache == null) {
+            // Thread-safe Identity map to prevent O(N) deep equality checks on the massive map
+            cache = java.util.Collections.synchronizedMap(new java.util.IdentityHashMap<>());

Review Comment:
   > Also, why is an IdentityHashMap used? This would be only effective, if the same `map` reference is used multiple times when calling this method. Is this really the case?
   
   I double-checked this, and the `map` reference is indeed constant 
   
   the `map` argument always comes from the class-level variables (e.g., [resourceTypeMappingsForResources](https://github.com/bkkothari2255/sling-org-apache-sling-models-impl/blob/22395917e8bd080c136c62df552e84f91a1eb636/src/main/java/org/apache/sling/models/impl/AdapterImplementations.java#L60)), which are declared as final, Because the reference never changes and `IdentityHashMap` works perfectly here to skip the deep equality check
   
    Could you verify if my logic holds up there?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "bkkothari2255 (via GitHub)" <gi...@apache.org>.
bkkothari2255 commented on code in PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#discussion_r3035941975


##########
src/main/java/org/apache/sling/models/impl/AdapterImplementations.java:
##########
@@ -314,39 +316,71 @@ public Class<?> getModelClassForResource(final Resource resource) {
         return getModelClassForResource(resource, resourceTypeMappingsForResources);
     }
 
+    @SuppressWarnings("unchecked")
+    private static Map<Map<String, Class<?>>, Map<String, Object>> getModelClassCache(final ResourceResolver resolver) {
+        return (Map<Map<String, Class<?>>, Map<String, Object>>)
+                resolver.getPropertyMap().get(CACHE_KEY);
+    }
+
     protected static Class<?> getModelClassForResource(final Resource resource, final Map<String, Class<?>> map) {
         if (resource == null) {
             return null;
         }
         ResourceResolver resolver = resource.getResourceResolver();
         final String originalResourceType = resource.getResourceType();
+        if (originalResourceType.isEmpty()) {
+            return null;
+        }
+
+        Map<Map<String, Class<?>>, Map<String, Object>> cache = getModelClassCache(resolver);
+
+        if (cache == null) {
+            // Thread-safe Identity map to prevent O(N) deep equality checks on the massive map
+            cache = java.util.Collections.synchronizedMap(new java.util.IdentityHashMap<>());

Review Comment:
   > A `ResourceResolver` is not thread-safe, so this cache does not need to be thread-safe as well.
   
   You are absolutely right! 
   
   in a production environment, the `ResourceResolver` is tied to a single request and single thread, so it shouldn't need thread-safety
   
   However, during the maven build, I observed that during the integration tests (like AdapterFactoryTest), a single mocked `ResourceResolver` is actually shared across multiple threads. This contract violation in the test suite was corrupting the standard map buckets and causing the JVM to hang
   
   I added synchronizedMap purely as a workaround to keep the test suite passing
   
   If we prefer to keep the production code "pure" we can remove the synchronization and look into fixing the multi-threading in the test class instead 
   
   What do you prefer?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "bkkothari2255 (via GitHub)" <gi...@apache.org>.
bkkothari2255 commented on code in PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#discussion_r3035933698


##########
src/main/java/org/apache/sling/models/impl/AdapterImplementations.java:
##########
@@ -314,39 +316,71 @@ public Class<?> getModelClassForResource(final Resource resource) {
         return getModelClassForResource(resource, resourceTypeMappingsForResources);
     }
 
+    @SuppressWarnings("unchecked")
+    private static Map<Map<String, Class<?>>, Map<String, Object>> getModelClassCache(final ResourceResolver resolver) {
+        return (Map<Map<String, Class<?>>, Map<String, Object>>)
+                resolver.getPropertyMap().get(CACHE_KEY);
+    }
+
     protected static Class<?> getModelClassForResource(final Resource resource, final Map<String, Class<?>> map) {
         if (resource == null) {
             return null;
         }
         ResourceResolver resolver = resource.getResourceResolver();
         final String originalResourceType = resource.getResourceType();
+        if (originalResourceType.isEmpty()) {
+            return null;
+        }
+
+        Map<Map<String, Class<?>>, Map<String, Object>> cache = getModelClassCache(resolver);
+
+        if (cache == null) {
+            // Thread-safe Identity map to prevent O(N) deep equality checks on the massive map
+            cache = java.util.Collections.synchronizedMap(new java.util.IdentityHashMap<>());

Review Comment:
   > A `ResourceResolver` is not thread-safe, so this cache does not need to be thread-safe as well.
   
   Agreed! I'll encapsulate that structure into an inner class to make it much easier to reason about, and push the change shortly
   
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#issuecomment-4187684425

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [97.1% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#issuecomment-4187100842

   ## [![Quality Gate Failed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-failed-20px.png 'Quality Gate Failed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86) **Quality Gate failed**  
   Failed conditions  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/failed-16px.png '') [C Reliability Rating on New Code](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86) (required ≥ A)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86)
   
   ##   
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/light_bulb-16px.png '') Catch issues before they fail your Quality Gate with our IDE extension ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/sonarlint-16px.png '') [SonarQube for IDE](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=pull-request)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "bkkothari2255 (via GitHub)" <gi...@apache.org>.
bkkothari2255 commented on code in PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#discussion_r3035960168


##########
src/main/java/org/apache/sling/models/impl/AdapterImplementations.java:
##########
@@ -314,39 +316,71 @@ public Class<?> getModelClassForResource(final Resource resource) {
         return getModelClassForResource(resource, resourceTypeMappingsForResources);
     }
 
+    @SuppressWarnings("unchecked")
+    private static Map<Map<String, Class<?>>, Map<String, Object>> getModelClassCache(final ResourceResolver resolver) {
+        return (Map<Map<String, Class<?>>, Map<String, Object>>)
+                resolver.getPropertyMap().get(CACHE_KEY);
+    }
+
     protected static Class<?> getModelClassForResource(final Resource resource, final Map<String, Class<?>> map) {
         if (resource == null) {
             return null;
         }
         ResourceResolver resolver = resource.getResourceResolver();
         final String originalResourceType = resource.getResourceType();
+        if (originalResourceType.isEmpty()) {
+            return null;
+        }
+
+        Map<Map<String, Class<?>>, Map<String, Object>> cache = getModelClassCache(resolver);
+
+        if (cache == null) {
+            // Thread-safe Identity map to prevent O(N) deep equality checks on the massive map
+            cache = java.util.Collections.synchronizedMap(new java.util.IdentityHashMap<>());

Review Comment:
   > And a third aspect: We probably should avoid using an unbounded map, as it could lead to massive memory usage or even a memory leak.
   
   For the memory leak, I actually used `ResourceResolver.getPropertyMap()` exactly as suggested in the Jira ticket, 
   
   Since the cache dies with the HTTP request, it can't cause a long-term leak. And based on your WKND stats (~36 distinct paths), it shouldn't use much memory during the request either
   
   Let me know if you'd still prefer we use a bounded map just to be extra safe or if you think the request-scoped map is good to go



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#issuecomment-4187186920

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [96.2% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "bkkothari2255 (via GitHub)" <gi...@apache.org>.
bkkothari2255 closed pull request #86: SLING-12217 : Reduce resource lookups in Sling Model resolution
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#issuecomment-4186757566

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [82.9% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "bkkothari2255 (via GitHub)" <gi...@apache.org>.
bkkothari2255 commented on code in PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#discussion_r3035933698


##########
src/main/java/org/apache/sling/models/impl/AdapterImplementations.java:
##########
@@ -314,39 +316,71 @@ public Class<?> getModelClassForResource(final Resource resource) {
         return getModelClassForResource(resource, resourceTypeMappingsForResources);
     }
 
+    @SuppressWarnings("unchecked")
+    private static Map<Map<String, Class<?>>, Map<String, Object>> getModelClassCache(final ResourceResolver resolver) {
+        return (Map<Map<String, Class<?>>, Map<String, Object>>)
+                resolver.getPropertyMap().get(CACHE_KEY);
+    }
+
     protected static Class<?> getModelClassForResource(final Resource resource, final Map<String, Class<?>> map) {
         if (resource == null) {
             return null;
         }
         ResourceResolver resolver = resource.getResourceResolver();
         final String originalResourceType = resource.getResourceType();
+        if (originalResourceType.isEmpty()) {
+            return null;
+        }
+
+        Map<Map<String, Class<?>>, Map<String, Object>> cache = getModelClassCache(resolver);
+
+        if (cache == null) {
+            // Thread-safe Identity map to prevent O(N) deep equality checks on the massive map
+            cache = java.util.Collections.synchronizedMap(new java.util.IdentityHashMap<>());

Review Comment:
   > A `ResourceResolver` is not thread-safe, so this cache does not need to be thread-safe as well.
   
   Agreed! I'll encapsulate that structure into an inner class to make it much easier to reason about, and push the change shortly
   
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "bkkothari2255 (via GitHub)" <gi...@apache.org>.
bkkothari2255 commented on code in PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#discussion_r3035934118


##########
src/main/java/org/apache/sling/models/impl/AdapterImplementations.java:
##########
@@ -314,39 +316,71 @@ public Class<?> getModelClassForResource(final Resource resource) {
         return getModelClassForResource(resource, resourceTypeMappingsForResources);
     }
 
+    @SuppressWarnings("unchecked")
+    private static Map<Map<String, Class<?>>, Map<String, Object>> getModelClassCache(final ResourceResolver resolver) {
+        return (Map<Map<String, Class<?>>, Map<String, Object>>)
+                resolver.getPropertyMap().get(CACHE_KEY);
+    }
+
     protected static Class<?> getModelClassForResource(final Resource resource, final Map<String, Class<?>> map) {
         if (resource == null) {
             return null;
         }
         ResourceResolver resolver = resource.getResourceResolver();
         final String originalResourceType = resource.getResourceType();
+        if (originalResourceType.isEmpty()) {
+            return null;
+        }
+
+        Map<Map<String, Class<?>>, Map<String, Object>> cache = getModelClassCache(resolver);

Review Comment:
   Agreed! I'll encapsulate that structure into an inner class to make it much easier to reason about, and push the change shortly



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] SLING-12217 : Reduce resource lookups in Sling Model resolution [sling-org-apache-sling-models-impl]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #86:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/86#issuecomment-4187148758

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [93.1% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl&pullRequest=86&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl&pullRequest=86)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org