From ecc4dfc40a202d988274a234559ea3a767a98842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Boquet?= Date: Thu, 24 Aug 2023 14:36:42 +0200 Subject: [PATCH] Improve objectId deserialisation --- etc/benchmarks/main.mjs | 22 +++++++++++++++++++++- package.json | 3 ++- src/parser/deserializer.ts | 4 +--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/etc/benchmarks/main.mjs b/etc/benchmarks/main.mjs index 6f48cb1f..db1889bf 100644 --- a/etc/benchmarks/main.mjs +++ b/etc/benchmarks/main.mjs @@ -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, @@ -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) { diff --git a/package.json b/package.json index 426a26e9..22809701 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/parser/deserializer.ts b/src/parser/deserializer.ts index abb61046..70aec90f 100644 --- a/src/parser/deserializer.ts +++ b/src/parser/deserializer.ts @@ -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(