Skip to content

Commit

Permalink
perf(NODE-6525): remove setPrototype and defineProperty from hot path (
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken authored Nov 11, 2024
1 parent 1965ed5 commit 48ed47e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
44 changes: 13 additions & 31 deletions src/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class ClientSession
* initially undefined. Gets set to false when startTransaction is called. When commitTransaction is sent to server, if the commitTransaction succeeds, it is then set to undefined, otherwise, set to true */
commitAttempted?: boolean;
/** @internal */
[kServerSession]: ServerSession | null;
private [kServerSession]: ServerSession | null;
/** @internal */
[kSnapshotTime]?: Timestamp;
/** @internal */
Expand Down Expand Up @@ -299,11 +299,8 @@ export class ClientSession
if (serverSession != null) {
// release the server session back to the pool
this.sessionPool.release(serverSession);
// Make sure a new serverSession never makes it onto this ClientSession
Object.defineProperty(this, kServerSession, {
value: ServerSession.clone(serverSession),
writable: false
});
// Store a clone of the server session for reference (debugging)
this[kServerSession] = new ServerSession(serverSession);
}
// mark the session as ended, and emit a signal
this.hasEnded = true;
Expand Down Expand Up @@ -973,7 +970,16 @@ export class ServerSession {
isDirty: boolean;

/** @internal */
constructor() {
constructor(cloned?: ServerSession | null) {
if (cloned != null) {
const idBytes = Buffer.allocUnsafe(16);
idBytes.set(cloned.id.id.buffer);
this.id = { id: new Binary(idBytes, cloned.id.id.sub_type) };
this.lastUse = cloned.lastUse;
this.txnNumber = cloned.txnNumber;
this.isDirty = cloned.isDirty;
return;
}
this.id = { id: new Binary(uuidV4(), Binary.SUBTYPE_UUID) };
this.lastUse = now();
this.txnNumber = 0;
Expand All @@ -994,30 +1000,6 @@ export class ServerSession {

return idleTimeMinutes > sessionTimeoutMinutes - 1;
}

/**
* @internal
* Cloning meant to keep a readable reference to the server session data
* after ClientSession has ended
*/
static clone(serverSession: ServerSession): Readonly<ServerSession> {
const arrayBuffer = new ArrayBuffer(16);
const idBytes = Buffer.from(arrayBuffer);
idBytes.set(serverSession.id.id.buffer);

const id = new Binary(idBytes, serverSession.id.id.sub_type);

// Manual prototype construction to avoid modifying the constructor of this class
return Object.setPrototypeOf(
{
id: { id },
lastUse: serverSession.lastUse,
txnNumber: serverSession.txnNumber,
isDirty: serverSession.isDirty
},
ServerSession.prototype
);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/benchmarks/driverBench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ benchmarkRunner
args: Object.fromEntries(
Object.entries(MONGODB_CLIENT_OPTIONS).map(([key, value]) => [
key,
typeof value === 'number' ? value | 0 : value ? 1 : 0
typeof value === 'number' ? value : value ? 1 : 0
])
)
},
Expand Down

0 comments on commit 48ed47e

Please sign in to comment.