-
- Notifications
You must be signed in to change notification settings - Fork 1.3k
perf(plugin): Avoid data copy when transformation finished #11223
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
Conversation
🦋 Changeset detectedLatest 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 |
There was a problem hiding this 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 selftomut selffor ownership transfer - Moved instance cleanup and memory deallocation before extracting transform results
- Eliminated cloning by using
Arc::try_unwrapto extract the final result
Comments suppressed due to low confidence (1)
crates/swc_plugin_runner/src/transform_executor.rs:1
- Explicitly dropping
self.instancebefore callingArc::try_unwrapsuggests there may be a shared reference issue. However, since the method now consumesself(line 41), all fields will be dropped at the end of the function anyway. This explicitdroponly makes sense ifinstanceholds a reference totransform_result, but this relationship is not clear from the code. Consider adding a comment explaining why this explicit drop is necessary forArc::try_unwrapto succeed.
use std::{env, sync::Arc}; 💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
CodSpeed Performance ReportMerging #11223 will not alter performanceComparing Summary
|
There was a problem hiding this 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.
| let transformed_result = Arc::try_unwrap(self.transform_result) | ||
| .map_err(|_| { | ||
| anyhow!("Failed to unwrap Arc: other references to transform_result exist") | ||
| })? | ||
| .into_inner(); |
Copilot AI Nov 3, 2025
There was a problem hiding this comment.
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.
| 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(); |
Binary Sizes
Commit: 5d3877d |
Description:
When the transformation is done, we can make sure that the
transformed_resultwon't be used by wasm imports. So it's safe to fetch the data from it without cloning the bytes.