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: Slightly optimise object interner for larger codebase #5170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
31 changes: 27 additions & 4 deletions packages/compiler/src/emitter-framework/asset-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,14 +860,28 @@ function isDeclaration(type: Type): type is TypeSpecDeclaration | Namespace {
* could consider.
*/
function createInterner() {
type PlainObject = Record<string, any>;

const emptyObject = {};
const knownObjects: Set<Record<string, any>> = new Set();
const knownKeys = new Map<string, Set<PlainObject>>();

return {
intern<T extends Record<string, any>>(object: T): T {
const keyLen = Object.keys(object).length;
intern<T extends PlainObject>(object: T): T {
const keys = Object.keys(object);
const keyLen = keys.length;
if (keyLen === 0) return emptyObject as any;

// Find an object set with minimum size by object keys
let knownObjects = new Set<PlainObject>();
let minSize = Infinity;
for (const objs of keys.map((key) => knownKeys.get(key))) {
if (objs && objs.size < minSize) {
knownObjects = objs;
minSize = objs.size;
}
}

// Now find a known object from the found smallest object set
for (const ko of knownObjects) {
const entries = Object.entries(ko);
if (entries.length !== keyLen) continue;
Expand All @@ -885,7 +899,16 @@ function createInterner() {
}
}

knownObjects.add(object);
// If the object is not known, add all keys as known
for (const key of keys) {
const ko = knownKeys.get(key);
if (ko) {
ko.add(object);
} else {
knownKeys.set(key, new Set([object]));
}
}

return object;
},
};
Expand Down