Skip to content

Commit e721910

Browse files
committed
fix OOM
1 parent 67d0805 commit e721910

6 files changed

Lines changed: 26 additions & 8 deletions

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/WurstCompilerJassImpl.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,9 @@ private void beginPhase(int phase, String description) {
645645

646646
private void printDebugImProg(String debugFile) {
647647
if (!errorHandler.isUnitTestMode() || !errorHandler.isOutputTestSource()) {
648-
// output only in unit test mode
649648
return;
650649
}
651-
652650
try {
653-
// TODO remove test output
654651
File file = new File(debugFile);
655652
file.getParentFile().mkdirs();
656653
try (Writer w = Files.newWriter(file, Charsets.UTF_8)) {

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/ErrorHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class ErrorHandler {
1919

2020
private final WurstGui gui;
2121
private boolean unitTestMode = false;
22-
public static boolean outputTestSource = true;
22+
/** Write intermediate IM debug files during tests. Off by default — only tests that
23+
* explicitly assert on IM output (e.g. DeterministicChecks) should set this to true. */
24+
public static boolean outputTestSource = false;
2325

2426
public ErrorHandler(WurstGui gui) {
2527
this.gui = gui;

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/HasAnnotation.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
import java.util.HashMap;
99
import java.util.Map;
10+
import java.util.WeakHashMap;
1011

1112
public class HasAnnotation {
12-
// OPTIMIZATION 1: Cache normalized annotations
13+
// OPTIMIZATION 1: Cache normalized annotations (String→String: safe as static, strings are interned/small)
1314
private static final Map<String, String> normalizationCache = new HashMap<>();
1415

1516
@NotNull
@@ -74,11 +75,19 @@ public static Annotation getAnnotation(NameDef e, String annotation) {
7475
return null;
7576
}
7677

77-
// OPTIMIZATION 8: Cache normalized annotation types per Annotation object
78-
private static final Map<Annotation, String> annotationTypeCache = new HashMap<>();
78+
// OPTIMIZATION 8: Cache normalized annotation types per Annotation object.
79+
// WeakHashMap: entries are collected automatically when the AST node (Annotation) is GC'd,
80+
// preventing this static cache from pinning entire compilation trees across tests.
81+
private static final Map<Annotation, String> annotationTypeCache = new WeakHashMap<>();
7982

8083
private static String getNormalizedType(Annotation a) {
8184
return annotationTypeCache.computeIfAbsent(a,
8285
ann -> normalizeAnnotation(ann.getAnnotationType()));
8386
}
87+
88+
/** Explicitly clear both caches. Called from GlobalCaches.clearAll() between tests. */
89+
public static void clearCaches() {
90+
annotationTypeCache.clear();
91+
normalizationCache.clear();
92+
}
8493
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtranslation/LuaNativeLowering.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ private static boolean hasHandleParam(ImFunction f) {
298298
}
299299

300300
/** Returns true for WC3 handle types (ImSimpleType that is not int/real/boolean/string). */
301-
static boolean isHandleType(ImType type) {
301+
public static boolean isHandleType(ImType type) {
302302
if (!(type instanceof ImSimpleType)) {
303303
return false;
304304
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/validation/GlobalCaches.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package de.peeeq.wurstscript.validation;
22

33
import de.peeeq.wurstscript.ast.Element;
4+
import de.peeeq.wurstscript.attributes.HasAnnotation;
45
import de.peeeq.wurstscript.intermediatelang.ILconst;
56
import de.peeeq.wurstscript.intermediatelang.interpreter.LocalState;
67
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
@@ -142,6 +143,7 @@ public static void clearAll() {
142143
LOCAL_STATE_CACHE.clear();
143144
LOCAL_STATE_NOARG_CACHE.clear();
144145
lookupCache.clear();
146+
HasAnnotation.clearCaches();
145147
}
146148

147149
public enum LookupType {

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/WurstScriptTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import de.peeeq.wurstscript.utils.Utils;
3131
import de.peeeq.wurstscript.validation.GlobalCaches;
3232
import org.testng.Assert;
33+
import org.testng.annotations.AfterMethod;
3334
import org.testng.annotations.BeforeMethod;
3435

3536
import java.io.*;
@@ -66,6 +67,13 @@ public void _clearBefore() {
6667
GlobalCaches.clearAll();
6768
}
6869

70+
@AfterMethod(alwaysRun = true)
71+
public void _clearAfter() {
72+
// Release all AST strong-refs held by caches so the GC can reclaim the
73+
// stdlib copy from the previous test before the next test allocates its own.
74+
GlobalCaches.clearAll();
75+
}
76+
6977
class TestConfig {
7078
private final String name;
7179
private boolean withStdLib;

0 commit comments

Comments
 (0)