Skip to content
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
2 changes: 1 addition & 1 deletion crates/node_binding/napi-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export declare class JsCompilation {
}

export declare class JsCompiler {
constructor(compilerPath: string, options: RawOptions, builtinPlugins: Array<BuiltinPlugin>, registerJsTaps: RegisterJsTaps, outputFilesystem: ThreadsafeNodeFS, intermediateFilesystem: ThreadsafeNodeFS | undefined | null, inputFilesystem: ThreadsafeNodeFS | undefined | null, resolverFactoryReference: JsResolverFactory)
constructor(compilerPath: string, options: RawOptions, builtinPlugins: Array<BuiltinPlugin>, registerJsTaps: RegisterJsTaps, outputFilesystem: ThreadsafeNodeFS, intermediateFilesystem: ThreadsafeNodeFS | undefined | null, inputFilesystem: ThreadsafeNodeFS | undefined | null, resolverFactoryReference: JsResolverFactory, unsafeFastDrop: boolean)
setNonSkippableRegisters(kinds: Array<RegisterJsTapKind>): void
/** Build with the given option passed to the constructor */
build(callback: (err: null | Error) => void): void
Expand Down
18 changes: 14 additions & 4 deletions crates/rspack_binding_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ mod virtual_modules;

use std::{
cell::RefCell,
mem::ManuallyDrop,
sync::{Arc, RwLock},
};

Expand Down Expand Up @@ -159,8 +160,11 @@ fn cleanup_revoked_modules(ctx: CallContext) -> Result<()> {

#[napi(custom_finalize)]
struct JsCompiler {
// whether to skip drop compiler in finalize
unsafe_fast_drop: bool,
js_hooks_plugin: JsHooksAdapterPlugin,
compiler: Compiler,
// call drop manually to avoid unnecessary drop overhead in cli build
compiler: ManuallyDrop<Compiler>,
state: CompilerState,
include_dependencies_map: FxHashMap<String, FxHashMap<EntryOptions, BoxDependency>>,
entry_dependencies_map: FxHashMap<String, FxHashMap<EntryOptions, BoxDependency>>,
Expand All @@ -183,6 +187,7 @@ impl JsCompiler {
intermediate_filesystem: Option<ThreadsafeNodeFS>,
input_filesystem: Option<ThreadsafeNodeFS>,
mut resolver_factory_reference: Reference<JsResolverFactory>,
unsafe_fast_drop: bool,
) -> Result<Self> {
tracing::info!(name:"rspack_version", version = rspack_workspace::rspack_pkg_version!());
tracing::info!(name:"raw_options", options=?&options);
Expand Down Expand Up @@ -309,17 +314,17 @@ impl JsCompiler {
);

Ok(Self {
compiler: Compiler::from(rspack),
compiler: ManuallyDrop::new(Compiler::from(rspack)),
state: CompilerState::init(),
js_hooks_plugin,
include_dependencies_map: Default::default(),
entry_dependencies_map: Default::default(),
compiler_context,
virtual_file_store,
unsafe_fast_drop,
})
})
}

#[napi]
pub fn set_non_skippable_registers(&self, kinds: Vec<RegisterJsTapKind>) {
self.js_hooks_plugin.set_non_skippable_registers(kinds)
Expand Down Expand Up @@ -460,7 +465,7 @@ impl JsCompiler {
}

impl ObjectFinalize for JsCompiler {
fn finalize(self, _env: Env) -> Result<()> {
fn finalize(mut self, _env: Env) -> Result<()> {
let compiler_id = self.compiler.id();

COMPILER_REFERENCES.with(|ref_cell| {
Expand All @@ -469,6 +474,11 @@ impl ObjectFinalize for JsCompiler {
});

ModuleObject::cleanup_by_compiler_id(&compiler_id);
if (!self.unsafe_fast_drop) {
unsafe {
ManuallyDrop::drop(&mut self.compiler);
}
}
Ok(())
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/rspack-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export class RspackCLI {
let compiler: MultiCompiler | Compiler | null;
try {
compiler = rspack(config, isWatch ? callback : undefined);
if (!isWatch && compiler) {
// unsafeFastDrop is an internal option api and not shown in types
compiler.unsafeFastDrop = true;
}
} catch (e) {
// Aligned with webpack-cli
// See: https://github.com/webpack/webpack-cli/blob/eea6adf7d34dfbfd3b5b784ece4a4664834f5a6a/packages/webpack-cli/src/webpack-cli.ts#L2394
Expand Down
4 changes: 4 additions & 0 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,8 @@ export class Compiler {
runAsChild(callback: (err?: null | Error, entries?: Chunk[], compilation?: Compilation) => any): void;
// (undocumented)
running: boolean;
// @internal
unsafeFastDrop: boolean;
// (undocumented)
watch(watchOptions: Watchpack.WatchOptions, handler: liteTapable.Callback<Error, Stats>): Watching;
// (undocumented)
Expand Down Expand Up @@ -4823,6 +4825,8 @@ export class MultiCompiler {
// (undocumented)
setDependencies(compiler: Compiler, dependencies: string[]): void;
// (undocumented)
set unsafeFastDrop(value: boolean);
// (undocumented)
validateDependencies(callback: liteTapable.Callback<Error, MultiStats>): boolean;
// (undocumented)
watch(watchOptions: WatchOptions | WatchOptions[], handler: liteTapable.Callback<Error, MultiStats>): MultiWatching;
Expand Down
11 changes: 10 additions & 1 deletion packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ class Compiler {
cache: Cache;
compilerPath: string;
options: RspackOptionsNormalized;
/**
* Whether to skip dropping Rust compiler instance to improve performance.
* This is an internal option api and could be removed or changed at any time.
* @internal
* true: Skip dropping Rust compiler instance.
* false: Drop Rust compiler instance when Compiler is garbage collected.
*/
unsafeFastDrop: boolean = false;

/**
* Note: This is not a webpack public API, maybe removed in future.
Expand Down Expand Up @@ -881,7 +889,8 @@ class Compiler {
)
: undefined,
inputFileSystem,
ResolverFactory.__to_binding(this.resolverFactory)
ResolverFactory.__to_binding(this.resolverFactory),
this.unsafeFastDrop
);

callback(null, this.#instance);
Expand Down
6 changes: 5 additions & 1 deletion packages/rspack/src/MultiCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ export class MultiCompiler {
});
}
}

set unsafeFastDrop(value: boolean) {
for (const compiler of this.compilers) {
compiler.unsafeFastDrop = value;
}
}
get options() {
return Object.assign(
this.compilers.map(c => c.options),
Expand Down
Loading