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(oxc_allocator): Add new method clone_in_with_semantic_ids for CloneIn trait #8608

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

IWANABETHATGUY
Copy link
Contributor

@IWANABETHATGUY IWANABETHATGUY commented Jan 20, 2025

  1. Unlike clone_in the new method also clone semantic fields, this is used for scenario like caching ast and semantic data at the same time.

Copy link

graphite-app bot commented Jan 20, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

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

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@github-actions github-actions bot added A-semantic Area - Semantic A-ast Area - AST C-enhancement Category - New feature or request labels Jan 20, 2025
@@ -41,7 +41,6 @@ pub struct Program<'a> {
pub directives: Vec<'a, Directive<'a>>,
pub body: Vec<'a, Statement<'a>>,
#[estree(skip)]
#[clone_in(default)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not right, should use cfg(attribete)

@IWANABETHATGUY IWANABETHATGUY changed the title feat(oxc_allocator): make data in arena cloneable TEST feat(oxc_allocator): make data in arena cloneable Jan 20, 2025
@Boshen
Copy link
Member

Boshen commented Jan 20, 2025

Can you provide some context and link your Rolldown prototype for other reviewers?

@IWANABETHATGUY
Copy link
Contributor Author

Can you provide some context and link your Rolldown prototype for other reviewers?

OK, I will link it once the new poc is finished.

@overlookmotel
Copy link
Contributor

overlookmotel commented Jan 20, 2025

I'm not quite sure what you're trying to achieve here, but this may be relevant: oxc-project/backlog#127

CloneIn is a bit of a nightmare. I tend to think we should not have introduced it in the first place, because it's a footgun.

At present, it unsets all scope_id etc fields to None, which is not good because a post-semantic AST should always have these fields set with an ID.

But if we go the other way (as in your PR), then it will have ID fields set, but they're likely to be the wrong IDs.

So either way is wrong! CloneIn cannot set the IDs correctly without context about where the cloned node is going to be inserted.

See also: oxc-project/backlog#96

@IWANABETHATGUY
Copy link
Contributor Author

IWANABETHATGUY commented Jan 20, 2025

I'm not quite sure what you're trying to achieve here, but this may be relevant: oxc-project/backlog#127

CloneIn is a bit of a nightmare. I tend to think we should not have introduced it in the first place, because it's a footgun.

At present, it unsets all scope_id etc fields to None, which is not good because a post-semantic AST should always have these fields set with an ID.

But if we go the other way (as in your PR), then it will have ID fields set, but they're likely to be the wrong IDs.

So either way is wrong! CloneIn cannot set the IDs correctly without context about where the cloned node is going to be inserted.

See also: oxc-project/backlog#96

incremental build scenario, since we need to modify the ast after parsing, we clone Ast to avoid parse it next round,
I assume if the source code is not changed, the AST and semantic data should also not be changed. In this scenario, the semantic data(like ScopeId, SymbolId) could be cloned safely

@overlookmotel
Copy link
Contributor

Ah I see.

If you clone both the AST and also the matching ScopeTree and SymbolTable, then yes the IDs will all remain valid. Ditto if you don't clone ScopeTree or SymbolTable, but you don't modify them in any way either.

But I suggest we introduce a new trait for this use case CloneIntoWithIds, and document that it's only valid to use in these specific circumstances. Other code currently relies on the current implementation of CloneIn, so I don't think we should change it.

CloneIn is codegen-ed, so we can easily codegen CloneIntoWithIds too.

Note on naming: I suggest CloneIntoWithIds not CloneInWithIds to indicate that you're cloning into a different allocator, instead of in the same allocator (see oxc-project/backlog#96).

We need Allocator::with_capacity method for other purposes, so I've broken that out into a separate PR #8620.

I assume you'll also need a way to get the capacity required for the allocator you're cloning into, so I've also added that in #8621. Please take a look and confirm that covers what you need.

@overlookmotel
Copy link
Contributor

overlookmotel commented Jan 21, 2025

Actually maybe we don't need a new trait. We could just add a 2nd method to CloneIn: clone_in_with_semantic_ids.

...and document the pitfalls of using this method!

@IWANABETHATGUY Are you comfortable enough with oxc_ast_tools to make that change yourself?

@IWANABETHATGUY
Copy link
Contributor Author

Actually maybe we don't need a new trait. We could just add a 2nd method to CloneIn: clone_in_with_semantic_ids.

...and document the pitfalls of using this method!

@IWANABETHATGUY Are you comfortable enough with oxc_ast_tools to make that change yourself?

Thanks for your guiding, I will try it tomorrow. I am working on other thing today.

@github-actions github-actions bot added the A-ast-tools Area - AST tools label Jan 22, 2025
Copy link
Contributor Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

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

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@IWANABETHATGUY IWANABETHATGUY force-pushed the feat/arena-clone branch 3 times, most recently from 73768c1 to a519e6e Compare January 22, 2025 09:26
@IWANABETHATGUY IWANABETHATGUY changed the title TEST feat(oxc_allocator): make data in arena cloneable feat(oxc_allocator): Add new method clone_in_with_semantic_ids for CloneIn trait Jan 22, 2025
@IWANABETHATGUY IWANABETHATGUY marked this pull request as ready for review January 22, 2025 09:32
Copy link

codspeed-hq bot commented Jan 22, 2025

CodSpeed Performance Report

Merging #8608 will not alter performance

Comparing feat/arena-clone (ad3ef33) with main (ba201a6)

Summary

✅ 32 untouched benchmarks

crates/oxc_syntax/src/reference.rs Outdated Show resolved Hide resolved
Comment on lines 35 to 40
/// Almost same as `clone_in`, but for some special type, it will also clone the semantic ids.
#[inline]
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
self.clone_in(allocator)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please expand this comment to explain why it's generally a bad idea to use this method.

tasks/ast_tools/src/derives/clone_in.rs Outdated Show resolved Hide resolved
tasks/ast_tools/src/derives/clone_in.rs Outdated Show resolved Hide resolved
tasks/ast_tools/src/derives/clone_in.rs Outdated Show resolved Hide resolved
@@ -60,52 +62,92 @@ fn derive_enum(def: &EnumDef) -> TokenStream {
}
};

impl_clone_in(&ty_ident, def.has_lifetime, &alloc_ident, &body)
impl_clone_in(&ty_ident, def.has_lifetime, &alloc_ident, &body, &quote!())
Copy link
Contributor

Choose a reason for hiding this comment

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

You will need a clone_in_with_semantic_ids impl for most enums too. If you call clone_in_with_semantic_ids on a &Program you want it to be using clone_in_with_semantic_ids all the way through the AST, not diverting to clone_in as soon as it hits a Statement.

Otherwise Program::clone_in_with_semantic_ids will do exactly the same as Program::clone_in - cloned AST won't have any IDs.

@IWANABETHATGUY IWANABETHATGUY force-pushed the feat/arena-clone branch 4 times, most recently from 1dd4c8e to c479a19 Compare January 22, 2025 16:27
@IWANABETHATGUY IWANABETHATGUY force-pushed the feat/arena-clone branch 2 times, most recently from 7f1144f to bd3e390 Compare January 23, 2025 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ast Area - AST A-ast-tools Area - AST tools A-semantic Area - Semantic C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants