[FELIX-3609] Optimization: keep hooks sorted so that access is faster as they don't need to be sorted again
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1365509 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java b/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
index c21ba5b..ba7e5e5 100644
--- a/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
+++ b/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
@@ -64,8 +64,8 @@
org.osgi.service.url.URLStreamHandlerService.class,
java.net.ContentHandler.class
};
- private final Map<Class<?>, Set<ServiceReference<?>>> m_allHooks =
- new HashMap<Class<?>, Set<ServiceReference<?>>>();
+ private final Map<Class<?>, TreeSet<ServiceReference<?>>> m_allHooks =
+ new HashMap<Class<?>, TreeSet<ServiceReference<?>>>();
public ServiceRegistry(Logger logger, ServiceRegistryCallbacks callbacks)
{
@@ -701,10 +701,10 @@
{
synchronized (m_allHooks)
{
- Set<ServiceReference<?>> hooks = m_allHooks.get(hookClass);
+ TreeSet<ServiceReference<?>> hooks = m_allHooks.get(hookClass);
if (hooks == null)
{
- hooks = new HashSet<ServiceReference<?>>();
+ hooks = new TreeSet<ServiceReference<?>>(Collections.reverseOrder());
m_allHooks.put(hookClass, hooks);
}
hooks.add(ref);
@@ -743,19 +743,18 @@
{
synchronized (m_allHooks)
{
- Set<ServiceReference<?>> hooks = m_allHooks.get(hookClass);
+ TreeSet<ServiceReference<?>> hooks = m_allHooks.get(hookClass);
if (hooks != null)
{
- SortedSet sorted = new TreeSet<ServiceReference<?>>(Collections.reverseOrder());
- sorted.addAll(hooks);
+ SortedSet<ServiceReference<?>> sorted = (SortedSet<ServiceReference<?>>) hooks.clone();
return asTypedSortedSet(sorted);
}
- return Collections.EMPTY_SET;
+ return Collections.emptySet();
}
}
private static <S> SortedSet<ServiceReference<S>> asTypedSortedSet(
- SortedSet<ServiceReference<?>> ss)
+ SortedSet<ServiceReference<?>> ss)
{
return (SortedSet<ServiceReference<S>>) (SortedSet) ss;
}