Skip to content
Permalink
Browse files
Parallelize startup and deduplicate work at startup.
This shaves off about half a second on my system.
  • Loading branch information
LadyCailin committed Apr 29, 2020
1 parent 1e4034b commit dfa233109e839931c3be6c07ec4e1039c1167b6e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
@@ -35,10 +35,11 @@ public static void main(String[] args) {
* If -XX:+DisableExplicitGC was specified on the command line, this function respects that, and silently returns
* (otherwise we would block for quite some time waiting for a natural garbage collection to happen).
* <p>
* This overload uses a default timeout of 100ms.
* This overload uses a default timeout of 0ms, which means that it is basically equivalent to just calling
* {@code System.gc()}.
*/
public static void BlockUntilGC() {
BlockUntilGC(100);
BlockUntilGC(0);
}

/**
@@ -3,9 +3,11 @@
import com.laytonsmith.PureUtilities.Common.ArrayUtils;
import com.laytonsmith.PureUtilities.Common.StringUtils;
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.abstraction.Implementation;
import com.laytonsmith.annotations.typeof;
import com.laytonsmith.core.FullyQualifiedClassName;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.Static;
import com.laytonsmith.core.compiler.CompilerEnvironment;
import com.laytonsmith.core.environments.Environment;
import com.laytonsmith.core.exceptions.CRE.CREUnsupportedOperationException;
@@ -432,8 +434,14 @@ public CClassType[] getInterfaces() {
*/
public CClassType[] getSuperclassesForType(Environment env) {
instantiateInvalidType(env);
return Stream.of(invalidType).flatMap(e -> Stream.of(e.getSuperclasses()))
.collect(Collectors.toSet()).toArray(CClassType.EMPTY_CLASS_ARRAY);
try {
return Stream.of(invalidType).flatMap(e -> Stream.of(e.getSuperclasses()))
.collect(Collectors.toSet()).toArray(CClassType.EMPTY_CLASS_ARRAY);
} catch (NullPointerException ex) {
// There is apparently some case where this can throw an NPE. It's completely unclear
// why or how this happens, so just catch it, log details about this class, and rethrow.
throw new RuntimeException("NPE while calling getSuperclassesForType for type " + getFQCN(), ex);
}
}

/**
@@ -43,6 +43,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

@@ -169,7 +170,7 @@ public static void Cache(File extCache, Class... extraClasses) {
CommandHelperFileLocations.getDefault().getCacheDirectory());
cache.setLogger(Static.getLogger());
DynamicClassLoader dcl = new DynamicClassLoader();
ClassDiscovery cd = new ClassDiscovery();
ClassDiscovery cd = ClassDiscovery.getDefaultInstance();

cd.setClassDiscoveryCache(cache);
cd.addDiscoveryLocation(ClassDiscovery.GetClassContainer(ExtensionManager.class));
@@ -438,12 +439,12 @@ public static void Initialize(ClassDiscovery cd) {
Set<ClassMirror<?>> classes = cd.getClassesWithAnnotation(api.class);

// Temp tracking for loading messages later on.
List<String> events = new ArrayList<>();
List<String> functions = new ArrayList<>();
AtomicInteger events = new AtomicInteger(0);
AtomicInteger functions = new AtomicInteger(0);

// Loop over the classes, instantiate and register functions and events,
// and store the instances in their trackers.
classes.stream().forEach((klass) -> {
classes.parallelStream().forEach((klass) -> {
URL url = klass.getContainer();

if(cd.doesClassExtend(klass, Event.class)
@@ -465,12 +466,16 @@ public static void Initialize(ClassDiscovery cd) {
ExtensionTracker trk = EXTENSIONS.get(url);

if(trk == null) {
trk = new ExtensionTracker(url, cd, dcl);
if(trk.identifier == null) {
trk.identifier = StringUtils.replaceLast(new java.io.File(url.getPath().replaceFirst("/", ""))
.getName(), ".jar", "");
synchronized(ExtensionManager.class) {
if(trk == null) {
trk = new ExtensionTracker(url, cd, dcl);
if(trk.identifier == null) {
trk.identifier = StringUtils.replaceLast(new java.io.File(url.getPath().replaceFirst("/", ""))
.getName(), ".jar", "");
}
EXTENSIONS.put(url, trk);
}
}
EXTENSIONS.put(url, trk);
}

// Instantiate, register and store.
@@ -490,7 +495,7 @@ public static void Initialize(ClassDiscovery cd) {
}

Event e = cls.newInstance();
events.add(e.getName());
events.addAndGet(1);

trk.registerEvent(e);
} else if(Function.class.isAssignableFrom(c)) {
@@ -508,7 +513,7 @@ public static void Initialize(ClassDiscovery cd) {
}

Function f = cls.newInstance();
functions.add(f.getName());
functions.addAndGet(0);

trk.registerFunction(f);
}
@@ -524,9 +529,9 @@ public static void Initialize(ClassDiscovery cd) {
try {
if(Prefs.DebugMode()) {
StreamUtils.GetSystemOut().println(Implementation.GetServerType().getBranding()
+ ": Loaded " + functions.size() + " function" + (functions.size() == 1 ? "." : "s."));
+ ": Loaded " + functions.get() + " function" + (functions.get() == 1 ? "." : "s."));
StreamUtils.GetSystemOut().println(Implementation.GetServerType().getBranding()
+ ": Loaded " + events.size() + " event" + (events.size() == 1 ? "." : "s."));
+ ": Loaded " + events.get() + " event" + (events.get() == 1 ? "." : "s."));
}
} catch (Throwable e) {
// Prefs weren't loaded, probably caused by running tests.
@@ -48,7 +48,7 @@ public ExtensionTracker(URL container, ClassDiscovery cd, DynamicClassLoader dcl
supportedPlatforms = new HashMap<>();

for(api.Platforms p : api.Platforms.values()) {
functions.put(p, new HashMap<String, FunctionBase>());
functions.put(p, new HashMap<>());
}

this.events = new EnumMap<>(Driver.class);
@@ -93,7 +93,7 @@ public Set<FunctionBase> getFunctions() {
return retn;
}

public void registerFunction(FunctionBase f) {
public synchronized void registerFunction(FunctionBase f) {
api api = f.getClass().getAnnotation(api.class);
api.Platforms[] platforms = api.platform();

@@ -102,8 +102,8 @@ public void registerFunction(FunctionBase f) {
}

if(supportedPlatforms.get(f.getName()) == null) {
supportedPlatforms.put(f.getName(), EnumSet.noneOf(api.Platforms.class));
}
supportedPlatforms.put(f.getName(), EnumSet.noneOf(api.Platforms.class));
}

supportedPlatforms.get(f.getName()).addAll(Arrays.asList(platforms));

@@ -139,7 +139,8 @@ public Set<Event> getEvents(Driver type) {
return retn;
}

public void registerEvent(Event e) {
@SuppressWarnings("UseSpecificCatch")
public synchronized void registerEvent(Event e) {
if(e instanceof AbstractEvent) {
AbstractEvent ae = (AbstractEvent) e;
//Get the mixin for this server, and add it to e
@@ -158,7 +159,7 @@ public void registerEvent(Event e) {

//Finally, add it to the list, and hook it.
if(!events.containsKey(e.driver())) {
events.put(e.driver(), new TreeSet<Event>());
events.put(e.driver(), new TreeSet<>());
}

events.get(e.driver()).add(e);

0 comments on commit dfa2331

Please sign in to comment.