Skip to content

Commit

Permalink
Merge pull request #7 from forestbelton/parallel-compile
Browse files Browse the repository at this point in the history
Parallelize class loading
  • Loading branch information
Jacob Rosenberg committed Mar 17, 2016
2 parents 9dcc86d + b45216b commit a6d97a0
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions framework/src/play/classloading/ApplicationClassloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Class;
import java.lang.annotation.Annotation;
import java.lang.instrument.ClassDefinition;
import java.net.MalformedURLException;
Expand All @@ -23,6 +24,10 @@
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import play.Logger;
import play.Play;
Expand Down Expand Up @@ -390,8 +395,8 @@ int computePathHash() {
* @return The list of well defined Class
*/
public List<Class> getAllClasses() {
if (allClasses == null) {
allClasses = new ArrayList<Class>();
if (!allClassesInitialized) {
allClassesInitialized = true;

if (Play.usePrecompiled) {

Expand Down Expand Up @@ -427,11 +432,35 @@ public List<Class> getAllClasses() {

}

for (ApplicationClass applicationClass : Play.classes.all()) {
Class clazz = loadApplicationClass(applicationClass.name);
if (clazz != null) {
allClasses.add(clazz);
final int cores = Runtime.getRuntime().availableProcessors() + 1;
final ExecutorService executor = Executors.newFixedThreadPool(cores);

Logger.info("Using %d cores.", cores);

for (final ApplicationClass applicationClass : Play.classes.all()) {
executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() {
final Class clazz = loadApplicationClass(applicationClass.name);

if (clazz != null) {
synchronized (allClasses) {
allClasses.add(clazz);
}
}

return true;
}
});
}

executor.shutdown();
try {
while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
Logger.trace("Waiting for class enhancer threads to complete, sleeping 1s...");
}
} catch (Exception e) {
Logger.error(e, "Class enhancer thread failed!");
}

Collections.sort(allClasses, new Comparator<Class>() {
Expand All @@ -444,7 +473,8 @@ public int compare(Class o1, Class o2) {
}
return allClasses;
}
List<Class> allClasses = null;
boolean allClassesInitialized = false;
final List<Class> allClasses = new ArrayList<Class>();

/**
* Retrieve all application classes assignable to this class.
Expand Down

0 comments on commit a6d97a0

Please sign in to comment.