joerghoh commented on code in PR #89:
URL: 
https://github.com/apache/sling-org-apache-sling-models-impl/pull/89#discussion_r4173562017


##########
src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceCache.java:
##########
@@ -0,0 +1,284 @@
+/*
+ * 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.sling.models.impl.injectors;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.osgi.framework.AllServiceListener;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.SynchronousBundleListener;
+
+/**
+ * Caches the OSGi service lookups of the {@link OSGiServiceInjector}.
+ * <p>
+ * Without a cache every injection of an OSGi service calls {@link 
BundleContext#getServiceReferences(String, String)}
+ * and {@link BundleContext#getService(ServiceReference)} (and later {@link 
BundleContext#ungetService(ServiceReference)}
+ * from a disposal callback). Each of these calls goes through the framework 
service registry:
+ * {@code getServiceReferences} also gets, calls and releases every registered
+ * {@link org.osgi.framework.hooks.service.FindHook}, and getting / releasing 
a service updates the usage count of the
+ * service for the requesting bundle. When many models are instantiated 
concurrently, request threads contend on the
+ * service registry for every injected service.
+ * <p>
+ * This cache keeps
+ * <ul>
+ * <li>the service references per requesting bundle context, service class and 
filter, sorted by descending service
+ * ranking. All lookups of a service class are evicted on any {@link 
ServiceEvent} for a service registered under that
+ * class. Empty results are cached as well, so repeated lookups of absent 
(optional) services do not hit the registry
+ * either;</li>
+ * <li>the service objects per requesting bundle context and service 
reference. A service object is obtained once
+ * (so the requesting bundle keeps a single usage of the service, like a 
static Declarative Services reference) and
+ * released when the service is unregistered, when the requesting bundle is 
stopping or when the cache is cleared.</li>
+ * </ul>
+ */
+final class OSGiServiceCache implements AllServiceListener, 
SynchronousBundleListener {
+
+    private static final ServiceReference<?>[] NO_REFERENCES = new 
ServiceReference<?>[0];
+
+    /**
+     * Service class name &rarr; (requesting bundle context, filter) &rarr; 
references, highest ranking first.

Review Comment:
   ```suggestion
        * Service class name =>(requesting bundle context, filter) => 
references, highest ranking first.
   ```
   
   These comments are mostly read in the sourcecode directly (it's not part of 
the offical API, so no javadoc), so IMO no need to escape those. Should be 
changed in the entire PR.



-- 
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