-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jsii-reflect): TypeSystem can be locked to improve reflection pe…
…rformance (#4318) Memoizes additional calls that rely on the typesystem to retrieve type instances. These calls can (theoretically) change when the typesystem is changed. Therefore we cannot assume it's okay to always memoize the first call. To workaround this limitation, we introduce a new mechanism to manually `lock` the typesystem once all assemblies are loaded. We then can start memoizing the additional calls. For example in `awslint` this reduces the runtime against `aws-cdk-lib` by ~20s. --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
- Loading branch information
Showing
6 changed files
with
163 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { TypeSystem } from '../lib'; | ||
import { memoized, memoizedWhenLocked } from '../lib/_memoized'; | ||
|
||
const accessorSpy = jest.fn(() => 'foobar'); | ||
|
||
class TestClass { | ||
public constructor(public readonly system: TypeSystem) {} | ||
|
||
public get uncached(): string { | ||
return accessorSpy(); | ||
} | ||
|
||
@memoized | ||
public get cached(): string { | ||
return accessorSpy(); | ||
} | ||
|
||
@memoizedWhenLocked | ||
public get cachedWhenLocked(): string { | ||
return accessorSpy(); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
function noop(_val: unknown) {} | ||
|
||
describe('memoized', () => { | ||
beforeEach(() => { | ||
accessorSpy.mockClear(); | ||
}); | ||
const subject = new TestClass(new TypeSystem()); | ||
|
||
test('cached property is memoized', () => { | ||
// Access the property twice | ||
noop(subject.cached); | ||
noop(subject.cached); | ||
|
||
expect(accessorSpy).toHaveBeenCalledTimes(1); | ||
expect(subject.cached).toBe('foobar'); | ||
}); | ||
|
||
test('uncached property is not memoized', () => { | ||
// Access the property twice | ||
noop(subject.uncached); | ||
noop(subject.uncached); | ||
|
||
expect(accessorSpy).toHaveBeenCalledTimes(2); | ||
expect(subject.uncached).toBe('foobar'); | ||
}); | ||
}); | ||
|
||
describe('memoizedWhenLocked', () => { | ||
let subject: TestClass; | ||
beforeEach(() => { | ||
accessorSpy.mockClear(); | ||
subject = new TestClass(new TypeSystem()); | ||
}); | ||
|
||
test('property is memoized when the typesystem is locked', () => { | ||
// Lock the typesystem to enable memoizing | ||
subject.system.lock(); | ||
|
||
// Access the property twice | ||
noop(subject.cachedWhenLocked); | ||
noop(subject.cachedWhenLocked); | ||
|
||
expect(accessorSpy).toHaveBeenCalledTimes(1); | ||
expect(subject.cachedWhenLocked).toBe('foobar'); | ||
}); | ||
|
||
test('property is not memoized when the typesystem is not locked', () => { | ||
// Access the property twice | ||
noop(subject.cachedWhenLocked); | ||
noop(subject.cachedWhenLocked); | ||
|
||
expect(accessorSpy).toHaveBeenCalledTimes(2); | ||
expect(subject.cachedWhenLocked).toBe('foobar'); | ||
}); | ||
}); |