From 4df44cadea9162a079084a00efb50068b5dc489a Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 1 Jul 2016 18:56:16 +0300 Subject: [PATCH 1/4] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20JMH-=D0=B1=D0=B5=D0=BD=D1=87=D0=BC=D0=B0=D1=80?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nbproject/project.properties | 3 +- .../ownlang/parser/LexerBenchmarkTest.java | 48 ++++++++++++++++++ .../ownlang/parser/ParserBenchmarkTest.java | 50 +++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 test/com/annimon/ownlang/parser/LexerBenchmarkTest.java create mode 100644 test/com/annimon/ownlang/parser/ParserBenchmarkTest.java diff --git a/nbproject/project.properties b/nbproject/project.properties index 75dc961..1fdb04e 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -50,7 +50,8 @@ javac.test.classpath=\ ${javac.classpath}:\ ${build.classes.dir}:\ ${libs.junit_4.classpath}:\ - ${libs.hamcrest.classpath} + ${libs.hamcrest.classpath}:\ + ${libs.JMH_1.12.classpath} javac.test.processorpath=\ ${javac.test.classpath} javadoc.additionalparam= diff --git a/test/com/annimon/ownlang/parser/LexerBenchmarkTest.java b/test/com/annimon/ownlang/parser/LexerBenchmarkTest.java new file mode 100644 index 0000000..934b0fa --- /dev/null +++ b/test/com/annimon/ownlang/parser/LexerBenchmarkTest.java @@ -0,0 +1,48 @@ +package com.annimon.ownlang.parser; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import org.junit.Ignore; +import org.junit.Test; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) +@Ignore +public class LexerBenchmarkTest { + + @Param({"1000"}) + private int iterations; + + private String input; + + @Setup(Level.Trial) + public void initializeTrial() throws IOException { + input = SourceLoader.readSource("program.own"); + } + + @Benchmark + public void lexerBenchmark() { + for (int i = 0; i < iterations; i++) { + Lexer.tokenize(input); + } + } + + @Test + public void executeBenchmark() throws RunnerException { + Options opt = new OptionsBuilder() + .include(LexerBenchmarkTest.class.getSimpleName()) + .warmupIterations(3) + .measurementIterations(5) + .threads(1) + .forks(1) + .build(); + + new Runner(opt).run(); + } +} diff --git a/test/com/annimon/ownlang/parser/ParserBenchmarkTest.java b/test/com/annimon/ownlang/parser/ParserBenchmarkTest.java new file mode 100644 index 0000000..f359564 --- /dev/null +++ b/test/com/annimon/ownlang/parser/ParserBenchmarkTest.java @@ -0,0 +1,50 @@ +package com.annimon.ownlang.parser; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.TimeUnit; +import org.junit.Ignore; +import org.junit.Test; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.infra.Blackhole; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) +@Ignore +public class ParserBenchmarkTest { + + @Param({"1000"}) + private int iterations; + + private List input; + + @Setup(Level.Trial) + public void initializeTrial() throws IOException { + input = Lexer.tokenize(SourceLoader.readSource("program.own")); + } + + @Benchmark + public void parserBenchmark(Blackhole bh) { + for (int i = 0; i < iterations; i++) { + bh.consume(Parser.parse(input)); + } + } + + @Test + public void executeBenchmark() throws RunnerException { + Options opt = new OptionsBuilder() + .include(ParserBenchmarkTest.class.getSimpleName()) + .warmupIterations(3) + .measurementIterations(5) + .threads(1) + .forks(1) + .build(); + + new Runner(opt).run(); + } +} From 03b56ce0191e5cd417bb68b88ebb87ce91d3d580 Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 1 Jul 2016 18:57:12 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D0=BA=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=B2=D1=8B=D1=88?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D0=BA=D0=BE=D1=80=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/annimon/ownlang/parser/Lexer.java | 8 ++++---- src/com/annimon/ownlang/parser/Parser.java | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/com/annimon/ownlang/parser/Lexer.java b/src/com/annimon/ownlang/parser/Lexer.java index 6c2e83e..eea695a 100644 --- a/src/com/annimon/ownlang/parser/Lexer.java +++ b/src/com/annimon/ownlang/parser/Lexer.java @@ -203,7 +203,7 @@ public final class Lexer { clearBuffer(); while (true) { final String text = buffer.toString(); - if (!OPERATORS.containsKey(text + current) && !text.isEmpty()) { + if (!text.isEmpty() && !OPERATORS.containsKey(text + current)) { addToken(OPERATORS.get(text)); return; } @@ -237,9 +237,9 @@ public final class Lexer { clearBuffer(); char current = peek(0); while (true) { + if (current == '`') break; if (current == '\0') throw error("Reached end of file while parsing extended word."); if (current == '\n' || current == '\r') throw error("Reached end of line while parsing extended word."); - if (current == '`') break; buffer.append(current); current = next(); } @@ -252,7 +252,6 @@ public final class Lexer { clearBuffer(); char current = peek(0); while (true) { - if (current == '\0') throw error("Reached end of file while parsing text string."); if (current == '\\') { current = next(); switch (current) { @@ -288,6 +287,7 @@ public final class Lexer { continue; } if (current == '"') break; + if (current == '\0') throw error("Reached end of file while parsing text string."); buffer.append(current); current = next(); } @@ -306,8 +306,8 @@ public final class Lexer { private void tokenizeMultilineComment() { char current = peek(0); while (true) { - if (current == '\0') throw error("Reached end of file while parsing multiline comment"); if (current == '*' && peek(1) == '/') break; + if (current == '\0') throw error("Reached end of file while parsing multiline comment"); current = next(); } next(); // * diff --git a/src/com/annimon/ownlang/parser/Parser.java b/src/com/annimon/ownlang/parser/Parser.java index e80adde..a89c921 100644 --- a/src/com/annimon/ownlang/parser/Parser.java +++ b/src/com/annimon/ownlang/parser/Parser.java @@ -6,6 +6,7 @@ import com.annimon.ownlang.lib.StringValue; import com.annimon.ownlang.lib.UserDefinedFunction; import com.annimon.ownlang.parser.ast.*; import java.util.ArrayList; +import java.util.EnumMap; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -26,10 +27,10 @@ public final class Parser { } private static final Token EOF = new Token(TokenType.EOF, "", -1, -1); - - private static final Map assignOperator; + + private static final EnumMap assignOperator; static { - assignOperator = new HashMap<>(BinaryExpression.Operator.values().length + 1); + assignOperator = new EnumMap(TokenType.class); assignOperator.put(TokenType.EQ, null); assignOperator.put(TokenType.PLUSEQ, BinaryExpression.Operator.ADD); assignOperator.put(TokenType.MINUSEQ, BinaryExpression.Operator.SUBTRACT); From b0f19e208ad4d6eecb90c2e38547413ea437dcfc Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 1 Jul 2016 19:05:33 +0300 Subject: [PATCH 3/4] =?UTF-8?q?=D0=97=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B8=20=D0=B4=D0=BB=D1=8F=20gradle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle b/build.gradle index d8e6d40..0758e11 100644 --- a/build.gradle +++ b/build.gradle @@ -37,4 +37,6 @@ repositories { dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' + testCompile group: 'org.openjdk.jmh', name: 'jmh-core', version: '1.12' + testCompile group: 'org.openjdk.jmh', name: 'jmh-generator-annprocess', version: '1.12' } From 13d320a3010bec1630c8be197d954f3422da9b4d Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 4 Jul 2016 12:10:02 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BB=D0=B8=D1=88=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=BA=D0=B8=20=D0=B2=20std::range?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/annimon/ownlang/lib/modules/functions/std_range.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/annimon/ownlang/lib/modules/functions/std_range.java b/src/com/annimon/ownlang/lib/modules/functions/std_range.java index 4b95ed8..e5198db 100644 --- a/src/com/annimon/ownlang/lib/modules/functions/std_range.java +++ b/src/com/annimon/ownlang/lib/modules/functions/std_range.java @@ -82,9 +82,9 @@ public final class std_range implements Function { private boolean isIntegerRange() { if (to > 0) { - return (to < Integer.MAX_VALUE) && (from > Integer.MIN_VALUE && to < Integer.MAX_VALUE); + return (from > Integer.MIN_VALUE && to < Integer.MAX_VALUE); } - return (to > Integer.MIN_VALUE) && (to > Integer.MIN_VALUE && from < Integer.MAX_VALUE); + return (to > Integer.MIN_VALUE && from < Integer.MAX_VALUE); } @Override