Skip to content
Open
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
14 changes: 14 additions & 0 deletions packages/@jsii/kernel/src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export class Kernel {
readonly #waiting = new Map<string, Callback>();
readonly #promises = new Map<string, AsyncInvocation>();

// Cache for O(1) FQN-to-Type lookups (assemblies are immutable after load)
readonly #typeCache = new Map<string, spec.Type>();

readonly #serializerHost: wire.SerializerHost;

#nextid = 20000; // incrementing counter for objid, cbid, promiseid
Expand Down Expand Up @@ -587,6 +590,9 @@ export class Kernel {
#addAssembly(assm: Assembly) {
this.#assemblies.set(assm.metadata.name, assm);

// Invalidate type cache as assembly data may have changed
this.#typeCache.clear();

// We can use jsii runtime type information from jsii 1.19.0 onwards... Note that a version of
// 0.0.0 means we are assessing against a development tree, which is newer...
const jsiiVersion = assm.metadata.jsiiVersion.split(' ', 1)[0];
Expand Down Expand Up @@ -1109,6 +1115,12 @@ export class Kernel {
}

#typeInfoForFqn(fqn: spec.FQN): spec.Type {
// Check cache first for O(1) lookup
const cached = this.#typeCache.get(fqn);
if (cached) {
return cached;
}

const components = fqn.split('.');
const moduleName = components[0];

Expand All @@ -1123,6 +1135,8 @@ export class Kernel {
throw new JsiiFault(`Type '${fqn}' not found`);
}

// Cache for subsequent lookups (callers must not mutate returned objects)
this.#typeCache.set(fqn, fqnInfo);
return fqnInfo;
}

Expand Down
Loading