Skip to content
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

perf: move from Buffer to zero-copy BufferSlice #8574

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export declare class EntryOptionsDto {
export type EntryOptionsDTO = EntryOptionsDto

export declare class JsCompilation {
updateAsset(filename: string, newSourceOrFunction: JsCompatSource | ((source: JsCompatSource) => JsCompatSource), assetInfoUpdateOrFunction?: JsAssetInfo | ((assetInfo: JsAssetInfo) => JsAssetInfo)): void
updateAsset(filename: string, newSourceOrFunction: JsCompatSource | ((source: JsCompatSourceOwned) => JsCompatSourceOwned), assetInfoUpdateOrFunction?: JsAssetInfo | ((assetInfo: JsAssetInfo) => JsAssetInfo)): void
getAssets(): Readonly<JsAsset>[]
getAsset(name: string): JsAsset | null
getAssetSource(name: string): JsCompatSource | null
Expand Down Expand Up @@ -526,11 +526,23 @@ export interface JsCodegenerationResults {
map: Record<string, Record<string, JsCodegenerationResult>>
}

/**
* Zero copy `JsCompatSource` slice shared between Rust and Node.js if buffer is used.
*
* It can only be used in non-async context and the lifetime is bound to the fn closure.
*
* If you want to use Node.js Buffer in async context or want to extend the lifetime, use `JsCompatSourceOwned` instead.
*/
export interface JsCompatSource {
source: string | Buffer
map?: string
}

export interface JsCompatSourceOwned {
source: string | Buffer
map?: string
}

export interface JsCreateData {
request: string
userRequest: string
Expand Down Expand Up @@ -781,7 +793,7 @@ export interface JsRuntimeGlobals {
}

export interface JsRuntimeModule {
source?: JsCompatSource
source?: JsCompatSourceOwned
moduleIdentifier: string
constructorName: string
name: string
Expand Down
4 changes: 2 additions & 2 deletions crates/node_binding/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rspack_binding_values::{
JsFactorizeOutput, JsModuleWrapper, JsNormalModuleFactoryCreateModuleArgs, JsResolveArgs,
JsResolveForSchemeArgs, JsResolveForSchemeOutput, JsResolveOutput, JsRuntimeGlobals,
JsRuntimeModule, JsRuntimeModuleArg, JsRuntimeRequirementInTreeArg,
JsRuntimeRequirementInTreeResult, ToJsCompatSource,
JsRuntimeRequirementInTreeResult, ToJsCompatSourceOwned,
};
use rspack_collections::IdentifierSet;
use rspack_core::{
Expand Down Expand Up @@ -1256,7 +1256,7 @@ impl CompilationRuntimeModule for CompilationRuntimeModuleTap {
source: Some(
module
.generate(compilation)?
.to_js_compat_source()
.to_js_compat_source_owned()
.unwrap_or_else(|err| panic!("Failed to generate runtime module source: {err}")),
),
module_identifier: module.identifier().to_string(),
Expand Down
22 changes: 13 additions & 9 deletions crates/rspack_binding_values/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ use rspack_plugin_runtime::RuntimeModuleFromJs;
use super::{JsFilename, PathWithInfo};
use crate::utils::callbackify;
use crate::JsAddingRuntimeModule;
use crate::JsCompatSource;
use crate::JsModuleGraph;
use crate::JsModuleWrapper;
use crate::JsStatsOptimizationBailout;
use crate::LocalJsFilename;
use crate::ToJsCompatSource;
use crate::{
chunk::JsChunk, JsAsset, JsAssetInfo, JsChunkGroup, JsCompatSource, JsPathData, JsStats,
};
use crate::ToJsCompatSource as _;
use crate::{chunk::JsChunk, JsAsset, JsAssetInfo, JsChunkGroup, JsPathData, JsStats};
use crate::{JsRspackDiagnostic, JsRspackError};

#[napi]
Expand Down Expand Up @@ -72,10 +71,11 @@ impl JsCompilation {
#[napi]
impl JsCompilation {
#[napi(
ts_args_type = r#"filename: string, newSourceOrFunction: JsCompatSource | ((source: JsCompatSource) => JsCompatSource), assetInfoUpdateOrFunction?: JsAssetInfo | ((assetInfo: JsAssetInfo) => JsAssetInfo)"#
ts_args_type = r#"filename: string, newSourceOrFunction: JsCompatSource | ((source: JsCompatSourceOwned) => JsCompatSourceOwned), assetInfoUpdateOrFunction?: JsAssetInfo | ((assetInfo: JsAssetInfo) => JsAssetInfo)"#
)]
pub fn update_asset(
&mut self,
env: &Env,
filename: String,
new_source_or_function: Either<JsCompatSource, Function<'_, JsCompatSource, JsCompatSource>>,
asset_info_update_or_function: Option<
Expand All @@ -90,8 +90,8 @@ impl JsCompilation {
let new_source = match new_source_or_function {
Either::A(new_source) => new_source.into(),
Either::B(new_source_fn) => {
let js_compat_source: JsCompatSource =
new_source_fn.call(original_source.to_js_compat_source()?)?;
let js_compat_source =
new_source_fn.call(original_source.to_js_compat_source(env)?)?;
js_compat_source.into()
}
};
Expand Down Expand Up @@ -147,13 +147,17 @@ impl JsCompilation {
}

#[napi]
pub fn get_asset_source(&self, name: String) -> Result<Option<JsCompatSource>> {
pub fn get_asset_source<'a>(
&self,
env: &'a Env,
name: String,
) -> Result<Option<JsCompatSource<'a>>> {
let compilation = self.as_ref()?;

compilation
.assets()
.get(&name)
.and_then(|v| v.source.as_ref().map(|s| s.to_js_compat_source()))
.and_then(|v| v.source.as_ref().map(|s| s.to_js_compat_source(env)))
.transpose()
}

Expand Down
16 changes: 11 additions & 5 deletions crates/rspack_binding_values/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ use rspack_plugin_runtime::RuntimeModuleFromJs;
use rspack_util::source_map::SourceMapKind;
use rustc_hash::FxHashMap as HashMap;

use super::{JsCompatSource, ToJsCompatSource};
use crate::{JsChunk, JsCodegenerationResults, JsDependenciesBlockWrapper, JsDependencyWrapper};
use super::JsCompatSourceOwned;
use crate::{
JsChunk, JsCodegenerationResults, JsCompatSource, JsDependenciesBlockWrapper,
JsDependencyWrapper, ToJsCompatSource,
};

#[derive(Default)]
#[napi(object)]
Expand Down Expand Up @@ -74,11 +77,14 @@ impl JsModule {
}

#[napi(getter)]
pub fn original_source(&mut self) -> napi::Result<Either<JsCompatSource, ()>> {
pub fn original_source<'a>(
&mut self,
env: &'a Env,
) -> napi::Result<Either<JsCompatSource<'a>, ()>> {
let module = self.as_ref()?;

Ok(match module.original_source() {
Some(source) => match source.to_js_compat_source().ok() {
Some(source) => match source.to_js_compat_source(env).ok() {
Some(s) => Either::A(s),
None => Either::B(()),
},
Expand Down Expand Up @@ -397,7 +403,7 @@ pub struct JsExecuteModuleArg {
#[derive(Default)]
#[napi(object)]
pub struct JsRuntimeModule {
pub source: Option<JsCompatSource>,
pub source: Option<JsCompatSourceOwned>,
pub module_identifier: String,
pub constructor_name: String,
pub name: String,
Expand Down
Loading
Loading