Skip to content

feat(allocator): TakeIn trait#9969

Merged
graphite-app[bot] merged 1 commit intomainfrom
03-22-feat_allocator_takein_trait
Mar 24, 2025
Merged

feat(allocator): TakeIn trait#9969
graphite-app[bot] merged 1 commit intomainfrom
03-22-feat_allocator_takein_trait

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Mar 22, 2025

Add 2 traits and implement them for AST types:

  • Dummy trait creates a dummy node for an AST type.
  • TakeIn replaces a node with a dummy.

Reason for 2 separate traits is that for some types we need to implement Dummy, but it'd be inappropriate to implement TakeIn. e.g.:

  • Fieldless enums which are Copy: They should just be copied, rather than replaced.
  • Span: Ditto.
  • Box<T>: You should replace the contents of the Box, not the Box itself.

Ideally we wouldn't be implementing TakeIn for so many types. Some types are very expensive to generate a dummy for, so we'd ideally guide consumers away from that by not implementing TakeIn for those types.

But we want to replace the TakeIn trait in Rolldown with our own version. Because Rolldown implements TakeIn on many many types, it seemed to make sense to codegen TakeIn for pretty much all AST types, so we can migrate their code over to using our trait without too much hassle.

@github-actions github-actions bot added A-ast Area - AST A-ast-tools Area - AST tools C-enhancement Category - New feature or request labels Mar 22, 2025
Copy link
Member Author

overlookmotel commented Mar 22, 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.

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

@overlookmotel overlookmotel force-pushed the 03-22-feat_allocator_takein_trait branch from c02e938 to 2f62830 Compare March 22, 2025 14:32
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 22, 2025

CodSpeed Instrumentation Performance Report

Merging #9969 will not alter performance

Comparing 03-22-feat_allocator_takein_trait (8cd7430) with main (5ed31e0)

Summary

✅ 33 untouched benchmarks

Copy link
Member

@Boshen Boshen left a comment

Choose a reason for hiding this comment

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

I'll try and replace Rolldown's adhoc TakeIn trait with ours. Please let me know when.

@overlookmotel overlookmotel force-pushed the 03-22-feat_allocator_takein_trait branch 5 times, most recently from d3ea17d to 8c4fd76 Compare March 24, 2025 06:48
@overlookmotel overlookmotel marked this pull request as ready for review March 24, 2025 07:01
@overlookmotel overlookmotel requested a review from Boshen March 24, 2025 07:01
@overlookmotel
Copy link
Member Author

overlookmotel commented Mar 24, 2025

@Boshen I think this is ready. It's slightly different from Rolldown's version in that Rolldown implements TakeIn for Box<T>, which I think is a probably a bad idea. But it should be possible to replace their TakeIn with ours with just minor tweaks to their code.

Where they do boxed.take_in(), just replace it with boxed.as_mut().take_in() to replace the contents of the Box instead of the Box itself. Or maybe auto-deref will do that automatically.

@Boshen Boshen self-assigned this Mar 24, 2025
@Boshen

This comment was marked as outdated.

@Boshen
Copy link
Member

Boshen commented Mar 24, 2025

@overlookmotel overlookmotel force-pushed the 03-22-feat_allocator_takein_trait branch from 8c4fd76 to 24b0e8a Compare March 24, 2025 10:19
@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Mar 24, 2025
Copy link
Member Author

overlookmotel commented Mar 24, 2025

Merge activity

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

Add 2 traits and implement them for AST types:

* `Dummy` trait creates a dummy node for an AST type.
* `TakeIn` replaces a node with a dummy.

Reason for 2 separate traits is that for some types we need to implement `Dummy`, but it'd be inappropriate to implement `TakeIn`. e.g.:

* Fieldless enums which are `Copy`: They should just be copied, rather than replaced.
* `Span`: Ditto.
* `Box<T>`: You should replace the *contents* of the `Box`, not the `Box` itself.

Ideally we wouldn't be implementing `TakeIn` for so many types. Some types are very expensive to generate a dummy for, so we'd ideally guide consumers away from that by not implementing `TakeIn` for those types.

But we want to replace the `TakeIn` trait in Rolldown with our own version. Because Rolldown implements `TakeIn` on many many types, it seemed to make sense to codegen `TakeIn` for pretty much all AST types, so we can migrate their code over to using our trait without too much hassle.
@graphite-app graphite-app bot force-pushed the 03-22-feat_allocator_takein_trait branch from 24b0e8a to 8cd7430 Compare March 24, 2025 10:30
@graphite-app graphite-app bot merged commit 8cd7430 into main Mar 24, 2025
29 checks passed
@graphite-app graphite-app bot deleted the 03-22-feat_allocator_takein_trait branch March 24, 2025 10:39
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Mar 24, 2025
graphite-app bot pushed a commit that referenced this pull request Mar 24, 2025
`AstBuilder::move_*` methods use `TakeIn::take_in` (added in #9969) to replace nodes with dummy nodes.

`take_in` will always generate a dummy in whatever way minimizes the size and number of allocations required.

It would be possible to replace all the `move_*` methods with a single `AstBuilder::take` method which is generic over `T: TakeIn` (#9970). But have opted not to do that, because it's useful to be able to see in calling code what type of node is being replaced.

Some nodes are quite expensive to create a dummy for (e.g. `Declaration` and `Function` require allocating 56 bytes into arena, `ArrowFunctionExpression` allocates 128 bytes in 2 separate allocations), so it's useful to see where we're doing that, so we can try to replace them with cheaper operations e.g. replacing an `Expression` or `Statement` instead (only 8 bytes). We also want to avoid inadvertently adding further expensive `take_in` calls, which a blanket `AstBuilder::take` method would make it easy to do.
graphite-app bot pushed a commit that referenced this pull request Apr 4, 2025
The `move_xxxx` method's mission is accomplished. Let's use the `TakeIn` trait instead. See more details in #9654 and #9969.
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 C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants