Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize GatherNodeParts #105

Open
overlookmotel opened this issue Sep 3, 2024 · 0 comments
Open

Optimize GatherNodeParts #105

overlookmotel opened this issue Sep 3, 2024 · 0 comments

Comments

@overlookmotel
Copy link

overlookmotel commented Sep 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant