Skip to content

perf(transformer/refresh): use take/take_in instead of drain#10656

Merged
graphite-app[bot] merged 1 commit intomainfrom
04-28-perf_transformer_refresh_use_take_take_in_instead_of_drain
Apr 30, 2025
Merged

perf(transformer/refresh): use take/take_in instead of drain#10656
graphite-app[bot] merged 1 commit intomainfrom
04-28-perf_transformer_refresh_use_take_take_in_instead_of_drain

Conversation

@Dunqing
Copy link
Member

@Dunqing Dunqing commented Apr 28, 2025

Calling take is cheaper than calling drain. These cases don't need to reuse the Vec after it drained, so we can replace them with take, which gives a tiny performance improvement.

image

Copy link
Member Author

Dunqing commented Apr 28, 2025

@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-performance Category - Solution not expected to change functional behavior, only performance labels Apr 28, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Apr 28, 2025

CodSpeed Instrumentation Performance Report

Merging #10656 will not alter performance

Comparing 04-28-perf_transformer_refresh_use_take_take_in_instead_of_drain (7d8efd8) with main (a3ada34)

Summary

✅ 36 untouched benchmarks

@Dunqing Dunqing changed the base branch from 04-28-perf_transformer_optimize_inserting_var_let_statements to graphite-base/10656 April 28, 2025 14:23
@Dunqing Dunqing force-pushed the graphite-base/10656 branch from 0479363 to 68620ef Compare April 28, 2025 14:30
@Dunqing Dunqing force-pushed the 04-28-perf_transformer_refresh_use_take_take_in_instead_of_drain branch from aecfae5 to 3648471 Compare April 28, 2025 14:30
@Dunqing Dunqing changed the base branch from graphite-base/10656 to 04-28-perf_transformer_optimize_inserting_var_let_statements April 28, 2025 14:30
@Dunqing Dunqing changed the base branch from 04-28-perf_transformer_optimize_inserting_var_let_statements to graphite-base/10656 April 29, 2025 02:25
@Dunqing Dunqing force-pushed the 04-28-perf_transformer_refresh_use_take_take_in_instead_of_drain branch from 3648471 to c01878f Compare April 29, 2025 02:32
@Dunqing Dunqing force-pushed the graphite-base/10656 branch from 68620ef to b44d2a9 Compare April 29, 2025 02:32
@Dunqing Dunqing changed the base branch from graphite-base/10656 to 04-28-perf_transformer_optimize_inserting_var_let_statements April 29, 2025 02:32
@Dunqing Dunqing marked this pull request as ready for review April 29, 2025 09:31
@Dunqing Dunqing requested a review from overlookmotel as a code owner April 29, 2025 09:31
@graphite-app graphite-app bot changed the base branch from 04-28-perf_transformer_optimize_inserting_var_let_statements to graphite-base/10656 April 29, 2025 16:01
@graphite-app graphite-app bot force-pushed the graphite-base/10656 branch from b44d2a9 to 91df9d4 Compare April 29, 2025 16:09
@graphite-app graphite-app bot force-pushed the 04-28-perf_transformer_refresh_use_take_take_in_instead_of_drain branch from c01878f to 58652a1 Compare April 29, 2025 16:09
@graphite-app graphite-app bot changed the base branch from graphite-base/10656 to main April 29, 2025 16:10
@graphite-app graphite-app bot force-pushed the 04-28-perf_transformer_refresh_use_take_take_in_instead_of_drain branch from 58652a1 to 25f8411 Compare April 29, 2025 16:10
Copy link
Member

@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.

Good spot. Though I'm not actually sure how much take_in gains over drain(..) here. The code for drain contains more instructions, but in the case of drain(..) compiler may be able to optimize most of them out. Maybe. That's just guesswork.

I suspect the perf gain in this PR is more due to:

- let mut new_statements = ctx.ast.vec_with_capacity(program.body.len());
+ let mut new_statements = ctx.ast.vec_with_capacity(program.body.len() * 2);

...so new_statements will never need to grow and reallocate during the loop.

I can see another couple of things. I'll do a follow-up PR.

@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Apr 29, 2025
Copy link
Member

overlookmotel commented Apr 29, 2025

Merge activity

  • Apr 29, 12:29 PM EDT: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Apr 29, 12:39 PM EDT: overlookmotel added this pull request to the Graphite merge queue.
  • Apr 29, 5:00 PM UTC: The Graphite merge queue couldn't merge this PR because it timed out.
  • Apr 29, 1:05 PM EDT: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Apr 29, 8:08 PM EDT: overlookmotel added this pull request to the Graphite merge queue.
  • Apr 29, 8:09 PM EDT: Merged by the Graphite merge queue.

graphite-app bot pushed a commit that referenced this pull request Apr 29, 2025
)

Calling `take` is cheaper than calling `drain`. These cases don't need to reuse the `Vec` after it drained, so we can replace them with `take`, which gives a tiny performance improvement.

<img width="633" alt="image" src="https://github.com/user-attachments/assets/a2a65957-09a0-47a5-9b9f-eaec576220c1" />
@graphite-app graphite-app bot force-pushed the 04-28-perf_transformer_refresh_use_take_take_in_instead_of_drain branch from 25f8411 to 04db587 Compare April 29, 2025 16:40
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Apr 29, 2025
@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Apr 29, 2025
)

Calling `take` is cheaper than calling `drain`. These cases don't need to reuse the `Vec` after it drained, so we can replace them with `take`, which gives a tiny performance improvement.

<img width="633" alt="image" src="https://github.com/user-attachments/assets/a2a65957-09a0-47a5-9b9f-eaec576220c1" />
@Dunqing Dunqing force-pushed the 04-28-perf_transformer_refresh_use_take_take_in_instead_of_drain branch from 04db587 to 7d8efd8 Compare April 30, 2025 00:01
@graphite-app graphite-app bot merged commit 7d8efd8 into main Apr 30, 2025
26 checks passed
@graphite-app graphite-app bot deleted the 04-28-perf_transformer_refresh_use_take_take_in_instead_of_drain branch April 30, 2025 00:09
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Apr 30, 2025
graphite-app bot pushed a commit that referenced this pull request Apr 30, 2025
…0712)

Follow-on after #10656.

There's no need to consume `self.registrations` because we only need a reference to `binding` (`BoundIdentifier`), and the `persistent_id` is an `Atom`, which is `Copy`.

Just iterate over `&self.registrations`.
graphite-app bot pushed a commit that referenced this pull request Apr 30, 2025
Follow-on after #10656.

Remove the temporary `Vec` `new_statements`. Append new statements directly to `program.body` instead.

We get around the fact that the `VariableDeclaration` has to be inserted first by initially pushing a `VariableDeclaration` with no declarators, and then setting its declarators at the end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler C-performance Category - Solution not expected to change functional behavior, only performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants