Skip to content

perf(NODE-6616): shortcircuit logging ejson.stringify #4377

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

Merged
merged 19 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
address review comments
  • Loading branch information
W-A-James committed Jan 23, 2025
commit d99e3b7b1fc92c58d3ffb81db3591df8bfda867e
6 changes: 4 additions & 2 deletions src/mongo_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,9 @@ export function stringifyWithMaxLen(
currentLength += String(v.value).length;
break;
case 'Double':
// Doesn't account for representing integers as <value>.0
currentLength += String(v.value).length;
// Account for representing integers as <value>.0
currentLength +=
(v.value | 0) === v.value ? String(v.value).length + 2 : String(v.value).length;
break;
case 'Long':
currentLength += v.toString().length;
Expand All @@ -515,6 +516,7 @@ export function stringifyWithMaxLen(
currentLength += (22 + value.position + value.position * 0.33 + 18) | 0;
break;
case 'Timestamp':
// '{"$timestamp":{"t":<t>,"i":<i>}}'
currentLength += 19 + String(v.t).length + 5 + String(v.i).length + 2;
break;
case 'Code':
Expand Down
22 changes: 10 additions & 12 deletions test/unit/mongo_logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1623,16 +1623,14 @@ describe('class MongoLogger', function () {
});

describe('stringifyWithMaxLen', function () {
let returnVal: string;

describe('when stringifying a string field', function () {
it('does not prematurely redact the next key', function () {
const doc = {
a: 'aaa',
b: 'bbb'
};

returnVal = stringifyWithMaxLen(doc, 13);
const returnVal = stringifyWithMaxLen(doc, 13);
expect(returnVal).to.contain('"b...');
});
});
Expand All @@ -1643,7 +1641,7 @@ describe('stringifyWithMaxLen', function () {
a: 1000,
b: 'bbb'
};
returnVal = stringifyWithMaxLen(doc, 12);
const returnVal = stringifyWithMaxLen(doc, 12);

expect(returnVal).to.contain('"b...');
});
Expand All @@ -1655,7 +1653,7 @@ describe('stringifyWithMaxLen', function () {
a: 1000n,
b: 'bbb'
};
returnVal = stringifyWithMaxLen(doc, 12);
const returnVal = stringifyWithMaxLen(doc, 12);

expect(returnVal).to.contain('"b...');
});
Expand All @@ -1667,7 +1665,7 @@ describe('stringifyWithMaxLen', function () {
c: new Code('console.log();'),
b: 'bbb'
};
returnVal = stringifyWithMaxLen(doc, 34);
const returnVal = stringifyWithMaxLen(doc, 34);

expect(returnVal).to.contain('"b...');
});
Expand All @@ -1679,7 +1677,7 @@ describe('stringifyWithMaxLen', function () {
c: new Double(123.1),
b: 'bbb'
};
returnVal = stringifyWithMaxLen(doc, 13);
const returnVal = stringifyWithMaxLen(doc, 13);

expect(returnVal).to.contain('"b...');
});
Expand All @@ -1691,7 +1689,7 @@ describe('stringifyWithMaxLen', function () {
c: new Int32(123),
b: 'bbb'
};
returnVal = stringifyWithMaxLen(doc, 11);
const returnVal = stringifyWithMaxLen(doc, 11);

expect(returnVal).to.contain('"b...');
});
Expand All @@ -1703,7 +1701,7 @@ describe('stringifyWithMaxLen', function () {
c: new MaxKey(),
b: 'bbb'
};
returnVal = stringifyWithMaxLen(doc, 21);
const returnVal = stringifyWithMaxLen(doc, 21);

expect(returnVal).to.contain('"b...');
});
Expand All @@ -1715,7 +1713,7 @@ describe('stringifyWithMaxLen', function () {
c: new MinKey(),
b: 'bbb'
};
returnVal = stringifyWithMaxLen(doc, 21);
const returnVal = stringifyWithMaxLen(doc, 21);

expect(returnVal).to.contain('"b...');
});
Expand All @@ -1727,7 +1725,7 @@ describe('stringifyWithMaxLen', function () {
c: new ObjectId(),
b: 'bbb'
};
returnVal = stringifyWithMaxLen(doc, 43);
const returnVal = stringifyWithMaxLen(doc, 43);

expect(returnVal).to.contain('"b...');
});
Expand All @@ -1739,7 +1737,7 @@ describe('stringifyWithMaxLen', function () {
c: new BSONRegExp('testRegex', 'is'),
b: 'bbb'
};
returnVal = stringifyWithMaxLen(doc, 69);
const returnVal = stringifyWithMaxLen(doc, 69);

expect(returnVal).to.contain('"b...');
});
Expand Down
Loading