atom / language-java

Java package for Atom
Other
62 stars 58 forks source link

Textmate grammar freezes with long string constant #207

Open alexr00 opened 4 years ago

alexr00 commented 4 years ago

From @swbrenneis in https://github.com/microsoft/vscode/issues/79640

Paste the following into a java file:

package org.mytest;

class MyTest {
    void doMyTest() {
        testString =  "A900102008Exx xxxxxxxxxxxxxxxxxxxxxx          T1201801210000P0000000002421000002Exxxxxx                                                     P021000021JPxxxxxxxxxxxxxxxxx                 021000021xxxxxxxxxxxxxxxxxxx                 0000000222100D                                   P021000089xxxxxxxxxxxx                        021000089xxxxxxxxxxxx                        0000000012100C                                   P051000017xxxxxxxxxxxxxxx                     051000017xxxxxxxxxxxxxxx                     0000000020000C                                   P081000032xxxxxxxxxxxxxxx                     081000032xxxxxxxxxxxxxxx                     0000000020000D                                   P121000248xxxxxxxxxxxxxxxx                    121000248xxxxxxxxxxxxxxxx                    0000000210000C                                   ";
    }
}

The grammar will cause a freeze

sadikovi commented 4 years ago

Thanks for reporting, I will have a look.

sadikovi commented 4 years ago

I root caused the issue - variable pattern causes the problem, will try fixing it.

RedCMD commented 1 year ago

Seems to be caused by class: https://github.com/atom/language-java/blob/047fd33bd12f4926bc5dbc0c930cd9d5fa280602/grammars/java.cson#L279-L367 and record: https://github.com/atom/language-java/blob/047fd33bd12f4926bc5dbc0c930cd9d5fa280602/grammars/java.cson#L1286-L1348

Commenting both out reduces lag massively

RedCMD commented 1 year ago

the [\\w\\s]* and \\b(?:class|(?<!@)interface|enum)\\s+ clashes quite badly causing catastrophic backtracking when no class/record is found on a standalone word combine that with the begin rule is a 0width lookahead with no outside anchoring points not a good recipe for performance

changing (?=\\w?[\\w\\s-]*\\b(?:class|(?<!@)interface|enum)\\s+[\\w$]+) to (?=(?>(?!\\b(?>class|(?<!@)interface|enum)\\s)\\w++|[\\w\\s-&&[^cei]]++)*+\\b(?>class|(?<!@)interface|enum)\\s+[\\w$]) helps out alot, but does not remove the lag entirely and the same for record: (?=\\w?[\\w\\s]*\\b(?:record)\\s+[\\w$]+) => (?=(?>(?!\\brecord\\s)\\w++|[\\w\\s&&[^r]]++)*+\\brecord\\s+[\\w$])

image