Skip to content

fix(NODE-5578): improve objectId deserialisation by skipping buffer allocation #615

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

Closed
Closed
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
22 changes: 21 additions & 1 deletion etc/benchmarks/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ await runner({
bson.lib.deserialize(documents[i], { validation: { utf8: false } });
}
});

await runner({
skip: false,
name: 'deserialize a document with lots of objectId',
iterations,
setup(libs) {
const bson = getCurrentLocalBSON(libs);
const entries = Array.from({ length: 100000 }, (_, i) => [
`${i}_${'a'.repeat(10)}`,
new bson.lib.ObjectId()
]);
const document = Object.fromEntries(entries);
const bytes = bson.lib.serialize(document);
return bytes;
},
run(i, bson, largeDocument) {
new bson.lib.deserialize(largeDocument);
}
});

////////////////////////////////////////////////////////////////////////////////////////////////////
await runner({
skip: true,
Expand Down Expand Up @@ -102,7 +122,7 @@ await runner({
/// nextBatch simulate
/// nextBatch: [ { string * 20 } * 1000 ] /// Garbage call
await runner({
skip: false,
skip: true,
name: 'deserialize a large batch of documents each with an array of many strings',
iterations,
setup(libs) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"format": "eslint --ext '.js,.ts' src test --fix",
"check:coverage": "nyc --check-coverage npm run check:node",
"prepare": "node etc/prepare.js",
"release": "standard-version -i HISTORY.md"
"release": "standard-version -i HISTORY.md",
"run:benchmark": "node ./etc/benchmarks/main.mjs"
}
}
4 changes: 1 addition & 3 deletions src/parser/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,7 @@ function deserializeObject(
value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
index = index + stringSize;
} else if (elementType === constants.BSON_DATA_OID) {
const oid = ByteUtils.allocate(12);
oid.set(buffer.subarray(index, index + 12));
value = new ObjectId(oid);
value = new ObjectId(buffer.subarray(index, index + 12));
index = index + 12;
} else if (elementType === constants.BSON_DATA_INT && promoteValues === false) {
value = new Int32(
Expand Down