From de0acb77c59f77c0b02c46284a6d2e9b7d23b3d8 Mon Sep 17 00:00:00 2001 From: Arndt Brenschede Date: Sun, 25 Jun 2023 12:51:10 +0200 Subject: [PATCH] optimizing constant expressions in profile parsing --- .../main/java/btools/router/ProfileCache.java | 4 +- .../java/btools/router/RoutingContext.java | 10 -- .../java/btools/expressions/BExpression.java | 106 +++++++++++++++++- .../expressions/BExpressionContext.java | 12 +- 4 files changed, 117 insertions(+), 15 deletions(-) diff --git a/brouter-core/src/main/java/btools/router/ProfileCache.java b/brouter-core/src/main/java/btools/router/ProfileCache.java index 34529e4..f285196 100644 --- a/brouter-core/src/main/java/btools/router/ProfileCache.java +++ b/brouter-core/src/main/java/btools/router/ProfileCache.java @@ -91,8 +91,8 @@ public final class ProfileCache { meta.readMetaData(new File(profileDir, "lookups.dat")); - rc.expctxWay.parseFile(profileFile, "global"); - rc.expctxNode.parseFile(profileFile, "global"); + rc.expctxWay.parseFile(profileFile, "global", rc.keyValues); + rc.expctxNode.parseFile(profileFile, "global", rc.keyValues); rc.readGlobalConfig(); diff --git a/brouter-core/src/main/java/btools/router/RoutingContext.java b/brouter-core/src/main/java/btools/router/RoutingContext.java index 5d7c8f1..43bd733 100644 --- a/brouter-core/src/main/java/btools/router/RoutingContext.java +++ b/brouter-core/src/main/java/btools/router/RoutingContext.java @@ -108,16 +108,6 @@ public final class RoutingContext { public void readGlobalConfig() { BExpressionContext expctxGlobal = expctxWay; // just one of them... - - if (keyValues != null) { - // add parameter to context - for (Map.Entry e : keyValues.entrySet()) { - float f = Float.parseFloat(e.getValue()); - expctxWay.setVariableValue(e.getKey(), f, true); - expctxNode.setVariableValue(e.getKey(), f, true); - } - } - setModel(expctxGlobal._modelClass); carMode = 0.f != expctxGlobal.getVariableValue("validForCars", 0.f); diff --git a/brouter-expressions/src/main/java/btools/expressions/BExpression.java b/brouter-expressions/src/main/java/btools/expressions/BExpression.java index 5bd0ba9..a9e966e 100644 --- a/brouter-expressions/src/main/java/btools/expressions/BExpression.java +++ b/brouter-expressions/src/main/java/btools/expressions/BExpression.java @@ -32,7 +32,7 @@ final class BExpression { private BExpression op3; private float numberValue; private int variableIdx; - private int lookupNameIdx; + private int lookupNameIdx = -1; private int[] lookupValueIdxArray; // Parse the expression and all subexpression @@ -41,6 +41,50 @@ final class BExpression { } private static BExpression parse(BExpressionContext ctx, int level, String optionalToken) throws Exception { + BExpression e = parseRaw(ctx, level, optionalToken); + if (e == null) { + return null; + } + + // try to simplify the expression + if (ASSIGN_EXP == e.typ) { + ctx.lastAssignedExpression.set(e.variableIdx, e.op1); + } else if (VARIABLE_EXP == e.typ) { + BExpression ae = ctx.lastAssignedExpression.get(e.variableIdx); + if (ae != null && ae.typ == NUMBER_EXP) { + e = ae; + } + } else { + e = e.tryCollapse(); + e = e.tryEvaluateConstant(); + } + if (level == 0) { + // mark the used lookups after the + // expression is collapsed to not mark + // lookups as used that appear in the profile + // but are de-activated by constant expressions + e.markLookupIdxUsed(ctx); + } + return e; + } + + private void markLookupIdxUsed(BExpressionContext ctx) { + if (lookupNameIdx >= 0) { + ctx.markLookupIdxUsed(lookupNameIdx); + } + if (op1 != null) { + op1.markLookupIdxUsed(ctx); + } + if (op2 != null) { + op2.markLookupIdxUsed(ctx); + } + if (op3 != null) { + op3.markLookupIdxUsed(ctx); + } + } + + private static BExpression parseRaw(BExpressionContext ctx, int level, String optionalToken) throws Exception { + boolean brackets = false; String operator = ctx.parseToken(); if (optionalToken != null && optionalToken.equals(operator)) { @@ -64,6 +108,8 @@ final class BExpression { BExpression exp = new BExpression(); int nops = 3; + BExpression op1Replacement = null; + boolean ifThenElse = false; if ("switch".equals(operator)) { @@ -110,6 +156,16 @@ final class BExpression { exp.variableIdx = ctx.getVariableIdx(variable, true); if (exp.variableIdx < ctx.getMinWriteIdx()) throw new IllegalArgumentException("cannot assign to readonly variable " + variable); + + // possibly replace the value to assign by an injected value + if (ctx.keyValues != null) { + String v = ctx.keyValues.get(variable); + if (v != null) { + op1Replacement = new BExpression(); + op1Replacement.typ = NUMBER_EXP; + op1Replacement.numberValue = Float.parseFloat(v); + } + } } else if ("not".equals(operator)) { exp.typ = NOT_EXP; } else { @@ -124,7 +180,6 @@ final class BExpression { if (exp.lookupNameIdx < 0) { throw new IllegalArgumentException("unknown lookup name: " + name); } - ctx.markLookupIdxUsed(exp.lookupNameIdx); StringTokenizer tk = new StringTokenizer(values, "|"); int nt = tk.countTokens(); int nt2 = nt == 0 ? 1 : nt; @@ -177,6 +232,9 @@ final class BExpression { // parse operands if (nops > 0) { exp.op1 = BExpression.parse(ctx, level + 1, exp.typ == ASSIGN_EXP ? "=" : null); + if (op1Replacement != null) { + exp.op1 = op1Replacement; + } } if (nops > 1) { if (ifThenElse) checkExpectedToken(ctx, "then"); @@ -245,6 +303,50 @@ final class BExpression { } } + // Try to collapse the expression + // if logically possible + public BExpression tryCollapse() { + switch (typ) { + case OR_EXP: + return NUMBER_EXP == op1.typ ? + (op1.numberValue != 0.f ? op1 : op2) + : (NUMBER_EXP == op2.typ ? + (op2.numberValue != 0.f ? op2 : op1) + : this); + case AND_EXP: + return NUMBER_EXP == op1.typ ? + (op1.numberValue == 0.f ? op1 : op2) + : (NUMBER_EXP == op2.typ ? + (op2.numberValue == 0.f ? op2 : op1) + : this); + case ADD_EXP: + return NUMBER_EXP == op1.typ ? + (op1.numberValue == 0.f ? op2 : this) + : (NUMBER_EXP == op2.typ ? + (op2.numberValue == 0.f ? op1 : this) + : this); + case SWITCH_EXP: + return NUMBER_EXP == op1.typ ? + (op1.numberValue == 0.f ? op3 : op2) : this; + default: + return this; + } + } + + // Try to evaluate the expression + // if all operands are constant + public BExpression tryEvaluateConstant() { + if (op1 != null && NUMBER_EXP == op1.typ + && (op2 == null || NUMBER_EXP == op2.typ) + && (op3 == null || NUMBER_EXP == op3.typ)) { + BExpression exp = new BExpression(); + exp.typ = NUMBER_EXP; + exp.numberValue = evaluate(null); + return exp; + } + return this; + } + private float max(float v1, float v2) { return v1 > v2 ? v1 : v2; } diff --git a/brouter-expressions/src/main/java/btools/expressions/BExpressionContext.java b/brouter-expressions/src/main/java/btools/expressions/BExpressionContext.java index 2bfd2f0..727a06e 100644 --- a/brouter-expressions/src/main/java/btools/expressions/BExpressionContext.java +++ b/brouter-expressions/src/main/java/btools/expressions/BExpressionContext.java @@ -52,6 +52,9 @@ public abstract class BExpressionContext implements IByteArrayUnifier { private Map variableNumbers = new HashMap<>(); + List lastAssignedExpression = new ArrayList<>(); + Map keyValues; + private float[] variableData; @@ -768,6 +771,12 @@ public abstract class BExpressionContext implements IByteArrayUnifier { } return foreignContext.getOutputVariableIndex(name, true); } + + public void parseFile(File file, String readOnlyContext, Map keyValues) { + this.keyValues = keyValues; + parseFile(file, readOnlyContext); + this.keyValues = null; + } public void parseFile(File file, String readOnlyContext) { if (!file.exists()) { @@ -785,8 +794,8 @@ public abstract class BExpressionContext implements IByteArrayUnifier { } linenr = 1; minWriteIdx = variableData == null ? 0 : variableData.length; - expressionList = _parseFile(file); + lastAssignedExpression = null; // determine the build-in variable indices String[] varNames = getBuildInVariableNames(); @@ -857,6 +866,7 @@ public abstract class BExpressionContext implements IByteArrayUnifier { if (create) { num = variableNumbers.size(); variableNumbers.put(name, num); + lastAssignedExpression.add(null); } else { return -1; }