• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 271

29 Sep 2025 12:12PM UTC coverage: 64.649% (+2.4%) from 62.222%
271

Pull #1096

circleci

Frotty
Merge branch 'perf-improvements' of https://github.com/wurstscript/WurstScript into perf-improvements
Pull Request #1096: Perf improvements

18202 of 28155 relevant lines covered (64.65%)

0.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.31
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/intermediatelang/interpreter/State.java
1
package de.peeeq.wurstscript.intermediatelang.interpreter;
2

3
import de.peeeq.wurstio.jassinterpreter.InterpreterException;
4
import de.peeeq.wurstscript.intermediatelang.ILconst;
5
import de.peeeq.wurstscript.intermediatelang.ILconstArray;
6
import de.peeeq.wurstscript.jassIm.ImArrayLikeType;
7
import de.peeeq.wurstscript.jassIm.ImArrayTypeMulti;
8
import de.peeeq.wurstscript.jassIm.ImType;
9
import de.peeeq.wurstscript.jassIm.ImVar;
10
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
11
import org.eclipse.jdt.annotation.Nullable;
12

13
import java.util.List;
14
import java.util.Map;
15

16
/**
17
 * Lazily allocates internal maps ONLY when needed.
18
 */
19
public abstract class State {
1✔
20

21
    // in State:
22
    private @Nullable Object2ObjectOpenHashMap<ImVar, ILconst> values;
23
    private @Nullable Object2ObjectOpenHashMap<ImVar, ILconstArray> arrayValues;
24

25
    private Object2ObjectOpenHashMap<ImVar, ILconst> ensureValues() {
26
        Object2ObjectOpenHashMap<ImVar, ILconst> v = values;
1✔
27
        if (v == null) {
1✔
28
            v = new Object2ObjectOpenHashMap<>(8);
1✔
29
            values = v;
1✔
30
        }
31
        return v;
1✔
32
    }
33

34
    protected Object2ObjectOpenHashMap<ImVar, ILconstArray> ensureArrayValues() {
35
        Object2ObjectOpenHashMap<ImVar, ILconstArray> a = arrayValues;
1✔
36
        if (a == null) {
1✔
37
            a = new Object2ObjectOpenHashMap<>(4);
1✔
38
            arrayValues = a;
1✔
39
        }
40
        return a;
1✔
41
    }
42

43

44
    public void setVal(ImVar v, ILconst val) {
45
        ensureValues().put(v, val);
1✔
46
    }
1✔
47

48
    public @Nullable ILconst getVal(ImVar v) {
49
        Map<ImVar, ILconst> vmap = values;
1✔
50
        return vmap == null ? null : vmap.get(v);
1✔
51
    }
52

53
    /** Returns the (lazy) array object for variable v, allocating only when first accessed. */
54
    protected ILconstArray getArray(ImVar v) {
55
        Map<ImVar, ILconstArray> amap = ensureArrayValues();
1✔
56
        ILconstArray arr = amap.get(v);
1✔
57
        if (arr == null) {
1✔
58
            arr = createArrayConstantFromType(v.getType());
1✔
59
            amap.put(v, arr);
1✔
60
        }
61
        return arr;
1✔
62
    }
63

64
    static ILconstArray createArrayConstantFromType(ImType vType) {
65
        if (!(vType instanceof ImArrayLikeType)) {
1✔
66
            throw new InterpreterException("Cannot get array for variable of type " + vType);
×
67
        }
68
        ImType componentType = ((ImArrayLikeType) vType).getEntryType();
1✔
69

70
        // Use declared first dimension if present; otherwise use "unbounded" sentinel.
71
        int size = Integer.MAX_VALUE;
1✔
72
        if (vType instanceof ImArrayTypeMulti) {
1✔
73
            List<Integer> arraySize = ((ImArrayTypeMulti) vType).getArraySize();
1✔
74
            if (!arraySize.isEmpty()) {
1✔
75
                size = arraySize.get(0);
1✔
76
            }
77
        }
78

79
        return new ILconstArray(size, componentType::defaultValue);
1✔
80
    }
81

82
    public void setArrayVal(ImVar v, List<Integer> indexes, ILconst val) {
83
        ILconstArray ar = getArray(v);
1✔
84
        for (int i = 0; i < indexes.size() - 1; i++) {
1✔
85
            ar = (ILconstArray) ar.get(indexes.get(i));
×
86
        }
87
        ar.set(indexes.get(indexes.size() - 1), val);
1✔
88
    }
1✔
89

90
    public @Nullable ILconst getArrayVal(ImVar v, List<Integer> indexes) {
91
        ILconstArray ar = getArray(v);
1✔
92
        for (int i = 0; i < indexes.size() - 1; i++) {
1✔
93
            ar = (ILconstArray) ar.get(indexes.get(i));
×
94
        }
95
        return ar.get(indexes.get(indexes.size() - 1));
1✔
96
    }
97

98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc