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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to