Skip to content

perf: simplify the search algorithm - #178

Merged
gurgunday merged 2 commits into
masterfrom
simplify
Dec 2, 2024
Merged

gurgunday merged 2 commits into
masterfrom
simplify

Conversation

@gurgunday

@gurgunday gurgunday commented Nov 28, 2024 •

Copy link
Copy Markdown
Member

Removes unnecessary checks

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

zekth commented Nov 29, 2024

Copy link
Copy Markdown
Member

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

@gurgunday

gurgunday commented Nov 29, 2024 •

Copy link
Copy Markdown
Member Author

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

I won't change behavior

@zekth

zekth commented Nov 30, 2024

Copy link
Copy Markdown
Member

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.

Comment thread deps/streamsearch/sbmh.js

// 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))

@gurgunday gurgunday Nov 30, 2024 •

Copy link
Copy Markdown
Member Author

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)

Comment thread deps/streamsearch/sbmh.js
return (this._bufpos = pos + needleLength)
}

pos = len - needleLength

@gurgunday gurgunday Nov 30, 2024 •

Copy link
Copy Markdown
Member Author

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

Comment thread deps/streamsearch/sbmh.js
(
data[pos] !== needle[0] ||
Buffer.compare(
data.subarray(pos, pos + len - pos),

Copy link
Copy Markdown
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

Comment thread deps/streamsearch/sbmh.js
data[pos] !== needle[0] ||
Buffer.compare(
data.subarray(pos, pos + len - pos),
needle.subarray(0, len - pos)

@gurgunday gurgunday Nov 30, 2024 •

Copy link
Copy Markdown
Member Author

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

Comment thread deps/streamsearch/sbmh.js
}

// 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
Copy Markdown
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

Copilot AI left a comment

Copy link
Copy Markdown

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.

@zekth zekth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One suggestion

Comment thread deps/streamsearch/sbmh.js Outdated
}

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

Copy link
Copy Markdown
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
@gurgunday
gurgunday deleted the simplify branch December 2, 2024 14:19
@github-actions

github-actions Bot commented May 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants