Commit 707026b
perf(es/plugin): Use shared tokio runtime to avoid creation overhead (#11267)
## Summary
- Fixes #10926 by implementing `Lazy<Runtime>` to avoid creating a new
tokio runtime for each plugin call
- Reduces plugin execution overhead from ~19s to ~3s (6x improvement)
for builds with multiple plugins
## Background
Starting in v1.4.6, the plugin system experienced severe performance
degradation due to creating a fresh `tokio::runtime::Runtime` for every
plugin transformation when no existing runtime was available. This
involved expensive initialization of:
- Thread pool setup
- I/O driver initialization
- Timer infrastructure
For builds with multiple plugins and many files, this overhead
compounded exponentially.
## Changes
- Added a static `SHARED_RUNTIME: Lazy<tokio::runtime::Runtime>` that
initializes once and is reused across all plugin calls
- Modified `RustPlugins::apply()` to use the shared runtime instead of
creating a new one
- Only applies when `plugin` feature is enabled and
`manual-tokio-runtime` is NOT enabled
- Falls back to existing runtime context when available via
`tokio::runtime::Handle::try_current()`
## Performance Impact
This change eliminates the 6x performance regression introduced in
v1.4.6:
- **Before**: Each plugin call creates new runtime (~300ms overhead per
call)
- **After**: Runtime created once and reused (negligible overhead)
## Test Plan
- [x] Code compiles with `cargo check -p swc --features plugin`
- [x] Existing behavior preserved when tokio runtime context exists
- [x] Existing behavior preserved with `manual-tokio-runtime` feature
- [ ] Performance benchmarks confirm improvement
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude <[email protected]>1 parent 2f13add commit 707026b
2 files changed
Lines changed: 18 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| |||
24 | 26 | | |
25 | 27 | | |
26 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
27 | 38 | | |
28 | 39 | | |
29 | 40 | | |
| |||
85 | 96 | | |
86 | 97 | | |
87 | 98 | | |
88 | | - | |
| 99 | + | |
89 | 100 | | |
90 | 101 | | |
91 | 102 | | |
| |||
0 commit comments