Skip to content

Commit

Permalink
Performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Oct 15, 2013
1 parent de55343 commit d35c2f9
Showing 1 changed file with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.jboss.forge.furnace.container.cdi.impl;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
Expand All @@ -24,7 +28,7 @@

public class ServiceRegistryImpl implements ServiceRegistry
{
private final Set<Class<?>> services;
private final Class<?>[] services;

private final BeanManager manager;

Expand All @@ -34,6 +38,12 @@ public class ServiceRegistryImpl implements ServiceRegistry

private final LockManager lock;

private Set<Class<?>> servicesSet;

private ClassLoader addonClassLoader;

private Map<String, Class<?>> classCache = new WeakHashMap<String, Class<?>>();

public ServiceRegistryImpl(LockManager lock, Addon addon, BeanManager manager,
Set<Class<?>> services)
{
Expand All @@ -43,7 +53,11 @@ public ServiceRegistryImpl(LockManager lock, Addon addon, BeanManager manager,
// Copy set to avoid any reference pointers
Set<Class<?>> copy = new LinkedHashSet<Class<?>>();
copy.addAll(services);
this.services = Collections.unmodifiableSet(copy);
this.services = new ArrayList<Class<?>>(copy).toArray(new Class<?>[copy.size()]);
this.servicesSet = Collections.unmodifiableSet(new LinkedHashSet<Class<?>>(Arrays.asList(this.services)));

// Extracted for performance optimization
this.addonClassLoader = addon.getClassLoader();
}

@Override
Expand Down Expand Up @@ -197,8 +211,9 @@ public <T> Set<ExportedInstance<T>> getExportedInstances(Class<T> requestedType)
return result;
}

for (Class<?> type : services)
for (int i = 0; i < services.length; i++)

This comment has been minimized.

Copy link
@gastaldi

gastaldi Oct 15, 2013

Member

Enhanced-for works with arrays also ;)

This comment has been minimized.

Copy link
@lincolnthree

lincolnthree Oct 16, 2013

Author Member

Yeah I think this can be restored to enhanced now that the target is an array instead of a collection.

{
Class<?> type = services[i];
if (requestedLoadedType.isAssignableFrom(type))
{
Set<Bean<?>> beans = manager.getBeans(type, getQualifiersFrom(type));
Expand All @@ -222,7 +237,7 @@ public <T> Set<ExportedInstance<T>> getExportedInstances(Class<T> requestedType)
@Override
public Set<Class<?>> getExportedTypes()
{
return services;
return servicesSet;
}

@Override
Expand All @@ -239,13 +254,13 @@ public <T> Set<Class<T>> getExportedTypes(Class<T> type)
}

/**
* Ensures that the returned class is loaded from the {@link Addon}
* Ensures that the returned class is loaded from this {@link Addon}
*/
@SuppressWarnings("unchecked")
private <T> Class<T> loadAddonClass(Class<T> actualType) throws ClassNotFoundException
{
final Class<T> type;
if (actualType.getClassLoader() == addon.getClassLoader())
if (actualType.getClassLoader() == addonClassLoader)
{
type = actualType;
}
Expand All @@ -258,7 +273,16 @@ private <T> Class<T> loadAddonClass(Class<T> actualType) throws ClassNotFoundExc

private Class<?> loadAddonClass(String className) throws ClassNotFoundException
{
return Class.forName(className, true, addon.getClassLoader());
Class<?> cached = classCache.get(className);
if (cached == null)
{
Class<?> result = Class.forName(className, false, addonClassLoader);
// potentially not thread-safe
classCache.put(className, result);
cached = result;
}

return cached;
}

@Override
Expand Down

0 comments on commit d35c2f9

Please sign in to comment.