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(ast): add AstNode trait and provide access to AstNodeId #2861

Closed

Conversation

rzvxa
Copy link
Collaborator

@rzvxa rzvxa commented Mar 29, 2024

closes #2818

Copy link
Collaborator Author

rzvxa commented Mar 29, 2024

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

Join @rzvxa and the rest of your teammates on Graphite Graphite

@rzvxa rzvxa changed the title feat(ast): add AstNode trait and its derive macro. feat(ast): add AstNode trait and provide access to AstNodeId Mar 29, 2024
@github-actions github-actions bot added A-parser Area - Parser A-ast Area - AST labels Mar 29, 2024
Copy link

codspeed-hq bot commented Mar 29, 2024

CodSpeed Performance Report

Merging #2861 will degrade performances by 3.05%

Comparing 03-30-feat_ast_add_astnode_trait_and_its_derive_macro (4ae5c5e) with main (7034bcc)

Summary

❌ 2 regressions
✅ 34 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main 03-30-feat_ast_add_astnode_trait_and_its_derive_macro Change
parser_napi[RadixUIAdoptionSection.jsx] 230.1 µs 237.3 µs -3.03%
semantic[checker.ts] 541 ms 558 ms -3.05%

@github-actions github-actions bot added A-linter Area - Linter A-minifier Area - Minifier A-transformer Area - Transformer / Transpiler labels Mar 30, 2024
@rzvxa rzvxa force-pushed the 03-30-feat_ast_add_astnode_trait_and_its_derive_macro branch from 1a7cb08 to 5f60ac7 Compare March 30, 2024 12:59
@rzvxa rzvxa force-pushed the 03-30-feat_ast_add_astnode_trait_and_its_derive_macro branch from ffd871f to 2d2f8f4 Compare March 30, 2024 14:13
@github-actions github-actions bot added the A-semantic Area - Semantic label Mar 30, 2024
@@ -24,10 +26,13 @@ pub struct JSXElement<'a> {
pub opening_element: Box<'a, JSXOpeningElement<'a>>,
pub closing_element: Option<Box<'a, JSXClosingElement<'a>>>,
pub children: Vec<'a, JSXChild<'a>>,

#[cfg_attr(feature = "serialize", serde(skip))]
pub(crate) ast_node_id: AstNodeIdContainer,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Boshen I like the idea of keeping the raw ast node IDs private to the crate, But it would mean that we have to either add new functions for a bunch of types or add missing types to ast_builder so we can use that in all cases instead of constructing with Type { ... } syntax. With ast_node_id private only enum types can be initialized directly.

Should I go for it or make the field public? The public field is a bit less encapsulated and results in a more verbose construction since you always need to set the field to default but reduces the dependency on ast builder, Do we want our types to even be initialized without a builder? Or do we desire to keep all allocations in a singular point? The ladder by default means that moving constructions to the builder is a plus but I can't say the same thing for the first one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we move all allocations to the builder we might want to make them inline as well to reduce the jumps while constructing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll make it public for now so it won't stop my progress, But it is up for debate if anyone wants to pitch in on how we would approach it. I can address it in an upcoming PR.

@overlookmotel
Copy link
Collaborator

In my view AstNodeId should be a non-zero type e.g. NonZeroUsize. Then AstNodeIdContainer will be half the size.

std::mem::size_of::<Cell<Option<usize>>>() == 16
std::mem::size_of::<Cell<Option<NonZeroUsize>>>() == 8

This would likely reduce the performance reduction visible on this PR at present.

You'd just need to initialize AstNodes::nodes and AstNodes::parent_ids with dummy entries to occupy index 0, and enforce that that dummy entry can never be popped off again (maybe wrap both Vecs to make them append-only).

Also, in my view AstNodeId could be a u32 (by which I mean NonZeroU32) for the reasons mentioned in #2876 (comment). But that's a separate argument from what I'm saying here.

@Boshen Boshen closed this May 8, 2024
@overlookmotel
Copy link
Collaborator

overlookmotel commented May 11, 2024

Let's keep this branch live, even though PR is closed. We may want to come back to this later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ast Area - AST A-linter Area - Linter A-minifier Area - Minifier A-parser Area - Parser A-semantic Area - Semantic A-transformer Area - Transformer / Transpiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ast_node_id field to all ast nodes
3 participants