Skip to content

Commit

Permalink
perf(geckolib): remove a bunch of string splitting and string creatio…
Browse files Browse the repository at this point in the history
…n at render time
  • Loading branch information
bernie-g committed Mar 9, 2024
1 parent 7c0642a commit 1b292b2
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 36 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/eliotlash/molang/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.eliotlash.molang.lexer.Keyword;
import com.eliotlash.molang.lexer.Token;
import com.eliotlash.molang.lexer.TokenType;
import com.eliotlash.molang.utils.ParserUtils;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -357,7 +358,7 @@ else if (match(DOT)) {

private Expr.Struct finishStruct(Expr.Struct parentStruct) {
if(match(IDENTIFIER)) {
Expr.Struct child = new Expr.Struct(new Expr.Variable(previous().lexeme()), parentStruct, new ArrayList<>());
Expr.Struct child = new Expr.Struct(ParserUtils.createVariableFromString(previous().lexeme()), parentStruct, new ArrayList<>());
parentStruct.children().add(child);
if(!match(DOT)) return child;
parentStruct = finishStruct(child);
Expand Down Expand Up @@ -390,7 +391,7 @@ private Expr primary() {
throw error(previous(), "Variable names must be lowercase.");
}
Expr constant = constants.get(lexeme);
return constant == null ? new Expr.Variable(lexeme) : constant;
return constant == null ? ParserUtils.createVariableFromString(lexeme) : constant;
}

if (match(NUMERAL)) {
Expand Down Expand Up @@ -483,4 +484,5 @@ private Token peekNext() {
private Token previous() {
return input.get(current - 1);
}

}
6 changes: 3 additions & 3 deletions src/main/java/com/eliotlash/molang/ast/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Double visitAccess(Expr.Access expr) {
return context.functionScopedArguments.getDouble(expr);
}
if (expr.target() instanceof Expr.Variable variable) {
RuntimeVariable cachedVariable = context.getCachedVariable(variable.name() + "." + expr.member());
RuntimeVariable cachedVariable = context.getCachedVariable(variable.flavor(), expr.member());
if (context.getVariableMap().containsKey(cachedVariable)) {
return context.getVariableMap().getDouble(cachedVariable);
}
Expand All @@ -121,7 +121,7 @@ public Double visitAssignment(Expr.Assignment expr) {
if (expr.variable() instanceof Expr.Access access) {
if (access.target() instanceof Expr.Variable target) {
context.assignableMap.put(access, value);
context.parseRuntimeVariable(target.name() + "." + access.member(), access);
context.parseRuntimeVariable(target.flavor(), target.name(), access);
}
else if (access.target() instanceof Expr.Struct struct) {
context.getStructMap().put(struct, value);
Expand Down Expand Up @@ -214,7 +214,7 @@ public Double visitSwitchContext(Expr.SwitchContext expr) {

@Override
public Double visitVariable(Expr.Variable expr) {
RuntimeVariable runtimeVariable = context.getCachedVariable(expr.name());
RuntimeVariable runtimeVariable = context.getCachedVariable(expr.flavor(), expr.name());
return context.getVariableMap().getOrDefault(runtimeVariable, 0);
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/eliotlash/molang/ast/Expr.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.eliotlash.molang.ast;

import com.eliotlash.molang.variables.VariableFlavor;

import java.util.List;

public interface Expr {
Expand Down Expand Up @@ -188,9 +190,10 @@ public <R> R accept(Visitor<R> visitor) {
}

/**
* some_identifier
* @param flavor optional
* @param name
*/
record Variable(String name) implements Expr, Accessible {
record Variable(VariableFlavor flavor, String name) implements Expr, Accessible {

@Override
public <R> R accept(Visitor<R> visitor) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/eliotlash/molang/utils/ParserUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.eliotlash.molang.utils;

import com.eliotlash.molang.ast.Expr;
import com.eliotlash.molang.variables.VariableFlavor;

public class ParserUtils {
public static Expr.Variable createVariableFromString(String string) {
String[] split = string.split("\\.", 2);
if (split.length == 1) {
return new Expr.Variable(null, split[0]);
} else {
return new Expr.Variable(VariableFlavor.parse(split[0]), split[1]);
}
}
}
30 changes: 12 additions & 18 deletions src/main/java/com/eliotlash/molang/variables/ExecutionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import com.eliotlash.molang.functions.utility.Random;
import com.eliotlash.molang.utils.MolangUtils;
import it.unimi.dsi.fastutil.Pair;
import com.eliotlash.molang.utils.ParserUtils;
import it.unimi.dsi.fastutil.objects.Object2DoubleMap;
import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap;

Expand Down Expand Up @@ -65,22 +65,16 @@ public Object2DoubleMap<Expr.Struct> getStructMap() {
/**
* Call with "query.something"
*/
public RuntimeVariable getCachedVariable(String var) {
return variableCache.computeIfAbsent(var, name -> {
RuntimeVariable runtimeVariable = parseRuntimeVariable(name, null);
return runtimeVariable;
});
public RuntimeVariable getCachedVariable(VariableFlavor flavor, String var) {
return variableCache.computeIfAbsent(var, name -> parseRuntimeVariable(flavor, name, null));
}

public RuntimeVariable parseRuntimeVariable(String name, Expr.Access access) {
String[] split = name.split("\\.", 2);

VariableFlavor flavor = VariableFlavor.parse(split[0]);
public RuntimeVariable parseRuntimeVariable(VariableFlavor flavor, String variableName, Expr.Access access) {
RuntimeVariable runtimeVariable;
if (split.length == 2 && flavor != null) {
runtimeVariable = new RuntimeVariable(split[1], flavor);
if (flavor != null) {
runtimeVariable = new RuntimeVariable(flavor, variableName);
} else {
runtimeVariable = new RuntimeVariable(name, null);
runtimeVariable = new RuntimeVariable(null, variableName);
}

if (access != null) {
Expand All @@ -95,13 +89,13 @@ public RuntimeVariable parseRuntimeVariable(String name, Expr.Access access) {
return runtimeVariable;
}

public void setVariable(String var, double value) {
RuntimeVariable cachedVariable = getCachedVariable(var);
public void setVariable(VariableFlavor flavor, String var, double value) {
RuntimeVariable cachedVariable = getCachedVariable(flavor, var);
variableMap.put(cachedVariable, value);
}

public void setVariable(String var, boolean val) {
setVariable(var, MolangUtils.booleanToFloat(val));
public void setVariable(VariableFlavor flavor, String variableName, boolean val) {
setVariable(flavor, variableName, MolangUtils.booleanToFloat(val));
}

public void registerFunction(String target, Function function) {
Expand All @@ -124,7 +118,7 @@ public Function getFunction(FunctionDefinition definition) {
}

private static FunctionDefinition asFunctionDefinition(String target, Function function) {
return new FunctionDefinition(new Expr.Variable(target), function.getName());
return new FunctionDefinition(ParserUtils.createVariableFromString(target), function.getName());
}

private static void addFunction(Map<FunctionDefinition, Function> map, String target, Function func) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eliotlash.molang.variables;

public record RuntimeVariable(String name, VariableFlavor flavor) {
public record RuntimeVariable(VariableFlavor flavor, String name) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public enum VariableFlavor {
/**
* Debugging purposes
*/
READOUT("readout", "r");
READOUT("readout", "r"),

LUNAR("lunar", "l");

public final String name;
public final String alias;
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/eliotlash/molang/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.eliotlash.molang.ast.Expr;
import com.eliotlash.molang.ast.Operator;
import com.eliotlash.molang.ast.Stmt;
import com.eliotlash.molang.utils.ParserUtils;

import java.util.List;

Expand All @@ -28,7 +29,7 @@ protected static Expr.Constant c(double constant) {
}

protected static Expr.Variable v(String var) {
return new Expr.Variable(var);
return ParserUtils.createVariableFromString(var);
}

protected static Stmt.Expression s(Expr expr) {
Expand Down
18 changes: 10 additions & 8 deletions src/test/java/com/eliotlash/molang/ast/EvaluatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.util.stream.Stream;

import com.eliotlash.molang.Molang;
import com.eliotlash.molang.utils.ParserUtils;
import com.eliotlash.molang.variables.ExecutionContext;
import com.eliotlash.molang.variables.VariableFlavor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -64,17 +66,17 @@ void visitGroup() {

@Test
void visitVariable() {
assertEquals(0, eval.visitVariable(new Expr.Variable("query.test")));
assertEquals(0, eval.visitVariable(ParserUtils.createVariableFromString("query.test")));

ExecutionContext context = new ExecutionContext(eval);
eval.setExecutionContext(context);
context.setVariable("q.test", 5);
context.setVariable("query.me", 5);
context.setVariable("hello", 5);
assertEquals(5, eval.visitVariable(new Expr.Variable("query.test")));
assertEquals(5, eval.visitVariable(new Expr.Variable("q.test")));
assertEquals(5, eval.visitVariable(new Expr.Variable("q.me")));
assertEquals(5, eval.visitVariable(new Expr.Variable("hello")));
context.setVariable(VariableFlavor.QUERY, "test", 5);
context.setVariable(VariableFlavor.QUERY, "me", 5);
context.setVariable(null, "hello", 5);
assertEquals(5, eval.visitVariable(ParserUtils.createVariableFromString("query.test")));
assertEquals(5, eval.visitVariable(ParserUtils.createVariableFromString("q.test")));
assertEquals(5, eval.visitVariable(ParserUtils.createVariableFromString("q.me")));
assertEquals(5, eval.visitVariable(ParserUtils.createVariableFromString("hello")));
}

@Test
Expand Down

0 comments on commit 1b292b2

Please sign in to comment.