From d5d34394db0eecb3a7f2c58c02cd977f10298851 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Sat, 23 Dec 2017 15:20:42 +0100 Subject: [PATCH] Added JIT warp-up phase --- src/main/java/org/warp/picalculator/Main.java | 4 +++ .../gui/screens/LoadingScreen.java | 3 +- .../picalculator/math/rules/RulesManager.java | 29 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/warp/picalculator/Main.java b/src/main/java/org/warp/picalculator/Main.java index 20aae58b..6a8d4f8e 100755 --- a/src/main/java/org/warp/picalculator/Main.java +++ b/src/main/java/org/warp/picalculator/Main.java @@ -25,6 +25,9 @@ public class Main { beforeStart(); new DisplayManager(screen); afterStart(); + if (screen instanceof LoadingScreen) { + ((LoadingScreen) screen).loaded = true; + } DisplayManager.INSTANCE.waitForExit(); Utils.out.println(1, "Shutdown..."); beforeShutdown(); @@ -82,6 +85,7 @@ public class Main { public void afterStart() { DisplayManager.INSTANCE.setBrightness(0.2f); RulesManager.initialize(); + RulesManager.warmUp(); } public void beforeShutdown() { diff --git a/src/main/java/org/warp/picalculator/gui/screens/LoadingScreen.java b/src/main/java/org/warp/picalculator/gui/screens/LoadingScreen.java index f9f086c8..bb807602 100755 --- a/src/main/java/org/warp/picalculator/gui/screens/LoadingScreen.java +++ b/src/main/java/org/warp/picalculator/gui/screens/LoadingScreen.java @@ -10,6 +10,7 @@ public class LoadingScreen extends Screen { public float endLoading; boolean mustRefresh = true; public float loadingTextTranslation = 0.0f; + public boolean loaded = false; public LoadingScreen() { super(); @@ -29,7 +30,7 @@ public class LoadingScreen extends Screen { loadingTextTranslation = GraphicUtils.sinDeg(endLoading * 90f) * 10f; endLoading += dt; - if (StaticVars.debugOn || endLoading >= 5f) { + if (loaded && (StaticVars.debugOn || endLoading >= 3.5f)) { DisplayManager.INSTANCE.setScreen(new MathInputScreen()); } mustRefresh = true; diff --git a/src/main/java/org/warp/picalculator/math/rules/RulesManager.java b/src/main/java/org/warp/picalculator/math/rules/RulesManager.java index a8288ce5..90ae94b8 100644 --- a/src/main/java/org/warp/picalculator/math/rules/RulesManager.java +++ b/src/main/java/org/warp/picalculator/math/rules/RulesManager.java @@ -24,9 +24,13 @@ import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; +import org.warp.picalculator.Error; import org.warp.picalculator.StaticVars; import org.warp.picalculator.Utils; +import org.warp.picalculator.math.Function; import org.warp.picalculator.math.MathContext; +import org.warp.picalculator.math.MathSolver; +import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Subtraction; import org.warp.picalculator.math.functions.Sum; import org.warp.picalculator.math.functions.SumSubtraction; @@ -73,6 +77,31 @@ public class RulesManager { e.printStackTrace(); } } + + public static void warmUp() { + ObjectArrayList uselessResult = null; + boolean uselessVariable; + for (RuleType val : RuleType.values()) { + final ObjectArrayList ruleList = rules[val.ordinal()]; + for (final Rule rule : ruleList) { + ObjectArrayList uselessResult2 = rule.execute(generateUselessExpression()); + uselessVariable = (uselessResult == null ? new ObjectArrayList<>() : uselessResult).equals(uselessResult2); + uselessResult = uselessResult2; + } + } + try { + new MathSolver(generateUselessExpression()).solveAllSteps(); + } catch (InterruptedException | Error e) { + e.printStackTrace(); + } + } + + private static Expression generateUselessExpression() { + MathContext mc = new MathContext(); + Expression expr = new Expression(mc); + expr = (Expression) expr.setParameter(new Variable(mc, 'x', V_TYPE.VARIABLE)); + return expr; + } public static void addRule(Rule rule) { rules[rule.getRuleType().ordinal()].add(rule);