Skip to content

Commit

Permalink
perf: cache immutable js field
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Jan 7, 2025
1 parent 8ded34b commit 0496ac8
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 68 deletions.
14 changes: 7 additions & 7 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,26 +220,26 @@ export declare class JsExportsInfo {

export declare class JsModule {
get constructorName(): string
get context(): string | undefined
get context(): string | null
get originalSource(): JsCompatSource | undefined
get resource(): string | undefined
get resource(): string | null
get moduleIdentifier(): string
get nameForCondition(): string | undefined
get request(): string | undefined
get request(): string | null
get userRequest(): string | undefined
set userRequest(val: string)
get rawRequest(): string | undefined
get rawRequest(): string | null
get factoryMeta(): JsFactoryMeta | undefined
get type(): string
get layer(): string | undefined
get layer(): string | null
get blocks(): JsDependenciesBlock[]
get dependencies(): JsDependency[]
size(ty?: string | undefined | null): number
get modules(): JsModule[] | undefined
get useSourceMap(): boolean
libIdent(options: JsLibIdentOptions): string | null
get resourceResolveData(): JsResourceData | undefined
get matchResource(): string | undefined
get resourceResolveData(): JsResourceData | null
get matchResource(): string | null
get loaders(): Array<string> | undefined
}

Expand Down
44 changes: 22 additions & 22 deletions crates/rspack_binding_values/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ impl JsModule {
}

#[napi(getter)]
pub fn context(&mut self) -> napi::Result<Either<String, ()>> {
pub fn context(&mut self) -> napi::Result<Option<String>> {
let module = self.as_ref()?;

Ok(match module.get_context() {
Some(ctx) => Either::A(ctx.to_string()),
None => Either::B(()),
Some(ctx) => Some(ctx.to_string()),
None => None,
})
}

Expand All @@ -103,12 +103,12 @@ impl JsModule {
}

#[napi(getter)]
pub fn resource(&mut self) -> napi::Result<Either<&String, ()>> {
pub fn resource(&mut self) -> napi::Result<Option<&String>> {
let module = self.as_ref()?;

Ok(match module.try_as_normal_module() {
Ok(normal_module) => Either::A(&normal_module.resource_resolved_data().resource),
Err(_) => Either::B(()),
Ok(normal_module) => Some(&normal_module.resource_resolved_data().resource),
Err(_) => None,
})
}

Expand All @@ -130,12 +130,12 @@ impl JsModule {
}

#[napi(getter)]
pub fn request(&mut self) -> napi::Result<Either<&str, ()>> {
pub fn request(&mut self) -> napi::Result<Option<&str>> {
let module = self.as_ref()?;

Ok(match module.try_as_normal_module() {
Ok(normal_module) => Either::A(normal_module.request()),
Err(_) => Either::B(()),
Ok(normal_module) => Some(normal_module.request()),
Err(_) => None,
})
}

Expand All @@ -160,12 +160,12 @@ impl JsModule {
}

#[napi(getter)]
pub fn raw_request(&mut self) -> napi::Result<Either<&str, ()>> {
pub fn raw_request(&mut self) -> napi::Result<Option<&str>> {
let module = self.as_ref()?;

Ok(match module.try_as_normal_module() {
Ok(normal_module) => Either::A(normal_module.raw_request()),
Err(_) => Either::B(()),
Ok(normal_module) => Some(normal_module.raw_request()),
Err(_) => None,
})
}

Expand All @@ -192,12 +192,12 @@ impl JsModule {
}

#[napi(getter)]
pub fn layer(&mut self) -> napi::Result<Either<&String, ()>> {
pub fn layer(&mut self) -> napi::Result<Option<&String>> {
let module = self.as_ref()?;

Ok(match module.get_layer() {
Some(layer) => Either::A(layer),
None => Either::B(()),
Some(layer) => Some(layer),
None => None,
})
}

Expand Down Expand Up @@ -310,23 +310,23 @@ impl JsModule {
}

#[napi(getter)]
pub fn resource_resolve_data(&mut self) -> napi::Result<Either<JsResourceData, ()>> {
pub fn resource_resolve_data(&mut self) -> napi::Result<Option<JsResourceData>> {
let module = self.as_ref()?;
Ok(match module.as_normal_module() {
Some(module) => Either::A(module.resource_resolved_data().into()),
None => Either::B(()),
Some(module) => Some(module.resource_resolved_data().into()),
None => None,
})
}

#[napi(getter)]
pub fn match_resource(&mut self) -> napi::Result<Either<&String, ()>> {
pub fn match_resource(&mut self) -> napi::Result<Option<&String>> {
let module = self.as_ref()?;
Ok(match module.as_normal_module() {
Some(module) => match &module.match_resource() {
Some(match_resource) => Either::A(&match_resource.resource),
None => Either::B(()),
Some(match_resource) => Some(&match_resource.resource),
None => None,
},
None => Either::B(()),
None => None,
})
}

Expand Down
23 changes: 15 additions & 8 deletions packages/rspack/src/Dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,27 @@ export const bindingDependencyFactory = {
};

export class Dependency {
#type: string | undefined;
#category: string | undefined;

get type(): string {
const binding = bindingDependencyFactory.getBinding(this);
if (binding) {
return binding.type;
if (this.#type === undefined) {
const binding = bindingDependencyFactory.getBinding(this);
if (binding) {
this.#type = binding.type;
}
}
return "unknown";
return this.#type || "unknown";
}

get category(): string {
const binding = bindingDependencyFactory.getBinding(this);
if (binding) {
return binding.category;
if (this.#category === undefined) {
const binding = bindingDependencyFactory.getBinding(this);
if (binding) {
this.#category = binding.category;
}
}
return "unknown";
return this.#category || "unknown";
}

get request(): string | undefined {
Expand Down
82 changes: 59 additions & 23 deletions packages/rspack/src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,23 @@ const BUILD_META_MAPPINGS = new Map<string, Record<string, any>>();

export class Module {
#inner: JsModule;

declare readonly context?: string;
declare readonly resource?: string;
declare readonly request?: string;
#identifier: string | undefined;
#type: string | undefined;
#layer: string | undefined | null;
#context: string | undefined | null;
#resource: string | undefined | null;
#request: string | undefined | null;
#rawRequest: string | undefined | null;
#resourceResolveData: ResolveData | undefined | null;
#matchResource: string | undefined | null;

declare readonly context: string | null;
declare readonly resource: string | null;
declare readonly request: string | null;
declare userRequest?: string;
declare readonly rawRequest?: string;
declare readonly rawRequest: string | null;
declare readonly type: string;
declare readonly layer: null | string;
declare readonly layer: string | null;
declare readonly factoryMeta?: JsFactoryMeta;

declare readonly modules: Module[] | undefined;
Expand Down Expand Up @@ -263,32 +272,47 @@ export class Module {
Object.defineProperties(this, {
type: {
enumerable: true,
get(): string | null {
return module.type || null;
get: (): string => {
if (this.#type === undefined) {
this.#type = module.type;
}
return this.#type;
}
},
layer: {
enumerable: true,
get(): string | undefined {
return module.layer;
get: (): string | null => {
if (this.#layer === undefined) {
this.#layer = module.layer;
}
return this.#layer;
}
},
context: {
enumerable: true,
get(): string | undefined {
return module.context;
get: (): string | null => {
if (this.#context === undefined) {
this.#context = module.context;
}
return this.#context;
}
},
resource: {
enumerable: true,
get(): string | undefined {
return module.resource;
get: (): string | null => {
if (this.#resource === undefined) {
this.#resource = module.resource;
}
return this.#resource;
}
},
request: {
enumerable: true,
get(): string | undefined {
return module.request;
get: (): string | null => {
if (this.#request === undefined) {
this.#request = module.request;
}
return this.#request;
}
},
userRequest: {
Expand All @@ -302,8 +326,11 @@ export class Module {
},
rawRequest: {
enumerable: true,
get(): string | undefined {
return module.rawRequest;
get: (): string | null => {
if (this.#rawRequest === undefined) {
this.#rawRequest = module.rawRequest;
}
return this.#rawRequest;
}
},
factoryMeta: {
Expand Down Expand Up @@ -351,14 +378,20 @@ export class Module {
},
resourceResolveData: {
enumerable: true,
get(): ResolveData | undefined {
return module.resourceResolveData as any;
get: (): ResolveData | null => {
if (this.#resourceResolveData === undefined) {
this.#resourceResolveData = module.resourceResolveData as any;
}
return this.#resourceResolveData!;
}
},
matchResource: {
enumerable: true,
get(): string | undefined {
return module.matchResource;
get: (): string | null => {
if (this.#matchResource === undefined) {
this.#matchResource = module.matchResource;
}
return this.#matchResource;
}
}
});
Expand All @@ -372,7 +405,10 @@ export class Module {
}

identifier(): string {
return this.#inner.moduleIdentifier;
if (this.#identifier === undefined) {
this.#identifier = this.#inner.moduleIdentifier;
}
return this.#identifier;
}

nameForCondition(): string | null {
Expand Down
Loading

0 comments on commit 0496ac8

Please sign in to comment.