• 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

51.43
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/intermediatelang/ILconstArray.java
1
package de.peeeq.wurstscript.intermediatelang;
2

3
import de.peeeq.wurstio.jassinterpreter.InterpreterException;
4
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
5
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
6

7
import java.util.Arrays;
8
import java.util.function.Supplier;
9

10
public class ILconstArray extends ILconstAbstract {
11

12
    // Sparse storage for explicit / first-touched entries.
13
    // Much lighter than TreeMap both in time and memory.
14
    private final Int2ObjectOpenHashMap<ILconst> values;
15

16
    /** Logical length / bound (can be Integer.MAX_VALUE for “unbounded”). */
17
    private final int size;
18

19
    /** Supplier for default values (e.g., nested arrays or primitive defaults). */
20
    private final Supplier<ILconst> defaultValue;
21

22
    public ILconstArray(int size, Supplier<ILconst> defaultValue) {
1✔
23
        this.size = size;
1✔
24
        this.defaultValue = defaultValue;
1✔
25
        // start tiny; will grow automatically
26
        this.values = new Int2ObjectOpenHashMap<>(0);
1✔
27
    }
1✔
28

29
    @Override
30
    public String print() {
31
        // We only need a sorted view when PRINTING for determinism.
32
        // Sorting keys here keeps runtime fast elsewhere.
33
        if (values.isEmpty()) return "[]";
×
34

35
        int[] keys = new int[values.size()];
×
36
        int k = 0;
×
37
        for (Int2ObjectMap.Entry<ILconst> e: values.int2ObjectEntrySet()) {
×
38
            keys[k++] = e.getIntKey();
×
39
        }
×
40
        Arrays.sort(keys);
×
41

42
        StringBuilder sb = new StringBuilder(8 + values.size() * 16);
×
43
        sb.append('[');
×
44
        for (int i = 0; i < keys.length; i++) {
×
45
            if (i > 0) sb.append(", ");
×
46
            int idx = keys[i];
×
47
            sb.append(idx).append(": ").append(values.get(idx));
×
48
        }
49
        sb.append(']');
×
50
        return sb.toString();
×
51
    }
52

53
    @Override
54
    public boolean isEqualTo(ILconst other) {
55
        return other == this;
×
56
    }
57

58
    public void set(int index, ILconst value) {
59
        checkIndex(index);
1✔
60
        values.put(index, value);
1✔
61
    }
1✔
62

63
    public ILconst get(int index) {
64
        checkIndex(index);
1✔
65
        ILconst v = values.get(index);
1✔
66
        if (v != null) return v;
1✔
67

68
        // First touch for this index: create and store default so that
69
        // nested writes (e.g., multi-d arrays) have a place to land.
70
        v = defaultValue.get();
1✔
71
        values.put(index, v);
1✔
72
        return v;
1✔
73
    }
74

75
    private void checkIndex(int index) {
76
        if (index < 0) {
1✔
77
            throw new InterpreterException("Array index " + index + " was negative.");
×
78
        }
79
        if (index >= size) {
1✔
80
            throw new InterpreterException("Array index " + index + " must be smaller than array size " + size);
1✔
81
        }
82
    }
1✔
83
}
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