Skip to content

Commit 5f0c8f2

Browse files
authored
refactor: Removes dead code paths from number serialization (#399)
Unnecessary control flow for number typing removed. NODE-2769
1 parent 2dd54e5 commit 5f0c8f2

File tree

2 files changed

+20
-61
lines changed

2 files changed

+20
-61
lines changed

src/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export const BSON_INT64_MIN = -Math.pow(2, 63);
77

88
// JS MAX PRECISE VALUES
99
// Any integer up to 2^53 can be precisely represented by a double.
10-
export const JS_INT_MAX = Number.MAX_SAFE_INTEGER + 1;
10+
export const JS_INT_MAX = Math.pow(2, 53);
1111
// Any integer down to -2^53 can be precisely represented by a double.
12-
export const JS_INT_MIN = Number.MIN_SAFE_INTEGER - 1;
12+
export const JS_INT_MIN = -Math.pow(2, 53);
1313

1414
/** Number BSON Type */
1515
export const BSON_DATA_NUMBER = 1;

src/parser/serializer.ts

Lines changed: 18 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -87,68 +87,27 @@ function serializeNumber(
8787
isArray?: boolean
8888
) {
8989
// We have an integer value
90-
// TODO(NODE-2769): Issue serializing large integers as doubles
9190
// TODO(NODE-2529): Add support for big int
9291
if (
93-
Math.floor(value) === value &&
94-
value >= constants.JS_INT_MIN &&
95-
value <= constants.JS_INT_MAX
92+
Number.isInteger(value) &&
93+
value >= constants.BSON_INT32_MIN &&
94+
value <= constants.BSON_INT32_MAX
9695
) {
97-
// If the value fits in 32 bits encode as int, if it fits in a double
98-
// encode it as a double, otherwise long
99-
if (value >= constants.BSON_INT32_MIN && value <= constants.BSON_INT32_MAX) {
100-
// Set int type 32 bits or less
101-
buffer[index++] = constants.BSON_DATA_INT;
102-
// Number of written bytes
103-
const numberOfWrittenBytes = !isArray
104-
? buffer.write(key, index, undefined, 'utf8')
105-
: buffer.write(key, index, undefined, 'ascii');
106-
// Encode the name
107-
index = index + numberOfWrittenBytes;
108-
buffer[index++] = 0;
109-
// Write the int value
110-
buffer[index++] = value & 0xff;
111-
buffer[index++] = (value >> 8) & 0xff;
112-
buffer[index++] = (value >> 16) & 0xff;
113-
buffer[index++] = (value >> 24) & 0xff;
114-
} else if (value >= constants.JS_INT_MIN && value <= constants.JS_INT_MAX) {
115-
// Encode as double
116-
buffer[index++] = constants.BSON_DATA_NUMBER;
117-
// Number of written bytes
118-
const numberOfWrittenBytes = !isArray
119-
? buffer.write(key, index, undefined, 'utf8')
120-
: buffer.write(key, index, undefined, 'ascii');
121-
// Encode the name
122-
index = index + numberOfWrittenBytes;
123-
buffer[index++] = 0;
124-
// Write float
125-
writeIEEE754(buffer, value, index, 'little', 52, 8);
126-
// Adjust index
127-
index = index + 8;
128-
} else {
129-
// Set long type
130-
buffer[index++] = constants.BSON_DATA_LONG;
131-
// Number of written bytes
132-
const numberOfWrittenBytes = !isArray
133-
? buffer.write(key, index, undefined, 'utf8')
134-
: buffer.write(key, index, undefined, 'ascii');
135-
// Encode the name
136-
index = index + numberOfWrittenBytes;
137-
buffer[index++] = 0;
138-
const longVal = Long.fromNumber(value);
139-
const lowBits = longVal.getLowBits();
140-
const highBits = longVal.getHighBits();
141-
// Encode low bits
142-
buffer[index++] = lowBits & 0xff;
143-
buffer[index++] = (lowBits >> 8) & 0xff;
144-
buffer[index++] = (lowBits >> 16) & 0xff;
145-
buffer[index++] = (lowBits >> 24) & 0xff;
146-
// Encode high bits
147-
buffer[index++] = highBits & 0xff;
148-
buffer[index++] = (highBits >> 8) & 0xff;
149-
buffer[index++] = (highBits >> 16) & 0xff;
150-
buffer[index++] = (highBits >> 24) & 0xff;
151-
}
96+
// If the value fits in 32 bits encode as int32
97+
// Set int type 32 bits or less
98+
buffer[index++] = constants.BSON_DATA_INT;
99+
// Number of written bytes
100+
const numberOfWrittenBytes = !isArray
101+
? buffer.write(key, index, undefined, 'utf8')
102+
: buffer.write(key, index, undefined, 'ascii');
103+
// Encode the name
104+
index = index + numberOfWrittenBytes;
105+
buffer[index++] = 0;
106+
// Write the int value
107+
buffer[index++] = value & 0xff;
108+
buffer[index++] = (value >> 8) & 0xff;
109+
buffer[index++] = (value >> 16) & 0xff;
110+
buffer[index++] = (value >> 24) & 0xff;
152111
} else {
153112
// Encode as double
154113
buffer[index++] = constants.BSON_DATA_NUMBER;

0 commit comments

Comments
 (0)