Skip to content

Optimize GatherNodeParts #105

@overlookmotel

Description

@overlookmotel

GatherNodeParts is used for generating unique identifiers based on an AST node. e.g. the MemberExpression foo.bar results in a UID _foo$bar.

Currently GatherNodeParts::gather calls a callback with &str slices which are assembled into a string.

We then:

  1. Cut name down to 20 bytes max.
  2. Sanitize the string to remove illegal characters (foo["bar-qux"] becomes _foo$barQux).
  3. Trim off leading _s.
  4. Add a leading _ on start.
  5. Add a number to the end if necessary to make it unique.

This algorithm is directly ported from Babel.

There are various ways we can optimize this:

  1. Pass a mutable &mut String / &mut CompactString into gather to append to (or maybe could be a &mut str referencing a str on the stack, since we have a static max length anyway).
  2. Stop adding to that string when hit 20 bytes max (since it'll be cut down to 20 bytes anyway).
  3. Skip sanitizing parts which we know cannot contain invalid characters (e.g. IdentifierReference, IdentifierName - common case).
  4. Avoid calling is_identifier_name at start of to_identifier. This involves iterating over the string twice if is_identifier_name returns false.
  5. Avoid Chars iterator - iterate over bytes instead.

When the output is going to be minified anyway, there's also no point in generating these "sensible" var names at all. Just _10 would suffice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions