Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ Notes; finished-work narrative does not.
separating real parameter differences; the dispatch group key cannot be used because it embeds each
class's type variable (`void|T192,real` against `void|T636,real`). Worth doing only if this stops
being dead weight.
A separate runtime miss was reproduced on unmodified master: `Root<T:>` overloads `value(T)` and
`value(string)`, `Parent<U:> implements Root<U>` returns 10/20, `Child extends Parent<int>` uses a module
whose `value(int)` returns 42, and `Grandchild` overrides it to return 43. Calls through `Root<int>`
reach the parent implementation (10) on Lua instead of 42/43; Jass succeeds. The same hierarchy without
the module passes on both targets (`DispatchImplementationTests.boundGenericOverloadsKeepTheirDispatch`).

- **Implement `RawHashMap<K, V>` from [docs/NATIVE_KEYED_STORE_DESIGN.md](docs/NATIVE_KEYED_STORE_DESIGN.md).**
The note records the cross-backend key/value contract, existing intrinsic reuse, specialization
Expand All @@ -50,13 +55,10 @@ Notes; finished-work narrative does not.
constants to them (not a call, and a division only by a constant: those may have an effect), on both targets, in
front of the compile-time functions, before the generics and after them. On castle fight (Lua) that takes the 34,200
functions of the translation to 10,500 and the statements from 258,000 to 110,000 before the generics, and the script
has 92,000. What it leaves is the weight which is neither: 3,512 classes of which 1,611 reach the script (the
specialisation of generics adds 220 more, and the class elimination on Jass builds an allocator, a deallocator and
field arrays for every one), and the fields of the classes. Next, in this order, each its own change with its own
proof: (1) classes which no live code allocates, names in a type, or inherits from, taking care that type ids are
numbered over the classes in the program and that a dead class can be the supertype of a live one; (2) globals
has 92,000. Class-metadata pruning was tried and deferred (see Notes). Next, in this order, each its own
change with its own proof: (1) globals
and fields which only assignments with an effect write, which keeps the effect and drops the variable (what
`ImOptimizer.removeGarbage` does late); (3) the work before the IM exists, which no pass over the IM can reach:
`ImOptimizer.removeGarbage` does late); (2) the work before the IM exists, which no pass over the IM can reach:
about 2.6 s of a warm castle fight build is outside the phases the IM passes cover, and the translation takes 1.3 s
to produce 34,000 functions of which 10,500 are used. Translating only function bodies which are reachable saves
about 0.17 s of that (measured), because the cost is per class and per method (`getClassesWithImplementation`,
Expand Down Expand Up @@ -138,14 +140,6 @@ Notes; finished-work narrative does not.
about 4%). The dataflow anomaly analysis (`VarStates`, `VState`) copies its immutable maps and multimaps on every
read and write, about 7% with its merges; persistent or mutable states would remove most of that. The rest is
attribute evaluation, see the next item.
- *Translation to IM (2.4 s):* `ImTranslator.getClassesWithImplementation` is 20% of it: for each method a member
lookup in every subclass, so a hierarchy where thousands of subclasses override one method pays methods times
subclasses lookups (the overrides are distinct, not duplicates). Tried and dropped: letting a subclass which
brings no function of that name take its superclass's answer agreed with the current answer on every call
(11,727 on that map, and the class and generics suites) but changed nothing measurable, because the subclasses
which make it slow override the method themselves. What would help is a cheaper lookup per overriding subclass
(its own declaration of the name is the candidate), with the binding of type arguments (`matchDefLinkReceiver`)
coming out the same.
- **Parallel type check and per-function IM passes.** Parsing is parallel (above); the rest of a build is not, and
JFR of fresh master says where the time is (shares of the compiler thread's samples, castle fight without
optimisations / optimised / zombie defense): type check light and heavy phases (attribute evaluation, per unit and per
Expand Down Expand Up @@ -192,6 +186,17 @@ Notes; finished-work narrative does not.

## Notes

- **Do not revisit early class-metadata pruning without new evidence.** Closed PR [#1457](https://github.com/wurstscript/WurstScript/pull/1457)
removed 1,459 classes on Castle Fight but showed no consistent end-to-end speedup. On the same current input
(3,669 classes), the precise version retained 2,210 and the conservative prototype retained 3,584 on both targets,
stable across three runs per variant. Keeping inheritance families and generic classes, and skipping pruning after
allocator state exists, removed only 85 classes (2.3%) while saving 44 of 274 net added production lines (16%).
The prototype passed 705 focused tests after changing the unused-sibling retention expectation and adding two
conservative-policy tests; it was discarded. Fewer intermediate classes alone do not justify the analysis and its
type-id, compile-time state, allocator and specialization obligations. Reopen only with a representative measured
build-time or memory benefit, or evidence of a substantially smaller correct implementation; speculative future
benefits and raw class counts are insufficient.

- `%` is real modulo in Wurst; `mod` is integer modulo. `int % 8` types as `real`. `div` and `mod`
return the left operand's type, so `real r = 7 div 2` compiles and is meant to
(`ExpressionTests.integerDivisionOfLiteralsIsStillAssignableToReal`).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import de.peeeq.wurstscript.WurstOperator;
import de.peeeq.wurstscript.ast.*;
import de.peeeq.wurstscript.attributes.CompileError;
import de.peeeq.wurstscript.attributes.names.DefLink;
import de.peeeq.wurstscript.attributes.names.FuncLink;
import de.peeeq.wurstscript.attributes.names.NameLink;
import de.peeeq.wurstscript.attributes.names.NameResolution;
import de.peeeq.wurstscript.attributes.names.PackageLink;
import de.peeeq.wurstscript.jassIm.*;
import de.peeeq.wurstscript.jassIm.Element;
Expand Down Expand Up @@ -2294,20 +2296,18 @@ public List<ImFunction> getInitializationOrder() {
* returns a list of classes and functions implementing funcDef
*/
public Map<ClassDef, FuncDef> getClassesWithImplementation(Collection<ClassDef> instances, FuncDef func) {
if (func.attrIsPrivate()) {
if (instances.isEmpty() || func.attrIsPrivate()) {
// private functions cannot be overridden
return Collections.emptyMap();
}
Map<ClassDef, FuncDef> result = Maps.newLinkedHashMap();
// The declaration is already known. General member lookup also searches every enclosing scope and
// adapts all same-named overloads for each subclass; only this declaration's receiver needs binding.
FuncLink declaration = FuncLink.create(func, func.getParent().attrNearestScope()).withConfigDef();
for (ClassDef c : instances) {
FuncLink funcNameLink = null;
WurstTypeClass cType = c.attrTypC();
for (FuncLink nameLink : func.lookupMemberFuncs(cType, func.getName())) {
if (nameLink.getDef() == func) {
funcNameLink = nameLink;
}
}
if (funcNameLink == null) {
DefLink bound = NameResolution.matchDefLinkReceiver(declaration, cType, func, false);
if (!(bound instanceof FuncLink funcNameLink)) {
throw new Error("Could not find "
+ Utils.printElementWithSource(Optional.of(func))
+ " in "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package tests.wurstscript.tests;

import de.peeeq.wurstio.WurstCompilerJassImpl;
import de.peeeq.wurstscript.RunArgs;
import de.peeeq.wurstscript.ast.ClassDef;
import de.peeeq.wurstscript.ast.FuncDef;
import de.peeeq.wurstscript.ast.WurstModel;
import de.peeeq.wurstscript.gui.WurstGuiCliImpl;
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.testng.Assert.*;

public class DispatchImplementationTests extends WurstScriptTest {
@Test
public void genericInterfaceImplementationsUseTheMostSpecificDeclaration() {
String source = String.join("\n",
"package Test",
"interface Root<T:>",
" function value(T input) returns int",
"class Parent<U:> implements Root<U>",
" override function value(U input) returns int",
" return 42",
"class Child extends Parent<int>",
" override function value(int input) returns int",
" return 43",
"class Inherits extends Parent<int>");
WurstGuiCliImpl gui = new WurstGuiCliImpl();
WurstCompilerJassImpl compiler = new WurstCompilerJassImpl(null, gui, null, new RunArgs());
WurstModel model = parseFiles(Collections.emptyList(), List.of(new CU("Dispatch.wurst", source)), false, compiler);
compiler.checkProg(model);
assertTrue(gui.getErrorList().isEmpty(), gui.getErrorList().toString());
List<ClassDef> classes = new ArrayList<>();
List<FuncDef> methods = new ArrayList<>();
model.accept(new WurstModel.DefaultVisitor() {
@Override
public void visit(ClassDef c) {
super.visit(c);
classes.add(c);
}

@Override
public void visit(FuncDef f) {
super.visit(f);
methods.add(f);
}
});
FuncDef root = methods.stream().filter(f -> f.attrNearestClassDef() == null).findFirst().orElseThrow();
ImTranslator translator = new ImTranslator(model, false, new RunArgs());
var implementations = translator.getClassesWithImplementation(classes, root);
assertEquals(implementations.size(), 3);
for (ClassDef c : classes) {
String owner = c.getName().equals("Child") ? "Child" : "Parent";
assertEquals(implementations.get(c).attrNearestClassDef().getName(), owner);
}
}

@Test
public void boundGenericOverloadsKeepTheirDispatch() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package Test",
"native testSuccess()",
"interface Root<T:>",
" function value(T input) returns int",
" function value(string input) returns int",
"class Parent<U:> implements Root<U>",
" override function value(U input) returns int",
" return 10",
" override function value(string input) returns int",
" return 20",
"class Child extends Parent<int>",
" override function value(int input) returns int",
" return 42",
"class Grandchild extends Child",
" override function value(int input) returns int",
" return 43",
"init",
" Root<int> a = new Child()",
" Root<int> b = new Grandchild()",
" if a.value(1) == 42 and b.value(1) == 43 and b.value(\"x\") == 20",
" testSuccess()");
}
}
Loading