Skip to content

perf: napi communication#11132

Merged
SyMind merged 9 commits intomainfrom
perf-napi-communicate
Jul 24, 2025
Merged

perf: napi communication#11132
SyMind merged 9 commits intomainfrom
perf-napi-communicate

Conversation

@SyMind
Copy link
Member

@SyMind SyMind commented Jul 22, 2025

Summary

Enhance NAPI communication performance through the following optimizations:

  1. Reduce memory allocation in JavaScript thread: Use thread-local variables to store temporary data, avoiding frequent allocation/deallocation cycles
  2. Direct NAPI Array usage: Replace Vec with direct NAPI Array operations to eliminate unnecessary Vec memory allocations
  3. Non-blocking Arc cleanup: Use rayon::spawn for dropping Arc to prevent blocking the JavaScript thread
  4. RequestResult caching: Cache RequestResult objects to avoid repeated JSON.parse operations

In this Next.js benchmark case https://github.com/SyMind/shadcn-ui/tree/next-rspack, build performance improved from 16s to 15s.

Related links

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@github-actions github-actions bot added team The issue/pr is created by the member of Rspack. release: performance release: performance related release(mr only) labels Jul 22, 2025
@netlify
Copy link

netlify bot commented Jul 22, 2025

Deploy Preview for rspack canceled.

Name Link
🔨 Latest commit 7dd66c9
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/6880a04b6e6f820008a6d991

@github-actions
Copy link
Contributor

github-actions bot commented Jul 22, 2025

📦 Binary Size-limit

Comparing 7dd66c9 to fix: external resolve esm module (#11144) by Cong-Cong Pan

🎉 Size decreased by 15.63KB from 49.00MB to 48.98MB (⬇️0.03%)

@codspeed-hq
Copy link

codspeed-hq bot commented Jul 22, 2025

CodSpeed Performance Report

Merging #11132 will not alter performance

Comparing perf-napi-communicate (7dd66c9) with main (2622024)

Summary

✅ 16 untouched benchmarks

@SyMind SyMind force-pushed the perf-napi-communicate branch from baae46b to e702b5e Compare July 23, 2025 08:01
@SyMind SyMind force-pushed the perf-napi-communicate branch from e702b5e to 3cf3c57 Compare July 23, 2025 08:02
@github-actions
Copy link
Contributor

github-actions bot commented Jul 23, 2025

📝 Ecosystem CI detail: Open

suite result
rsbuild ❌ failure
examples ❌ failure
rspress ❌ failure
rslib ❌ failure
devserver ✅ success
plugin ✅ success
lynx-stack ❌ failure
rsdoctor ✅ success
modernjs ✅ success
nuxt ✅ success

@SyMind SyMind marked this pull request as ready for review July 23, 2025 08:42
Copilot AI review requested due to automatic review settings July 23, 2025 08:42
@SyMind SyMind requested review from h-a-n-a and hardfist as code owners July 23, 2025 08:42
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 implements a performance optimization for NAPI communication by changing data serialization strategy from structured objects to JSON strings and introducing memory pooling techniques to reduce allocations.

  • Replaces structured data passing with JSON serialization for NAPI communication
  • Introduces thread-local buffer pooling to reduce memory allocations
  • Optimizes data structure usage by replacing HashMap with UkeyMap for better performance
  • Adds caching mechanisms for external item processing

Reviewed Changes

Copilot reviewed 19 out of 21 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
packages/rspack/src/taps/normalModuleFactory.ts Changes parameter type from JsAfterResolveData to string and adds JSON parsing
packages/rspack/src/builtin-plugin/ExternalsPlugin.ts Adds caching, removes Compiler dependency, and optimizes JSON parsing
packages/rspack/etc/core.api.md Updates API signature to remove Compiler parameter
crates/rspack_core/src/module_graph/mod.rs Replaces HashMap with UkeyMap for dependency-related data structures
crates/rspack_binding_api/src/resource_data.rs Introduces thread-local buffer for property creation
crates/rspack_binding_api/src/raw_options/raw_external.rs Optimizes external item context handling with Arc and async cleanup
crates/rspack_binding_api/src/plugins/interceptor.rs Changes hook to use JSON string serialization
crates/rspack_binding_api/src/normal_module_factory.rs Adds Serialize derive to data structures
crates/rspack_binding_api/src/modules/* Introduces MODULE_PROPERTIES_BUFFER for property reuse
crates/rspack_binding_api/src/module_graph.rs Optimizes connection methods to reuse buffers and return Arrays
crates/rspack_binding_api/src/dependency.rs Optimizes array creation for dependency IDs
crates/rspack_binding_api/src/compilation/mod.rs Optimizes modules getter to return Array directly
crates/rspack_binding_api/src/build_info.rs Introduces thread-local buffer for build info properties
crates/rspack_binding_api/Cargo.toml Adds rayon dependency for async cleanup

@h-a-n-a h-a-n-a changed the title perf: napi communicate perf: napi communication Jul 23, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Jul 24, 2025

📝 Ecosystem CI detail: Open

suite result
rspress ✅ success
modernjs ✅ success
examples ✅ success
nuxt ✅ success
rslib ❌ failure
devserver ✅ success
rsdoctor ✅ success
lynx-stack ❌ failure
plugin ✅ success
rsbuild ✅ success

@SyMind SyMind merged commit 25000a7 into main Jul 24, 2025
83 of 84 checks passed
@SyMind SyMind deleted the perf-napi-communicate branch July 24, 2025 02:00
SyMind added a commit that referenced this pull request Jul 24, 2025
stormslowly added a commit that referenced this pull request Jul 24, 2025
stormslowly added a commit that referenced this pull request Jul 24, 2025
Revert "perf: napi communication (#11132)"

This reverts commit 25000a7.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: performance release: performance related release(mr only) team The issue/pr is created by the member of Rspack.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants