Skip to content

Commit

Permalink
Stacktrace manager optimization
Browse files Browse the repository at this point in the history
Store `StacktTraceManager` directly in `GlobalEnv` to prevent a synchronized hashmap lookup on every `eval()` and `seval()` call.
Move `StackTraceManager` getter in `CClosure` to post-environment-clone to ensure that it does not overwrite the `StackTraceManager` of the original thread when called from a new thread(by for example `x_new_thread()`).
  • Loading branch information
Pieter12345 committed Nov 11, 2023
1 parent 97c0882 commit 0c54ffd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
11 changes: 8 additions & 3 deletions src/main/java/com/laytonsmith/core/constructs/CClosure.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,18 @@ protected void execute(Mixed... values) throws ConfigRuntimeException, ProgramFl
if(node == null) {
return;
}
StackTraceManager stManager = env.getEnv(GlobalEnv.class).GetStackTraceManager();
stManager.addStackTraceElement(new ConfigRuntimeException.StackTraceElement("<<closure>>", getTarget()));
Environment environment;
try {
Environment environment;
synchronized(this) {
environment = env.clone();
}
} catch (CloneNotSupportedException ex) {
Logger.getLogger(CClosure.class.getName()).log(Level.SEVERE, null, ex);
return;
}
StackTraceManager stManager = environment.getEnv(GlobalEnv.class).GetStackTraceManager();
stManager.addStackTraceElement(new ConfigRuntimeException.StackTraceElement("<<closure>>", getTarget()));
try {
CArray arguments = new CArray(node.getData().getTarget());
CArray vararg = null;
CClassType varargType = null;
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/com/laytonsmith/core/environments/GlobalEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand All @@ -54,7 +53,8 @@ public class GlobalEnv implements Environment.EnvironmentImpl, Cloneable {
private BoundEvent.ActiveEvent event = null;
private boolean interrupt = false;
private final List<Iterator> arrayAccessList = Collections.synchronizedList(new ArrayList<>());
private final WeakHashMap<Thread, StackTraceManager> stackTraceManagers = new WeakHashMap<>();
private Thread stackTraceManagerThread = null;
private StackTraceManager stackTraceManager = null;
private final MutableObject<Map<String, Mixed>> runtimeSettings
= new MutableObject<>(new ConcurrentHashMap<>());
private FileOptions fileOptions;
Expand Down Expand Up @@ -155,6 +155,8 @@ public EnvironmentImpl clone() throws CloneNotSupportedException {
} else if(!cloneVars) {
clone.iVariableList = new IVariableList(clone.iVariableList);
}
clone.stackTraceManager = this.stackTraceManager;
clone.stackTraceManagerThread = this.stackTraceManagerThread;
return clone;
}

Expand Down Expand Up @@ -344,14 +346,11 @@ public List<Iterator> GetArrayAccessIteratorsFor(ArrayAccess array) {
*/
public StackTraceManager GetStackTraceManager() {
Thread currentThread = Thread.currentThread();
synchronized(stackTraceManagers) {
StackTraceManager manager = stackTraceManagers.get(currentThread);
if(manager == null) {
manager = new StackTraceManager();
stackTraceManagers.put(currentThread, manager);
}
return manager;
if(this.stackTraceManager == null || currentThread != this.stackTraceManagerThread) {
this.stackTraceManager = new StackTraceManager();
this.stackTraceManagerThread = currentThread;
}
return this.stackTraceManager;
}

/**
Expand Down

0 comments on commit 0c54ffd

Please sign in to comment.