Skip to content

Commit c1917d1

Browse files
committed
some optimizations
1 parent 2ca5471 commit c1917d1

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,25 @@ private void replaceMethodCall(ImMethodCall mc) {
628628
ImExprs arguments = JassIm.ImExprs(receiver);
629629
arguments.addAll(mc.getArguments().removeAll());
630630

631-
ImFunction dispatch = dispatchFuncs.get(mc.getMethod());
631+
ImMethod method = mc.getMethod();
632+
// Fast path: with unchecked dispatch, a monomorphic method call can be lowered
633+
// directly to its implementation function.
634+
if (!checkedDispatch
635+
&& !method.getIsAbstract()
636+
&& method.getSubMethods().isEmpty()) {
637+
mc.replaceBy(JassIm.ImFunctionCall(
638+
mc.getTrace(),
639+
method.getImplementation(),
640+
JassIm.ImTypeArguments(),
641+
arguments,
642+
false,
643+
CallType.NORMAL));
644+
return;
645+
}
646+
647+
ImFunction dispatch = dispatchFuncs.get(method);
632648
if (dispatch == null) {
633-
throw new CompileError(mc.attrTrace().attrSource(), "Could not find dispatch for " + mc.getMethod().getName());
649+
throw new CompileError(mc.attrTrace().attrSource(), "Could not find dispatch for " + method.getName());
634650
}
635651
mc.replaceBy(JassIm.ImFunctionCall(mc.getTrace(), dispatch, JassIm.ImTypeArguments(), arguments, false, CallType.NORMAL));
636652

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,8 @@ private boolean isGlobalInitStmt(ImSet s, ImVar v) {
955955
}
956956

957957
private void collectGenericUsages(Element element) {
958+
// Cache expensive recursive submethod checks within this traversal.
959+
Map<ImMethod, Boolean> hasGenericSubmethodCache = new IdentityHashMap<>();
958960
element.accept(new Element.DefaultVisitor() {
959961
@Override
960962
public void visit(ImFunctionCall f) {
@@ -969,10 +971,19 @@ public void visit(ImMethodCall mc) {
969971
super.visit(mc);
970972
ImMethod method = mc.getMethod();
971973
boolean hasTypeArgs = !mc.getTypeArguments().isEmpty();
972-
// Interface/base dispatch methods can be non-generic but still require specialization
973-
// when they dispatch to generic implementors.
974-
boolean needsDispatchSpecialization =
975-
methodImplementationIsGeneric(method) || hasGenericSubmethodImplementation(method);
974+
boolean needsDispatchSpecialization = false;
975+
// If type args are present, specialization is unconditional, so avoid extra checks.
976+
if (!hasTypeArgs) {
977+
// Interface/base dispatch methods can be non-generic but still require specialization
978+
// when they dispatch to generic implementors.
979+
needsDispatchSpecialization = methodImplementationIsGeneric(method);
980+
if (!needsDispatchSpecialization) {
981+
needsDispatchSpecialization = hasGenericSubmethodCache.computeIfAbsent(
982+
method,
983+
EliminateGenerics.this::hasGenericSubmethodImplementation
984+
);
985+
}
986+
}
976987
if (hasTypeArgs || needsDispatchSpecialization) {
977988
dbg("COLLECT GenericMethodCall: method=" + mc.getMethod().getName() + " " + id(mc.getMethod())
978989
+ " impl=" + (mc.getMethod().getImplementation() == null ? "null" : (mc.getMethod().getImplementation().getName() + " " + id(mc.getMethod().getImplementation())))

0 commit comments

Comments
 (0)