Skip to content

Commit

Permalink
perf optimizations, buffer index and deserialization fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Jun 28, 2022
1 parent 37c8edb commit fb76f7d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
11 changes: 10 additions & 1 deletion etc/benchmarks/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ console.log();

////////////////////////////////////////////////////////////////////////////////////////////////////
await runner({
skip: false,
skip: true,
name: 'deserialize({ oid, string }, { validation: { utf8: false } })',
iterations,
setup(libs) {
Expand Down Expand Up @@ -58,6 +58,15 @@ await runner({
}
});

await runner({
skip: false,
name: 'Double Serialization',
iterations,
run(i, bson) {
bson.lib.serialize({ d: 2.3 });
}
});

// End
console.log(
'Total time taken to benchmark:',
Expand Down
6 changes: 4 additions & 2 deletions src/parser/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,12 @@ function deserializeObject(
(buffer[index++] << 16) |
(buffer[index++] << 24);
} else if (elementType === constants.BSON_DATA_NUMBER && promoteValues === false) {
value = new Double(buffer.readDoubleLE(index));
const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
value = new Double(dv.getFloat64(index, true));
index = index + 8;
} else if (elementType === constants.BSON_DATA_NUMBER) {
value = buffer.readDoubleLE(index);
const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
value = dv.getFloat64(index, true);
index = index + 8;
} else if (elementType === constants.BSON_DATA_DATE) {
const lowBits =
Expand Down
12 changes: 10 additions & 2 deletions src/parser/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ function serializeString(
return index;
}

const SPACE_FOR_FLOAT64 = new Uint8Array(8);
const DV_FOR_FLOAT64 = new DataView(
SPACE_FOR_FLOAT64.buffer,
SPACE_FOR_FLOAT64.byteOffset,
SPACE_FOR_FLOAT64.byteLength
);
function serializeNumber(
buffer: Buffer,
key: string,
Expand Down Expand Up @@ -118,7 +124,8 @@ function serializeNumber(
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write float
buffer.writeDoubleLE(value, index);
DV_FOR_FLOAT64.setFloat64(0, value, true);
buffer.set(SPACE_FOR_FLOAT64, index);
// Adjust index
index = index + 8;
}
Expand Down Expand Up @@ -486,7 +493,8 @@ function serializeDouble(
buffer[index++] = 0;

// Write float
buffer.writeDoubleLE(value.value, index);
DV_FOR_FLOAT64.setFloat64(0, value.value, true);
buffer.set(SPACE_FOR_FLOAT64, index);

// Adjust index
index = index + 8;
Expand Down
4 changes: 2 additions & 2 deletions test/node/bson_corpus.spec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const Buffer = require('buffer').Buffer;
const BSON = require('../register-bson');
const { getNodeMajor } = require('./tools/utils');
const { getNodeMajor, isBrowser } = require('./tools/utils');
const BSONError = BSON.BSONError;
const EJSON = BSON.EJSON;

Expand Down Expand Up @@ -122,7 +122,7 @@ describe('BSON Corpus', function () {
describe('valid-bson', function () {
for (const v of valid) {
it(v.description, function () {
if (v.description === 'NaN with payload' && getNodeMajor() < 10) {
if (v.description === 'NaN with payload' && !isBrowser() && getNodeMajor() < 10) {
this.skip();
}

Expand Down
9 changes: 6 additions & 3 deletions test/node/double_tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const BSON = require('../register-bson');
const { getNodeMajor } = require('./tools/utils');
const { getNodeMajor, isBrowser } = require('./tools/utils');
const Double = BSON.Double;

describe('Double', function () {
Expand Down Expand Up @@ -64,11 +64,14 @@ describe('Double', function () {
});

it('NaN with payload', function () {
if (getNodeMajor() < 10) {
if (!isBrowser() && getNodeMajor() < 10) {
this.skip();
}
let buffer = Buffer.from('120000000000F87F', 'hex');
let value = buffer.readDoubleLE(0);

const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
let value = dv.getFloat64(0, true);

let serializedDouble = BSON.serialize({ d: value });
expect(serializedDouble.subarray(7, 15)).to.deep.equal(buffer);
let { d: newVal } = BSON.deserialize(serializedDouble, { promoteValues: true });
Expand Down

0 comments on commit fb76f7d

Please sign in to comment.