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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]