-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplifying ContextManagers and avoid new Lists when creating or reac…
…tivating snapshots. This should be a significant performance improvement. Signed-off-by: Sjoerd Talsma <[email protected]>
- Loading branch information
1 parent
7932f71
commit 46680b7
Showing
4 changed files
with
140 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
context-propagation-core/src/main/java/nl/talsmasoftware/context/core/ServiceCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2016-2024 Talsma ICT | ||
* | ||
* Licensed 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 nl.talsmasoftware.context.core; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Cache for resolved services. | ||
* | ||
* <p> | ||
* This is required because the ServiceLoader itself is not thread-safe due to its internal lazy iterator. | ||
*/ | ||
class ServiceCache { | ||
private static final Logger LOGGER = Logger.getLogger(ServiceCache.class.getName()); | ||
|
||
/** | ||
* Internal concurrent map as cache. | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
private static final ConcurrentMap<Class, List> CACHE = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Sometimes a single, fixed classloader may be necessary (e.g. #97) | ||
*/ | ||
private static volatile ClassLoader classLoaderOverride = null; | ||
|
||
static synchronized void useClassLoader(ClassLoader classLoader) { | ||
if (classLoaderOverride == classLoader) { | ||
LOGGER.finest(() -> "Maintaining classloader override as " + classLoader + " (unchanged)"); | ||
return; | ||
} | ||
LOGGER.fine(() -> "Updating classloader override to " + classLoader + " (was: " + classLoaderOverride + ")"); | ||
classLoaderOverride = classLoader; | ||
CACHE.clear(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
static <T> List<T> cached(Class<T> serviceClass) { | ||
return (List<T>) CACHE.computeIfAbsent(serviceClass, ServiceCache::load); | ||
} | ||
|
||
static void clear() { | ||
CACHE.clear(); | ||
} | ||
|
||
/** | ||
* Loads the service implementations of the requested type. | ||
* | ||
* <p> | ||
* This method is synchronized because ServiceLoader is not thread-safe. | ||
* Fortunately this only gets called after a cache miss, so should not affect performance. | ||
* | ||
* <p> | ||
* The returned {@code List} will be {@linkplain Collections#unmodifiableList(List) unmodifiable}. | ||
* | ||
* @param serviceType The service type to load. | ||
* @param <T> The service type to load. | ||
* @return Unmodifiable list of service implementations. | ||
*/ | ||
private synchronized static <T> List<T> load(Class<T> serviceType) { | ||
final ArrayList<T> services = new ArrayList<>(); | ||
final ServiceLoader<T> loader = classLoaderOverride == null | ||
? ServiceLoader.load(serviceType) | ||
: ServiceLoader.load(serviceType, classLoaderOverride); | ||
for (T service : loader) { | ||
services.add(service); | ||
} | ||
services.trimToSize(); | ||
// TODO debug logging | ||
return Collections.unmodifiableList(services); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters