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

wurstscript / WurstScript / 267

29 Sep 2025 09:17AM UTC coverage: 62.258% (+0.04%) from 62.222%
267

Pull #1096

circleci

web-flow
Update de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/validation/WurstValidator.java

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Pull Request #1096: Perf improvements

17484 of 28083 relevant lines covered (62.26%)

0.62 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
 * Behavior is unchanged vs. the original.
19
 */
20
public abstract class State {
1✔
21

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

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

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

44

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

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

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

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

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

80
        // IMPORTANT: relies on ILconstArray being sparse/lazy itself.
81
        // If ILconstArray actually allocates 'size' eagerly, consider a sparse implementation.
82
        return new ILconstArray(size, componentType::defaultValue);
1✔
83
    }
84

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

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

101
}
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