Skip to content

Repo: investigate switching core TS->ESTree conversion logic from a "switch/case" to a "jump table" for performance improvements #6149

Closed as not planned
@bradzacher

Description

@bradzacher

Suggestion

https://artemis.sh/2022/08/07/emulating-calculators-fast-in-js.html

I read this a while ago but didn't think about it's possible performance application in our parser.

Our core conversion logic is one massive switch/case (currently 159 cases).

According to the stackoverflow comment (which has references to the v8 source code) - if you've got more than 128 cases then the switch might not get properly optimised and thus acts slower than hand-written if/else.

One workaround is to use a "jump table" instead of a switch. Jump tables rely on the case value instead being the key of an object - meaning the lookup is instead always as performant as a property lookup (which is usually O(1)).

We should investigate if switching to a "jump table" improves the perf of our parser.

For reference the conversion would be something like this:

switch (node.kind) {
  case SyntaxKind.SourceFile: // ...
  case SyntaxKind.Block: // ...
  // ...
}

// to

const JUMP_TABLE = {
  [SyntaxKind.SourceFile]: (node: ts.SourceFile) => { /* ... */ },
  [SyntaxKind.Block]: (node: ts.Block) => { /* ... */ },
  // ...
};

JUMP_TABLE[node.kind](node);

For this task we'll want to:

  1. instrument typescript-estree to measure the performance baseline with the current code.
  2. make the conversion to a jump table.
  3. measure the performance after the change.
  4. if (3) < (1), then merge the change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueperformanceIssues regarding performancerepo maintenancethings to do with maintenance of the repo, and not with code/docs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions