From 0c54ffd8136966ba9dcc379a2e9faa09fac30b78 Mon Sep 17 00:00:00 2001
From: Pieter12345
Date: Fri, 10 Nov 2023 01:07:39 +0100
Subject: [PATCH] Stacktrace manager optimization
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()`).
---
.../laytonsmith/core/constructs/CClosure.java | 11 ++++++++---
.../core/environments/GlobalEnv.java | 17 ++++++++---------
2 files changed, 16 insertions(+), 12 deletions(-)
diff --git a/src/main/java/com/laytonsmith/core/constructs/CClosure.java b/src/main/java/com/laytonsmith/core/constructs/CClosure.java
index efc7dc806..db0c16ebf 100644
--- a/src/main/java/com/laytonsmith/core/constructs/CClosure.java
+++ b/src/main/java/com/laytonsmith/core/constructs/CClosure.java
@@ -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("<>", 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("<>", getTarget()));
+ try {
CArray arguments = new CArray(node.getData().getTarget());
CArray vararg = null;
CClassType varargType = null;
diff --git a/src/main/java/com/laytonsmith/core/environments/GlobalEnv.java b/src/main/java/com/laytonsmith/core/environments/GlobalEnv.java
index 7ad8e17bc..fcc2d79f6 100644
--- a/src/main/java/com/laytonsmith/core/environments/GlobalEnv.java
+++ b/src/main/java/com/laytonsmith/core/environments/GlobalEnv.java
@@ -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;
/**
@@ -54,7 +53,8 @@ public class GlobalEnv implements Environment.EnvironmentImpl, Cloneable {
private BoundEvent.ActiveEvent event = null;
private boolean interrupt = false;
private final List arrayAccessList = Collections.synchronizedList(new ArrayList<>());
- private final WeakHashMap stackTraceManagers = new WeakHashMap<>();
+ private Thread stackTraceManagerThread = null;
+ private StackTraceManager stackTraceManager = null;
private final MutableObject