From facb9c213f53d7ae4467c92605a4d08196dae4f1 Mon Sep 17 00:00:00 2001 From: catbref Date: Mon, 10 Aug 2020 14:01:58 +0100 Subject: [PATCH] Convert logging to lambda-based to reduce CPU load if logging disabled, also bump to v1.3.6 as interface changed --- Java/pom.xml | 2 +- Java/src/main/java/org/ciyam/at/AtLogger.java | 8 +++++++ .../main/java/org/ciyam/at/FunctionCode.java | 2 +- .../main/java/org/ciyam/at/MachineState.java | 23 +++++++++---------- .../java/org/ciyam/at/test/TestLogger.java | 17 ++++++++++++++ 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Java/pom.xml b/Java/pom.xml index 9930c16..58aa1a7 100644 --- a/Java/pom.xml +++ b/Java/pom.xml @@ -4,7 +4,7 @@ 4.0.0 org.ciyam AT - 1.3.5 + 1.3.6 jar true diff --git a/Java/src/main/java/org/ciyam/at/AtLogger.java b/Java/src/main/java/org/ciyam/at/AtLogger.java index d8a0f41..0f14085 100644 --- a/Java/src/main/java/org/ciyam/at/AtLogger.java +++ b/Java/src/main/java/org/ciyam/at/AtLogger.java @@ -1,11 +1,19 @@ package org.ciyam.at; +import java.util.function.Supplier; + public interface AtLogger { void error(final String message); + void error(final Supplier messageSupplier); + void debug(final String message); + void debug(final Supplier messageSupplier); + void echo(final String message); + void echo(final Supplier messageSupplier); + } diff --git a/Java/src/main/java/org/ciyam/at/FunctionCode.java b/Java/src/main/java/org/ciyam/at/FunctionCode.java index 56ce9af..33a17e1 100644 --- a/Java/src/main/java/org/ciyam/at/FunctionCode.java +++ b/Java/src/main/java/org/ciyam/at/FunctionCode.java @@ -1068,7 +1068,7 @@ public enum FunctionCode { if (functionData.paramCount == 2 && functionData.value2 == null) throw new IllegalFunctionCodeException("Passed value2 is null but function has paramCount of (" + this.paramCount + ")"); - state.getLogger().debug("Function \"" + this.name() + "\""); + state.getLogger().debug(() -> String.format("Function \"%s\"", this.name())); postCheckExecute(functionData, state, rawFunctionCode); } diff --git a/Java/src/main/java/org/ciyam/at/MachineState.java b/Java/src/main/java/org/ciyam/at/MachineState.java index c7fae61..46ee345 100644 --- a/Java/src/main/java/org/ciyam/at/MachineState.java +++ b/Java/src/main/java/org/ciyam/at/MachineState.java @@ -675,18 +675,17 @@ public class MachineState { // Pre-execution checks if (this.isFinished) { - logger.debug("Not executing as already finished!"); + logger.debug(() -> "Not executing as already finished!"); return; } if (this.isFrozen && this.currentBalance <= this.frozenBalance) { - logger.debug("Not executing as current balance [" + this.currentBalance + "] hasn't increased since being frozen at [" + this.frozenBalance + "]"); + logger.debug(() -> String.format("Not executing as current balance [%d] hasn't increased since being frozen at [%d]", this.currentBalance, this.frozenBalance)); return; } if (this.isSleeping && this.sleepUntilHeight != null && this.currentBlockHeight < this.sleepUntilHeight) { - logger.debug("Not executing as current block height [" + this.currentBlockHeight + "] hasn't reached sleep-until block height [" - + this.sleepUntilHeight + "]"); + logger.debug(() -> String.format("Not executing as current block height [%d] hasn't reached sleep-until block height [%d]", this.currentBlockHeight, this.sleepUntilHeight)); return; } @@ -716,21 +715,21 @@ public class MachineState { if (nextOpCode == null) throw new IllegalOperationException("OpCode 0x" + String.format("%02x", rawOpCode) + " not recognised"); - this.logger.debug("[PC: " + String.format("%04x", this.programCounter) + "] " + nextOpCode.name()); + this.logger.debug(() -> String.format("[PC: %04x] %s", this.programCounter, nextOpCode.name())); // Request opcode step-fee from API, apply fee to balance, etc. int opcodeSteps = this.api.getOpCodeSteps(nextOpCode); long opcodeFee = opcodeSteps * feePerStep; if (this.steps + opcodeSteps > maxSteps) { - logger.debug("Enforced sleep due to exceeding maximum number of steps (" + maxSteps + ") per execution round"); + logger.debug(() -> String.format("Enforced sleep due to exceeding maximum number of steps (%d) per execution round", maxSteps)); this.isSleeping = true; break; } if (this.currentBalance < opcodeFee) { // Not enough balance left to continue execution - freeze AT - logger.debug("Frozen due to lack of balance"); + logger.debug(() -> "Frozen due to lack of balance"); this.isFrozen = true; this.frozenBalance = this.currentBalance; break; @@ -746,7 +745,7 @@ public class MachineState { // Synchronize programCounter with codeByteBuffer in case of JMPs, branches, etc. this.programCounter = codeByteBuffer.position(); } catch (ExecutionException e) { - this.logger.debug("Error at PC " + String.format("%04x", this.programCounter) + ": " + e.getMessage()); + this.logger.error(() -> String.format("Error at PC %04x: %s", this.programCounter, e.getMessage())); if (this.onErrorAddress == null) { this.isFinished = true; @@ -767,18 +766,18 @@ public class MachineState { if (this.isSleeping) { if (this.sleepUntilHeight != null) - this.logger.debug("Sleeping until block " + this.sleepUntilHeight); + this.logger.debug(() -> String.format("Sleeping until block %d", this.sleepUntilHeight)); else - this.logger.debug("Sleeping until next block"); + this.logger.debug(() -> "Sleeping until next block"); } if (this.isStopped) { - this.logger.debug("Setting program counter to stop address: " + String.format("%04x", this.onStopAddress)); + this.logger.debug(() -> String.format("Setting program counter to stop address: %04x", this.onStopAddress)); this.programCounter = this.onStopAddress; } if (this.isFinished) { - this.logger.debug("Finished - refunding remaining funds back to creator"); + this.logger.debug(() -> "Finished - refunding remaining funds back to creator"); this.api.onFinished(this.currentBalance, this); this.currentBalance = 0; } diff --git a/Java/src/test/java/org/ciyam/at/test/TestLogger.java b/Java/src/test/java/org/ciyam/at/test/TestLogger.java index 01c624c..80d3082 100644 --- a/Java/src/test/java/org/ciyam/at/test/TestLogger.java +++ b/Java/src/test/java/org/ciyam/at/test/TestLogger.java @@ -1,5 +1,7 @@ package org.ciyam.at.test; +import java.util.function.Supplier; + import org.ciyam.at.AtLogger; public class TestLogger implements AtLogger { @@ -9,14 +11,29 @@ public class TestLogger implements AtLogger { System.err.println("ERROR: " + message); } + @Override + public void error(Supplier messageSupplier) { + System.err.println("ERROR: " + messageSupplier.get()); + } + @Override public void debug(String message) { System.err.println("DEBUG: " + message); } + @Override + public void debug(Supplier messageSupplier) { + System.err.println("DEBUG: " + messageSupplier.get()); + } + @Override public void echo(String message) { System.err.println("ECHO: " + message); } + @Override + public void echo(Supplier messageSupplier) { + System.err.println("ECHO: " + messageSupplier.get()); + } + }