Skip to content

perf(NODE-6525): remove setPrototype and defineProperty from hot path #4321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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