Skip to content

Commit

Permalink
perf: dont compile for empty expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
yusshu committed Dec 28, 2023
1 parent bc6166b commit 5efc56a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/java/team/unnamed/mocha/runtime/MochaFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package team.unnamed.mocha.runtime;

import org.jetbrains.annotations.NotNull;
import team.unnamed.mocha.runtime.compiled.MochaCompiledFunction;

/**
Expand All @@ -32,6 +33,10 @@
*/
@FunctionalInterface
public interface MochaFunction extends MochaCompiledFunction {
static @NotNull MochaFunction nop() {
return NopMochaFunctionHolder.NOP;
}

/**
* Evaluates this function.
*
Expand All @@ -40,3 +45,7 @@ public interface MochaFunction extends MochaCompiledFunction {
*/
double evaluate();
}

final class NopMochaFunctionHolder {
static final MochaFunction NOP = () -> 0D;
}
6 changes: 6 additions & 0 deletions src/main/java/team/unnamed/mocha/runtime/MolangCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public MolangCompiler(final @NotNull ClassLoader classLoader, final @NotNull Glo
requireNonNull(expressions, "expressions");
requireNonNull(clazz, "clazz");

if (clazz == MochaFunction.class && expressions.isEmpty()) {
// no expressions and the target type is MochaFunction,
// we know the NOP function
return clazz.cast(MochaFunction.nop());
}

if (!clazz.isInterface()) {
throw new IllegalArgumentException("Target type must be an interface: " + clazz.getName());
}
Expand Down

0 comments on commit 5efc56a

Please sign in to comment.