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

perf: simplify the search algorithm #178

Merged
merged 2 commits into from
Dec 2, 2024
Merged

perf: simplify the search algorithm #178

merged 2 commits into from
Dec 2, 2024

Conversation

gurgunday
Copy link
Member

@gurgunday gurgunday commented Nov 28, 2024

Removes unnecessary checks

@gurgunday gurgunday requested review from climba03003 and a team November 28, 2024 13:58
@gurgunday gurgunday marked this pull request as draft November 29, 2024 13:00
@zekth
Copy link
Member

zekth commented Nov 29, 2024

Shouldn't we take the opportunity to add tests if we start to modify the implementation?

@gurgunday
Copy link
Member Author

gurgunday commented Nov 29, 2024

Coverage of this file is already at 100% but I could add more once it's finished

I won't change behavior

@zekth
Copy link
Member

zekth commented Nov 30, 2024

Coverage of this file is already at 100% but I could add more once it's finished

I won't change behavior

My bad i was assuming tests were siblings of the vendored lib.

@@ -168,15 +169,15 @@ SBMH.prototype._sbmh_feed = function (data) {

// Lookbehind buffer is now empty. We only need to check if the
// needle is in the haystack.
pos = data.indexOf(needle, pos + ((pos >= 0) * this._bufpos))
Copy link
Member Author

@gurgunday gurgunday Nov 30, 2024

Choose a reason for hiding this comment

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

(pos >= 0) is pointless as pos is never negative here (lookbehind is emptied)

return (this._bufpos = pos + needleLength)
}

pos = len - needleLength
Copy link
Member Author

@gurgunday gurgunday Nov 30, 2024

Choose a reason for hiding this comment

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

When needle is larger than the incoming data buffer, pos becomes negative here and there are many redundant/duplicate comparisons

At this part pos should only look at the incoming data (lookbehind is emptied), so it cannot be a negative integer

(
data[pos] !== needle[0] ||
Buffer.compare(
data.subarray(pos, pos + len - pos),
Copy link
Member Author

Choose a reason for hiding this comment

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

pos + len - pos === len

(
data[pos] !== needle[0] ||
Buffer.compare(
data.subarray(pos, pos + len - pos),
needle.subarray(0, len - pos)
Copy link
Member Author

@gurgunday gurgunday Nov 30, 2024

Choose a reason for hiding this comment

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

We can start with 1 as the first character is already checked on the check just above

this._lookbehind_size = len - pos
}

// Everything until pos is guaranteed not to contain needle data.
if (pos > 0) { this.emit('info', false, data, this._bufpos, pos < len ? pos : len) }
Copy link
Member Author

Choose a reason for hiding this comment

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

pos < len ? pos : len is pointless as pos can never surpass len here

@gurgunday gurgunday marked this pull request as ready for review November 30, 2024 14:38
@Fdawgs Fdawgs requested a review from Copilot December 1, 2024 13:27

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no suggestions.

Copy link
Member

@zekth zekth left a comment

Choose a reason for hiding this comment

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

One suggestion

return (this._bufpos = pos + needleLength)
}

pos = len - needleLength
pos = Math.max(len - needleLastCharIndex, 0)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
pos = Math.max(len - needleLastCharIndex, 0)
pos = len - needleLastCharIndex
pos = pos < 0 ? 0

Math.max is unfortunately slower than if statement

@gurgunday gurgunday merged commit 74c3e23 into master Dec 2, 2024
13 checks passed
@gurgunday gurgunday deleted the simplify branch December 2, 2024 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants