Chevrotain / chevrotain

Parser Building Toolkit for JavaScript
https://chevrotain.io
Apache License 2.0
2.48k stars 204 forks source link

Why does wrappedGrammarRule and invokeRuleWithTry 10x the runtime in browser? #1728

Closed tyleradams closed 2 years ago

tyleradams commented 2 years ago
Google Chrome 96.0.4664.93 (Official Build) (64-bit)
Revision 17531e0a70b4f8108f2418e8b5117f465077710b-refs/branch-heads/4664@{#1229}
OS Linux
JavaScript V8 9.6.180.20
User Agent Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36

Running chevrotain 9.1.0

+chevrotain@^9.1.0:
+  version "9.1.0"
+  resolved "https://registry.yarnpkg.com/chevrotain/-/chevrotain-9.1.0.tgz#ca2a811372687ad6f4d11c063cd27a26e5fbd52d"
+  integrity sha512-A86/55so63HCfu0dgGg3j9u8uuuBOrSqly1OhBZxRu2x6sAKILLzfVjbGMw45kgier6lz45EzcjjWtTRgoT84Q==

I'm calling and profiling calling a rule 1000x in browser and getting weird results.

I'm just evaluating 1 x 1000.So looking into it, I found this weirdness:

                "name": "formulaWithBinaryOp",
                "children": [
                  {
                    "name": "wrappedGrammarRule",
                    "children": [
                      {
                        "name": "invokeRuleWithTry",
                        "children": [
                          {
                            "name": "apply",
                            "children": [
                              {
                                "name": "formulaWithBinaryOpInternal",
                                "children": [],
                                "time": 365.3999992609024,
                                "count": 3015
                              }
                            ],
                            "time": 1268.0000015497208,
                            "count": 14101
                          }
                        ],
                        "time": 1387.3000016212463,
                        "count": 14101
                      }
                    ],
                    "time": 1511.3000013828278,
                    "count": 14101
                  }
                ],
                "time": 2953.499998807907,
                "count": 1001
              },

formulaWithBinaryOp is a rule. The actual method "this.formulaBinaryOp", takes 2953ms total Then measuring inside wrappedGrammarRule (inside recognizer_engine.ts) , I get half that (1500) Then it's mostly small decreases within recognizer_engine.ts until I get to "impl.appy(this, args);" (https://github.com/Chevrotain/chevrotain/blob/master/packages/chevrotain/src/parse/parser/traits/recognizer_engine.ts#L241) which outside takes 1268ms but the rule method (impl) takes only 365ms!?

What's going on and is there a way we can optimize this?

bd82 commented 2 years ago

moving to discussions