-
Notifications
You must be signed in to change notification settings - Fork 258
perf(NODE-6246): Significantly improve memory usage and performance of ObjectId #703
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
Changes from all commits
8986b69
4c5d5dc
1f07ba1
296ac15
21dc61d
80526c7
b858ab1
32e0a9b
639cc3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,10 +231,9 @@ function serializeObjectId(buffer: Uint8Array, key: string, value: ObjectId, ind | |
// Write the type | ||
buffer[index++] = constants.BSON_DATA_OID; | ||
// Number of written bytes | ||
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index); | ||
index += ByteUtils.encodeUTF8Into(buffer, key, index); | ||
|
||
// Encode the name | ||
index = index + numberOfWrittenBytes; | ||
buffer[index++] = 0; | ||
|
||
index += value.serializeInto(buffer, index); | ||
|
@@ -647,6 +646,8 @@ export function serializeInto( | |
index = serializeNull(buffer, key, value, index); | ||
} else if (value === null) { | ||
index = serializeNull(buffer, key, value, index); | ||
} else if (value._bsontype === 'ObjectId') { | ||
index = serializeObjectId(buffer, key, value, index); | ||
} else if (isUint8Array(value)) { | ||
index = serializeBuffer(buffer, key, value, index); | ||
} else if (value instanceof RegExp || isRegExp(value)) { | ||
|
@@ -668,8 +669,6 @@ export function serializeInto( | |
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION | ||
) { | ||
throw new BSONVersionError(); | ||
} else if (value._bsontype === 'ObjectId') { | ||
index = serializeObjectId(buffer, key, value, index); | ||
} else if (value._bsontype === 'Decimal128') { | ||
index = serializeDecimal128(buffer, key, value, index); | ||
} else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') { | ||
|
@@ -757,6 +756,8 @@ export function serializeInto( | |
index = serializeDate(buffer, key, value, index); | ||
} else if (value === null || (value === undefined && ignoreUndefined === false)) { | ||
index = serializeNull(buffer, key, value, index); | ||
} else if (value._bsontype === 'ObjectId') { | ||
index = serializeObjectId(buffer, key, value, index); | ||
Comment on lines
+759
to
+760
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the motivation for moving this step up here? seems like it may have broken the version check that comes later for this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did see a measurable performance gain by moving the ObjectId check to the top There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update: I narrowed the performance impact to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A shared copy of the symbol sounds like a good call. As long as the version check is still performed, moving probably the most common BSON type into a position where it performs better sounds good to me. |
||
} else if (isUint8Array(value)) { | ||
index = serializeBuffer(buffer, key, value, index); | ||
} else if (value instanceof RegExp || isRegExp(value)) { | ||
|
@@ -778,8 +779,6 @@ export function serializeInto( | |
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION | ||
) { | ||
throw new BSONVersionError(); | ||
} else if (value._bsontype === 'ObjectId') { | ||
index = serializeObjectId(buffer, key, value, index); | ||
} else if (type === 'object' && value._bsontype === 'Decimal128') { | ||
index = serializeDecimal128(buffer, key, value, index); | ||
} else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') { | ||
|
@@ -867,6 +866,8 @@ export function serializeInto( | |
if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index); | ||
} else if (value === null) { | ||
index = serializeNull(buffer, key, value, index); | ||
} else if (value._bsontype === 'ObjectId') { | ||
index = serializeObjectId(buffer, key, value, index); | ||
} else if (isUint8Array(value)) { | ||
index = serializeBuffer(buffer, key, value, index); | ||
} else if (value instanceof RegExp || isRegExp(value)) { | ||
|
@@ -888,8 +889,6 @@ export function serializeInto( | |
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION | ||
) { | ||
throw new BSONVersionError(); | ||
} else if (value._bsontype === 'ObjectId') { | ||
index = serializeObjectId(buffer, key, value, index); | ||
} else if (type === 'object' && value._bsontype === 'Decimal128') { | ||
index = serializeDecimal128(buffer, key, value, index); | ||
} else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,7 +170,7 @@ export const webByteUtils = { | |
return Uint8Array.from(buffer); | ||
}, | ||
|
||
toHex(uint8array: Uint8Array): string { | ||
toHex(uint8array: Uint8Array, _start?: number, _end?: number): string { | ||
return Array.from(uint8array, byte => byte.toString(16).padStart(2, '0')).join(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this call |
||
}, | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.