antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.08k stars 3.68k forks source link

Swift3: Performance issue #1691

Open mbolotov opened 4 years ago

mbolotov commented 4 years ago

Swift3 parser takes about 5 seconds to parse file of such content:

class MyTest : XCTestCase {
     func test0() {
     }
     func test1() {
     }
     <...>
     func test256() {
     }
}

For comparison, the java8 parser takes about 25 ms to parse a similar file in java. To reproduce I used the following code:

import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.junit.Assert;
import org.junit.Test;

public class PerformanceTest {
    static int functionCount = 256;

    static String functions;
    static {
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < functionCount; i++) {
            b.append(String.format("\tfunc test%s() {\n\t}\n", i));
        }
        functions = b.toString();
    }

    static String  source = "class MyTest : XCTestCase {\n" + functions + "\n}";

    @Test
    public void test1kLinesOfTestMethods() {
        long start = System.currentTimeMillis();
        Swift3Lexer lexer = new Swift3Lexer(CharStreams.fromString(source));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        Swift3Parser parser = new Swift3Parser(tokens);
                parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
                parser.top_level();
                long time = System.currentTimeMillis() - start;
        Assert.assertTrue("Swift3 parser is too slow: " + time + " ms total, " + (time / functionCount) + " ms per empty function", time < 500);
    }
}

This test is available here in my fork: https://github.com/mbolotov/grammars-v4/blob/master/swift/swift3/src/test/java/PerformanceTest.java

KvanTTT commented 4 years ago

I think this is related to performance problems in Swift runtime itself. Check issues in ANTLR tool repository or create new if required.

ghost commented 4 years ago

Wait, it's swift3 grammar on java target

mbolotov commented 4 years ago

@KvanTTT, this is on java target as @studentmain noticed. Why should we create another issue?

KvanTTT commented 4 years ago

Sorry, my fault. I'm reopening.

Also, take a look at our fork with Swift5 grammar. We are working on it and we are going to merge that grammar as soon as possible.

mbolotov commented 4 years ago

@KvanTTT thanks for pointing to the Swift5 grammar! It works much faster then the original Swfit3 one. I just had to switch from c# back to java support there.

xiongyoudou commented 3 years ago

@KvanTTT thanks for pointing to the Swift5 grammar! It works much faster then the original Swfit3 one. I just had to switch from c# back to java support there.

@Mikhail Bolotov @mbolotov Hello,I also doing want to target Java to get Swift ast. But I am not familiar with C#, so can you get me one copy of the two files(SwiftBaseLexer.cs、SwiftBaseParser.cs) in Java version? Thanks!