Skip to content

Commit 35da599

Browse files
committed
perf(core): optimize context switching with lazy caching
1 parent fcaa117 commit 35da599

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

packages/core/helpers/execution-context-host.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99

1010
export class ExecutionContextHost implements ExecutionContext {
1111
private contextType = 'http';
12+
private httpCache?: HttpArgumentsHost;
13+
private rpcCache?: RpcArgumentsHost;
14+
private wsCache?: WsArgumentsHost;
1215

1316
constructor(
1417
private readonly args: any[],
@@ -41,25 +44,34 @@ export class ExecutionContextHost implements ExecutionContext {
4144
}
4245

4346
switchToRpc(): RpcArgumentsHost {
44-
return Object.assign(this, {
45-
getData: () => this.getArgByIndex(0),
46-
getContext: () => this.getArgByIndex(1),
47-
});
47+
if (!this.rpcCache) {
48+
this.rpcCache = Object.assign(this, {
49+
getData: () => this.getArgByIndex(0),
50+
getContext: () => this.getArgByIndex(1),
51+
});
52+
}
53+
return this.rpcCache;
4854
}
4955

5056
switchToHttp(): HttpArgumentsHost {
51-
return Object.assign(this, {
52-
getRequest: () => this.getArgByIndex(0),
53-
getResponse: () => this.getArgByIndex(1),
54-
getNext: () => this.getArgByIndex(2),
55-
});
57+
if (!this.httpCache) {
58+
this.httpCache = Object.assign(this, {
59+
getRequest: () => this.getArgByIndex(0),
60+
getResponse: () => this.getArgByIndex(1),
61+
getNext: () => this.getArgByIndex(2),
62+
});
63+
}
64+
return this.httpCache;
5665
}
5766

5867
switchToWs(): WsArgumentsHost {
59-
return Object.assign(this, {
60-
getClient: () => this.getArgByIndex(0),
61-
getData: () => this.getArgByIndex(1),
62-
getPattern: () => this.getArgByIndex(this.getArgs().length - 1),
63-
});
68+
if (!this.wsCache) {
69+
this.wsCache = Object.assign(this, {
70+
getClient: () => this.getArgByIndex(0),
71+
getData: () => this.getArgByIndex(1),
72+
getPattern: () => this.getArgByIndex(this.getArgs().length - 1),
73+
});
74+
}
75+
return this.wsCache;
6476
}
6577
}

packages/core/test/helpers/execution-context-host.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ describe('ExecutionContextHost', () => {
4848
expect(proxy.getData()).to.be.eq(args[0]);
4949
expect(proxy.getContext()).to.be.eq(args[1]);
5050
});
51+
52+
it('should return cached proxy on subsequent calls', () => {
53+
const proxy1 = contextHost.switchToRpc();
54+
const proxy2 = contextHost.switchToRpc();
55+
expect(proxy1).to.equal(proxy2);
56+
});
5157
});
5258

5359
describe('switchToHttp', () => {
@@ -60,6 +66,12 @@ describe('ExecutionContextHost', () => {
6066
expect(proxy.getResponse()).to.be.eq(args[1]);
6167
expect(proxy.getNext()).to.be.eq(args[2]);
6268
});
69+
70+
it('should return cached proxy on subsequent calls', () => {
71+
const proxy1 = contextHost.switchToHttp();
72+
const proxy2 = contextHost.switchToHttp();
73+
expect(proxy1).to.equal(proxy2);
74+
});
6375
});
6476

6577
describe('switchToWs', () => {
@@ -70,5 +82,11 @@ describe('ExecutionContextHost', () => {
7082
expect(proxy.getClient()).to.be.eq(args[0]);
7183
expect(proxy.getData()).to.be.eq(args[1]);
7284
});
85+
86+
it('should return cached proxy on subsequent calls', () => {
87+
const proxy1 = contextHost.switchToWs();
88+
const proxy2 = contextHost.switchToWs();
89+
expect(proxy1).to.equal(proxy2);
90+
});
7391
});
7492
});

0 commit comments

Comments
 (0)