Skip to content

Commit a5a8685

Browse files
committed
perf(NODE-6525): remove setPrototype and defineProperty from hot path
1 parent ed25d56 commit a5a8685

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

src/sessions.ts

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,8 @@ export class ClientSession
299299
if (serverSession != null) {
300300
// release the server session back to the pool
301301
this.sessionPool.release(serverSession);
302-
// Make sure a new serverSession never makes it onto this ClientSession
303-
Object.defineProperty(this, kServerSession, {
304-
value: ServerSession.clone(serverSession),
305-
writable: false
306-
});
302+
// Store a clone of the server session for reference (debugging)
303+
this[kServerSession] = new ServerSession(serverSession);
307304
}
308305
// mark the session as ended, and emit a signal
309306
this.hasEnded = true;
@@ -973,7 +970,18 @@ export class ServerSession {
973970
isDirty: boolean;
974971

975972
/** @internal */
976-
constructor() {
973+
constructor(
974+
cloned?: { id: ServerSessionId; lastUse: number; txnNumber: number; isDirty: boolean } | null
975+
) {
976+
if (cloned != null) {
977+
const idBytes = Buffer.allocUnsafe(16);
978+
idBytes.set(cloned.id.id.buffer);
979+
this.id = { id: new Binary(idBytes, cloned.id.id.sub_type) };
980+
this.lastUse = cloned.lastUse;
981+
this.txnNumber = cloned.txnNumber;
982+
this.isDirty = cloned.isDirty;
983+
return;
984+
}
977985
this.id = { id: new Binary(uuidV4(), Binary.SUBTYPE_UUID) };
978986
this.lastUse = now();
979987
this.txnNumber = 0;
@@ -994,30 +1002,6 @@ export class ServerSession {
9941002

9951003
return idleTimeMinutes > sessionTimeoutMinutes - 1;
9961004
}
997-
998-
/**
999-
* @internal
1000-
* Cloning meant to keep a readable reference to the server session data
1001-
* after ClientSession has ended
1002-
*/
1003-
static clone(serverSession: ServerSession): Readonly<ServerSession> {
1004-
const arrayBuffer = new ArrayBuffer(16);
1005-
const idBytes = Buffer.from(arrayBuffer);
1006-
idBytes.set(serverSession.id.id.buffer);
1007-
1008-
const id = new Binary(idBytes, serverSession.id.id.sub_type);
1009-
1010-
// Manual prototype construction to avoid modifying the constructor of this class
1011-
return Object.setPrototypeOf(
1012-
{
1013-
id: { id },
1014-
lastUse: serverSession.lastUse,
1015-
txnNumber: serverSession.txnNumber,
1016-
isDirty: serverSession.isDirty
1017-
},
1018-
ServerSession.prototype
1019-
);
1020-
}
10211005
}
10221006

10231007
/**

0 commit comments

Comments
 (0)