From be57daa108cc6d8468b17307f0f9b1b5756871d5 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 30 Apr 2013 21:41:55 -0500 Subject: [PATCH] Remove unnecessary reflection, fix option to not reuse DFA --- .../org/antlr/v4/test/TestPerformance.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/tool/test/org/antlr/v4/test/TestPerformance.java b/tool/test/org/antlr/v4/test/TestPerformance.java index facd8d2a3..1cc2a60ea 100644 --- a/tool/test/org/antlr/v4/test/TestPerformance.java +++ b/tool/test/org/antlr/v4/test/TestPerformance.java @@ -75,7 +75,6 @@ import java.lang.ref.Reference; import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; import java.lang.reflect.Constructor; -import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; @@ -750,15 +749,18 @@ public class TestPerformance extends BaseTest { lexer.setInputStream(input); } else { lexer = lexerCtor.newInstance(input); - if (COMPUTE_TRANSITION_STATS) { - lexer.setInterpreter(new StatisticsLexerATNSimulator(lexer, lexer.getATN(), lexer.getInterpreter().decisionToDFA, lexer.getInterpreter().getSharedContextCache())); - } - sharedLexers[thread] = lexer; + DFA[] decisionToDFA = lexer.getInterpreter().decisionToDFA; if (!REUSE_LEXER_DFA) { - Field decisionToDFAField = LexerATNSimulator.class.getDeclaredField("decisionToDFA"); - decisionToDFAField.setAccessible(true); - decisionToDFAField.set(lexer.getInterpreter(), lexer.getInterpreter().decisionToDFA.clone()); + decisionToDFA = new DFA[decisionToDFA.length]; } + + if (COMPUTE_TRANSITION_STATS) { + lexer.setInterpreter(new StatisticsLexerATNSimulator(lexer, lexer.getATN(), decisionToDFA, lexer.getInterpreter().getSharedContextCache())); + } else if (!REUSE_LEXER_DFA) { + lexer.setInterpreter(new LexerATNSimulator(lexer, lexer.getATN(), decisionToDFA, lexer.getInterpreter().getSharedContextCache())); + } + + sharedLexers[thread] = lexer; } if (!REUSE_LEXER_DFA) { @@ -787,9 +789,17 @@ public class TestPerformance extends BaseTest { parser.setInputStream(tokens); } else { parser = parserCtor.newInstance(tokens); - if (COMPUTE_TRANSITION_STATS) { - parser.setInterpreter(new StatisticsParserATNSimulator(parser, parser.getATN(), parser.getInterpreter().decisionToDFA, parser.getInterpreter().getSharedContextCache())); + DFA[] decisionToDFA = parser.getInterpreter().decisionToDFA; + if (!REUSE_PARSER_DFA) { + decisionToDFA = new DFA[decisionToDFA.length]; } + + if (COMPUTE_TRANSITION_STATS) { + parser.setInterpreter(new StatisticsParserATNSimulator(parser, parser.getATN(), decisionToDFA, parser.getInterpreter().getSharedContextCache())); + } else if (!REUSE_PARSER_DFA) { + parser.setInterpreter(new ParserATNSimulator(parser, parser.getATN(), decisionToDFA, parser.getInterpreter().getSharedContextCache())); + } + sharedParsers[thread] = parser; } @@ -799,12 +809,6 @@ public class TestPerformance extends BaseTest { parser.addErrorListener(new SummarizingDiagnosticErrorListener()); } - if (!REUSE_PARSER_DFA) { - Field decisionToDFAField = ParserATNSimulator.class.getDeclaredField("decisionToDFA"); - decisionToDFAField.setAccessible(true); - decisionToDFAField.set(parser.getInterpreter(), parser.getInterpreter().decisionToDFA.clone()); - } - if (!REUSE_PARSER_DFA) { ATN atn = parser.getATN(); for (int i = 0; i < parser.getInterpreter().decisionToDFA.length; i++) { @@ -849,10 +853,15 @@ public class TestPerformance extends BaseTest { if (REUSE_PARSER && parser != null) { parser.setInputStream(tokens); } else { + Parser previousParser = parser; parser = parserCtor.newInstance(tokens); + DFA[] decisionToDFA = previousParser.getInterpreter().decisionToDFA; if (COMPUTE_TRANSITION_STATS) { - parser.setInterpreter(new StatisticsParserATNSimulator(parser, parser.getATN(), parser.getInterpreter().decisionToDFA, parser.getInterpreter().getSharedContextCache())); + parser.setInterpreter(new StatisticsParserATNSimulator(parser, parser.getATN(), decisionToDFA, parser.getInterpreter().getSharedContextCache())); + } else if (!REUSE_PARSER_DFA) { + parser.setInterpreter(new ParserATNSimulator(parser, parser.getATN(), decisionToDFA, parser.getInterpreter().getSharedContextCache())); } + sharedParsers[thread] = parser; }