Esprima performance

52 views
Skip to first unread message
Message has been deleted

Ariya Hidayat

unread,
Nov 29, 2016, 10:42:54 PM11/29/16
to esp...@googlegroups.com
Hi Kenny,

So far I am mainly focusing on getting ES2017 features into Esprima 4.0 (see https://github.com/jquery/esprima/issues/1589 for the details). I plan to focus again on performance post the 4.0 release.

We use a predefined corpus to run timing analysis, you can see this by running `npm run benchmark`. If there are ideas to speed things up and it results in a non-negligible difference of the said benchmark, I’m all ears!

Thank you!

Best regards,



On Tue, Nov 29, 2016 at 8:07 AM Kenny Flashlight <Kenny Flashlight > wrote:

Hi. I was wondering about esprima performance. What is the thoughts and plans for improvement?

I noticed esprima is faster then Acorn but soon as V8 warms up acorn wins on large data sets.

Some of the current esprima code can be optimized to use bitmasks to gain better perf.

Im noWindowWorking on github

--
You received this message because you are subscribed to the Google Groups "esprima" group.
To unsubscribe from this group and stop receiving emails from it, send an email to esprima+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Message has been deleted

Ariya Hidayat

unread,
Dec 1, 2016, 10:31:18 AM12/1/16
to esp...@googlegroups.com

Does that change the result of running `npm run benchmark`?


--
Ariya Hidayat, https://ariya.io
Message has been deleted
Message has been deleted

Ariya Hidayat

unread,
Dec 1, 2016, 10:46:31 PM12/1/16
to esp...@googlegroups.com
Hi Kenny,

When you play with these optimizations in your setup, what’s the improvement that you observed? Do you mind posting the output of `npm run benchmark` before and after?

Thanks!

On Thu, Dec 01, 2016 at 3:01 PM KFRF <KFRF > wrote:
With the suggestions above everything will stay as before. No tests breaks etc. You only get a code size reduction. Regarding benchmark, yes somehow it will have impact on the benchmark results, but you would also need to change this module

https://github.com/jquery/esprima/blob/master/src/token.ts#L1-L12

For the token, you change it to

export const enum Token {
    BooleanLiteral = 1,
    EOF  = 1 << 1,
    Identifier = 1 << 2,
    Keyword = 1 << 3,
    NullLiteral = 1 << 4,
    NumericLiteral = 1 << 5,
    Punctuator = 1 << 6,
    StringLiteral = 1 << 7,
    RegularExpression = 1 << 8,
    Template = 1 << 9,
};

Note the *const* before the enum. Meaning when you compile the source, you save bytes again because it will be a replaced by numbers.


And this one has to be switched into a function to make V8 happy. 

https://github.com/jquery/esprima/blob/master/src/token.ts#L14

function getTokenName(value: number): string {
            switch (value) {
           case Token.BooleanLiteral: return 'boolean'             ... 
           default:
if (Token.Template === value) { return 'Template'; }
           }
}

Note that I added a 'default' to the switch statement. This also to make V8 happy. V8 does not like switch without default and causing a perf loss.

This is all you need to do. Now you can  replace the strict equal comparison - === - with this

Example

if (token.type === Token.Identifier) {}
has to be
if((token.type & Token.Identifier) !== 0) {}

To save space here you can create a helper function

function isNegativeBitmask(value) {
return (value & Token.Identifier) === 0)
}
function isPositiveBitmask(value) {
return (value & Token.Identifier) !== 0)
}


That's it. You will not break anything, and the benchmark results will be improved. And also V8 will be happy.

--
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages