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

feat(traverse): add generate_uid_in_current_hoist_scope method #7423

Conversation

Dunqing
Copy link
Member

@Dunqing Dunqing commented Nov 22, 2024

Add an API to handle variable hoisting.

Copy link

graphite-app bot commented Nov 22, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “0-merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

@github-actions github-actions bot added the C-enhancement Category - New feature or request label Nov 22, 2024
@Dunqing Dunqing force-pushed the 11-22-feat_traverse_support_automatically_variable_hoisting_during_generating_bindings branch from 4911290 to d1dbaa1 Compare November 22, 2024 14:27
@github-actions github-actions bot added the A-transformer Area - Transformer / Transpiler label Nov 22, 2024
Copy link

codspeed-hq bot commented Nov 22, 2024

CodSpeed Performance Report

Merging #7423 will not alter performance

Comparing 11-22-feat_traverse_support_automatically_variable_hoisting_during_generating_bindings (9c9deae) with main (3a1ef6a)

Summary

✅ 30 untouched benchmarks

@Dunqing Dunqing force-pushed the 11-22-feat_traverse_support_automatically_variable_hoisting_during_generating_bindings branch 2 times, most recently from 7ca6c06 to f0fd5d3 Compare November 25, 2024 10:47
@Dunqing Dunqing marked this pull request as ready for review November 25, 2024 10:48
@Dunqing Dunqing force-pushed the 11-22-feat_traverse_support_automatically_variable_hoisting_during_generating_bindings branch from f0fd5d3 to ea274bc Compare November 25, 2024 11:09
@overlookmotel overlookmotel force-pushed the 11-22-feat_traverse_support_automatically_variable_hoisting_during_generating_bindings branch 2 times, most recently from 2d5d1d3 to 460f0d2 Compare November 25, 2024 13:05
@overlookmotel
Copy link
Contributor

overlookmotel commented Nov 25, 2024

I've squashed the auto-fix commit and updated conformance results, so can better see what's going on here. Reviewing now...

Copy link
Contributor

@overlookmotel overlookmotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions:

  1. Add current_var_hoisting_scope_id method to TraverseCtx too.
  2. Instead of set_current_scope_id taking 2 params, add a 2nd function set_current_hoist_scope_id instead.
  • Only generate calls to set_current_hoist_scope_id for the scopes which are "hoist scopes" (functions etc).
  • This should be faster for the majority of scopes which are not "hoist scopes".
  1. Rather than all binding creation going via add_binding which has the cost of looking up scope flags and a branch, should we have generate_uid_in_current_scope and generate_uid_in_current_hoist_scope?
  • When generating a let binding, there's no reason to do any work checking flags.

These could be done in follow-up PRs if you prefer.

Longer-term we should probably (as you suggested previously) integrate creating the binding into VarDeclarationsStore::insert_var and insert_let. So then those 2 functions will already know what scope the binding should be in, as they know if it's a var or let.

@Dunqing
Copy link
Member Author

Dunqing commented Nov 25, 2024

3. Rather than all binding creation going via add_binding which has the cost of looking up scope flags and a branch, should we have generate_uid_in_current_scope and generate_uid_in_current_hoist_scope?

Yes, we even do not need to pass SymbolFlags in this API

@overlookmotel
Copy link
Contributor

Yes, we even do not need to pass SymbolFlags in this API

Oh yes! I didn't even think of that.

@Dunqing
Copy link
Member Author

Dunqing commented Nov 25, 2024

Yes, we even do not need to pass SymbolFlags in this API

Oh yes! I didn't even think of that.

Haha. I am trying to avoid adding new APIs (because we have many generate binding APIs), but it is worth doing it.

@Dunqing
Copy link
Member Author

Dunqing commented Nov 25, 2024

  1. Add current_var_hoisting_scope_id method to TraverseCtx too.

We have no real case that needs to be inserted to var hoisting scope id manually, so I think it doesn't need to be added to TraverseCtx for now. What are your thoughts on this? Please let me know.

@overlookmotel
Copy link
Contributor

overlookmotel commented Nov 25, 2024

We currently alias all methods of TraverseScoping (and TraverseAncestry) on to TraverseCtx, so personally I think it's preferable to be consistent. Generated code for walk_* functions can also use the shorter version.

@Dunqing Dunqing changed the title feat(traverse): support automatically variable hoisting during generating bindings feat(traverse): add generate_uid_in_current_hoist_scope method Nov 25, 2024
@Dunqing Dunqing force-pushed the 11-22-feat_traverse_support_automatically_variable_hoisting_during_generating_bindings branch from 460f0d2 to 69da6fc Compare November 25, 2024 17:35
@Dunqing
Copy link
Member Author

Dunqing commented Nov 25, 2024

Seems we still need an API to automatically check whether to use pass-in scope_id or current_hoist_scope_id. Please see #7469. Now it looks good

@Dunqing Dunqing force-pushed the 11-22-feat_traverse_support_automatically_variable_hoisting_during_generating_bindings branch 4 times, most recently from 67e0d9c to 2ee960a Compare November 26, 2024 05:52
Copy link
Contributor

@overlookmotel overlookmotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a few commits. Just nits, really.

Merging this now. @Dunqing if you don't like any of my changes, feel free to alter them in follow-ups.

@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Nov 26, 2024
Copy link
Contributor

overlookmotel commented Nov 26, 2024

Merge activity

  • Nov 26, 6:17 AM EST: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Nov 26, 6:17 AM EST: A user added this pull request to the Graphite merge queue.
  • Nov 26, 6:27 AM EST: A user merged this pull request with the Graphite merge queue.

overlookmotel pushed a commit that referenced this pull request Nov 26, 2024
@overlookmotel overlookmotel force-pushed the 11-22-feat_traverse_support_automatically_variable_hoisting_during_generating_bindings branch from 6adfb7f to e8c99bb Compare November 26, 2024 11:18
@overlookmotel overlookmotel force-pushed the 11-22-feat_traverse_support_automatically_variable_hoisting_during_generating_bindings branch from e8c99bb to 9c9deae Compare November 26, 2024 11:21
@graphite-app graphite-app bot merged commit 9c9deae into main Nov 26, 2024
27 checks passed
@graphite-app graphite-app bot deleted the 11-22-feat_traverse_support_automatically_variable_hoisting_during_generating_bindings branch November 26, 2024 11:27
@oxc-bot oxc-bot mentioned this pull request Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0-merge Merge with Graphite Merge Queue A-transformer Area - Transformer / Transpiler C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants