Skip to content

Commit

Permalink
fix(jsii-reflect): getAncestors takes a long time
Browse files Browse the repository at this point in the history
Memoize the call so repeated calls are faster.
  • Loading branch information
mrgrain committed Nov 7, 2023
1 parent 90ce1da commit 0073c3e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/jsii-reflect/lib/class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as jsii from '@jsii/spec';

import { memoized } from './_memoized';
import { Assembly } from './assembly';
import { Initializer } from './initializer';
import { InterfaceType } from './interface';
Expand All @@ -20,6 +21,7 @@ export class ClassType extends ReferenceType {
/**
* Base class (optional).
*/
@memoized
public get base(): ClassType | undefined {
if (!this.spec.base) {
return undefined;
Expand Down Expand Up @@ -60,12 +62,22 @@ export class ClassType extends ReferenceType {

/**
* Returns list of all base classes (first is the direct base and last is the top-most).
*
* @deprecated use ClassType.ancestors instead
*/
public getAncestors() {
return this.ancestors;
}

/**
* Returns list of all base classes (first is the direct base and last is the top-most).
*/
@memoized
public get ancestors() {
const out = new Array<ClassType>();
if (this.base) {
out.push(this.base);
out.push(...this.base.getAncestors());
out.push(...this.base.ancestors);
}
return out;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/jsii-reflect/lib/method.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as jsii from '@jsii/spec';

import { memoized } from './_memoized';
import { Assembly } from './assembly';
import { Callable } from './callable';
import { Documentable } from './docs';
Expand Down Expand Up @@ -42,6 +43,7 @@ export class Method
return this.spec.name;
}

@memoized
public get overrides(): Type | undefined {
if (!this.spec.overrides) {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/jsii-reflect/lib/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export abstract class Type implements Documentable, SourceLocatable {
return this.getInterfaces(true).some((iface) => iface === base);
}
if (this.isClassType() && base.isClassType()) {
return this.getAncestors().some((clazz) => clazz === base);
return this.ancestors.some((clazz) => clazz === base);
}
return false;
}
Expand Down

0 comments on commit 0073c3e

Please sign in to comment.