Skip to content

Conversation

@CPunisher
Copy link
Member

Description:

When the transformation is done, we can make sure that the transformed_result won't be used by wasm imports. So it's safe to fetch the data from it without cloning the bytes.

Copilot AI review requested due to automatic review settings November 3, 2025 09:52
@CPunisher CPunisher requested a review from a team as a code owner November 3, 2025 09:52
@changeset-bot
Copy link

changeset-bot bot commented Nov 3, 2025

🦋 Changeset detected

Latest commit: 992ac65

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the PluginTransformState::run method to take ownership of self instead of a mutable reference, enabling the extraction of transform_result from Arc<Mutex<T>> without cloning. The cleanup operations are also moved earlier in the execution flow.

  • Changed method signature from &mut self to mut self for ownership transfer
  • Moved instance cleanup and memory deallocation before extracting transform results
  • Eliminated cloning by using Arc::try_unwrap to extract the final result
Comments suppressed due to low confidence (1)

crates/swc_plugin_runner/src/transform_executor.rs:1

  • Explicitly dropping self.instance before calling Arc::try_unwrap suggests there may be a shared reference issue. However, since the method now consumes self (line 41), all fields will be dropped at the end of the function anyway. This explicit drop only makes sense if instance holds a reference to transform_result, but this relationship is not clear from the code. Consider adding a comment explaining why this explicit drop is necessary for Arc::try_unwrap to succeed.
use std::{env, sync::Arc}; 

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 3, 2025

CodSpeed Performance Report

Merging #11223 will not alter performance

Comparing CPunisher:11-03-perf/plugin-ast-copy (992ac65) with main (b9aeaa3)

Summary

✅ 138 untouched

@kdy1 kdy1 changed the title perf(plugin): avoid data copy when transformation finished perf(plugin): Avoid data copy when transformation finished Nov 3, 2025
Copilot AI review requested due to automatic review settings November 3, 2025 10:06
@kdy1 kdy1 added this to the Planned milestone Nov 3, 2025
@kdy1 kdy1 enabled auto-merge (squash) November 3, 2025 10:07
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +79 to +83
let transformed_result = Arc::try_unwrap(self.transform_result)
.map_err(|_| {
anyhow!("Failed to unwrap Arc: other references to transform_result exist")
})?
.into_inner();
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

The Arc::try_unwrap may fail even after dropping self.instance because the instance's import object closures hold cloned references to transform_env, which in turn holds a clone of the transform_result Arc. The instance's Drop implementation and the cleanup() method (which has a default no-op implementation) don't guarantee that these closures are released. Consider keeping the original .lock().clone() approach, or ensure the runtime implementation properly releases all import object closures during cleanup/drop.

Suggested change
let transformed_result = Arc::try_unwrap(self.transform_result)
.map_err(|_| {
anyhow!("Failed to unwrap Arc: other references to transform_result exist")
})?
.into_inner();
// Instead of trying to unwrap the Arc, clone the inner value.
let transformed_result = self.transform_result.lock().clone();
Copilot uses AI. Check for mistakes.
@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2025

Binary Sizes

File Size
swc.linux-x64-gnu.node 31M (31933576 bytes)

Commit: 5d3877d

@kdy1 kdy1 merged commit af134fa into swc-project:main Nov 3, 2025
190 checks passed
@kdy1 kdy1 modified the milestones: Planned, 1.15.0 Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants