Skip to content
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

events: simplify stack compare function #24744

Closed
Closed
Changes from all commits
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
41 changes: 20 additions & 21 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,30 +103,29 @@ EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
return $getMaxListeners(this);
};

// Returns the longest sequence of `a` that fully appears in `b`,
// of length at least 3.
// This is a lazy approach but should work well enough, given that stack
// frames are usually unequal or otherwise appear in groups, and that
// we only run this code in case of an unhandled exception.
function longestSeqContainedIn(a, b) {
for (var len = a.length; len >= 3; --len) {
for (var i = 0; i < a.length - len; ++i) {
// Attempt to find a[i:i+len] in b
for (var j = 0; j < b.length - len; ++j) {
let matches = true;
for (var k = 0; k < len; ++k) {
if (a[i + k] !== b[j + k]) {
matches = false;
break;
}
// Returns the length and line number of the first sequence of `a` that fully
// appears in `b` with a length of at least 4.
function identicalSequenceRange(a, b) {
for (var i = 0; i < a.length - 3; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would cache a.length - 3 in a variable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect the value to be constant fold (but I did not check).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a could be quite big, and it used to be slightly faster to cache the value if it's computed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a should normally be small (we currently only use this for stack frames) and this implementation should also be faster than the one before. If it's about performance, I could save a couple comparisons by using a simple for loop instead of indexOf (currently I check until the last entry but the last three entries are not interesting).

@bmeurer do values like these get constant fold?

// Find the first entry of b that matches the current entry of a.
const pos = b.indexOf(a[i]);
addaleax marked this conversation as resolved.
Show resolved Hide resolved
if (pos !== -1) {
const rest = b.length - pos;
if (rest > 3) {
let len = 1;
const maxLen = Math.min(a.length - i, rest);
// Count the number of consecutive entries.
while (maxLen > len && a[i + len] === b[pos + len]) {
len++;
}
if (len > 3) {
return [len, i];
}
if (matches)
return [ len, i, j ];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And for the same reason, returning j might not be used in this specific setup, but it’s part of having this be a more generic function.

Copy link
Member Author

@BridgeAR BridgeAR Nov 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pos translates to the former j but I recommend to change the signature when necessary and not to keep code in here that is currently unused.

}
}
}

return [ 0, 0, 0 ];
return [0, 0];
}

function enhanceStackTrace(err, own) {
Expand All @@ -135,9 +134,9 @@ function enhanceStackTrace(err, own) {
const errStack = err.stack.split('\n').slice(1);
const ownStack = own.stack.split('\n').slice(1);

const [ len, off ] = longestSeqContainedIn(ownStack, errStack);
const [ len, off ] = identicalSequenceRange(ownStack, errStack);
if (len > 0) {
ownStack.splice(off + 1, len - 1,
ownStack.splice(off + 1, len - 2,
' [... lines matching original stack trace ...]');
}
// Do this last, because it is the only operation with side effects.
Expand Down