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 b66ee75
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
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
6 changes: 5 additions & 1 deletion packages/rspack/src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ const BUILD_META_MAPPINGS = new Map<string, Record<string, any>>();

export class Module {
#inner: JsModule;
#identifier: string | undefined;

declare readonly context?: string;
declare readonly resource?: string;
Expand Down Expand Up @@ -372,7 +373,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
18 changes: 14 additions & 4 deletions packages/rspack/src/ModuleGraphConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class ModuleGraphConnection {
declare readonly dependency: Dependency;

#inner: JsModuleGraphConnection;
#dependency: Dependency | undefined;
#resolvedModule: Module | undefined | null;

static __from_binding(binding: JsModuleGraphConnection) {
let connection = MODULE_GRAPH_CONNECTION_MAPPINGS.get(binding);
Expand Down Expand Up @@ -39,19 +41,27 @@ export class ModuleGraphConnection {
},
dependency: {
enumerable: true,
get(): Dependency {
return bindingDependencyFactory.create(
get: (): Dependency => {
if (this.#dependency !== undefined) {
return this.#dependency;
}
this.#dependency = bindingDependencyFactory.create(
Dependency,
binding.dependency
);
return this.#dependency;
}
},
resolvedModule: {
enumerable: true,
get(): Module | null {
return binding.resolvedModule
get: (): Module | null => {
if (this.#resolvedModule !== undefined) {
return this.#resolvedModule;
}
this.#resolvedModule = binding.resolvedModule
? Module.__from_binding(binding.resolvedModule)
: null;
return this.#resolvedModule;
}
},
originModule: {
Expand Down

0 comments on commit b66ee75

Please sign in to comment.