From 14e6e31de72d4a00aba806a905c984c0b003e60f Mon Sep 17 00:00:00 2001 From: Daniel Sperry Date: Wed, 8 Apr 2015 17:30:52 -0400 Subject: [PATCH] Avoiding creating bytecode for the same interface twice. Double checking the created interface from within the synchronized block. --- .../actors/runtime/ActorFactoryGenerator.java | 90 ++++++++++--------- 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/actors/core/src/main/java/com/ea/orbit/actors/runtime/ActorFactoryGenerator.java b/actors/core/src/main/java/com/ea/orbit/actors/runtime/ActorFactoryGenerator.java index f9637759d..267e122cf 100644 --- a/actors/core/src/main/java/com/ea/orbit/actors/runtime/ActorFactoryGenerator.java +++ b/actors/core/src/main/java/com/ea/orbit/actors/runtime/ActorFactoryGenerator.java @@ -26,7 +26,14 @@ */ public class ActorFactoryGenerator { - private static ClassPool classPool; + private final static ClassPool classPool; + + static + { + classPool = new ClassPool(null); + classPool.appendSystemPath(); + classPool.appendClassPath(new ClassClassPath(ActorFactoryGenerator.class)); + } private static class GenericActorFactory extends ActorFactory { @@ -101,31 +108,23 @@ public ActorFactory getFactoryFor(final Class aInterface) @SuppressWarnings("unchecked") private Class makeReferenceClass(final Class aInterface, final String interfaceFullName, final int interfaceId, final String referenceFullName) throws NotFoundException, CannotCompileException { - try + Class clazz = lookup(referenceFullName); + if (clazz != null) { - return (Class) Class.forName(referenceFullName); - } - catch (final Exception ex) - { - // ignore; - } - final ClassPool pool = getClassPool(); - try - { - return (Class) pool.getClassLoader().loadClass(referenceFullName); - } - catch (final Exception ex2) - { - // ignore; - } - - if ("ISomeChatObserver".equals(aInterface.getSimpleName())) - { - System.out.println(); + return clazz; } synchronized (aInterface) { + // trying again from within the synchronized block. + clazz = lookup(referenceFullName); + if (clazz != null) + { + return clazz; + } + + final ClassPool pool = classPool; + final CtClass cc = pool.makeClass(referenceFullName); final CtClass ccInterface = pool.get(aInterface.getName()); final CtClass ccActorReference = pool.get(ActorReference.class.getName()); @@ -159,26 +158,22 @@ private Class makeReferenceClass(final Class aInterface, final String private Class makeInvokerClass(final Class aInterface, final String invokerFullName) throws NotFoundException, CannotCompileException { - try - { - return Class.forName(invokerFullName); - } - catch (final Exception ex) + Class clazz = lookup(invokerFullName); + if (clazz != null) { - // ignore; - } - final ClassPool pool = getClassPool(); - try - { - return pool.getClassLoader().loadClass(invokerFullName); - } - catch (final Exception ex2) - { - // ignore; + return clazz; } synchronized (aInterface) { + // trying again from within the synchronized block. + clazz = lookup(invokerFullName); + if (clazz != null) + { + return clazz; + } + ClassPool pool = classPool; + final CtClass cc = pool.makeClass(invokerFullName); final CtClass ccInterface = pool.get(aInterface.getName()); final CtClass ccActorInvoker = pool.get(ActorInvoker.class.getName()); @@ -226,16 +221,25 @@ private Class makeInvokerClass(final Class aInterface, final String in } } - private static synchronized ClassPool getClassPool() + private Class lookup(String className) { - if (classPool == null) + try { - classPool = new ClassPool(null); - classPool.appendSystemPath(); - classPool.appendClassPath(new ClassClassPath(ActorFactoryGenerator.class)); + return Class.forName(className); } - - return classPool; + catch (final Exception ex) + { + // ignore; + } + try + { + return classPool.getClassLoader().loadClass(className); + } + catch (final Exception ex2) + { + // ignore; + } + return null; } }