-
Notifications
You must be signed in to change notification settings - Fork 60
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
Use slices.BinarySearch in util.searchToken #408
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just one question about the test.
@@ -424,3 +425,32 @@ func TestGetTokenDistance(t *testing.T) { | |||
require.Equal(t, testData.expected, distance) | |||
} | |||
} | |||
|
|||
func TestSearchToken(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you verified that this behavior matches the old implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3fe4308
to
9cec3d0
Compare
This reverts commit 9cec3d0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I checked the actual sort.Search()
vs slices.SearchBinary()
implementation and I've also written this simple comparison test:
func TestSearchToken_EnsureBinarySearchLogicIsEqualToSortSearch(t *testing.T) {
tokens := []uint32{1, 5, 6, 8, 10}
searchOld := func(target uint32) int {
return sort.Search(len(tokens), func(x int) bool {
return tokens[x] > target
})
}
searchNew := func(target uint32) int {
i, found := slices.BinarySearch(tokens, target)
if found {
// we want the first token > key, not >= key
i = i + 1
}
return i
}
for i := uint32(0); i < tokens[len(tokens)-1]+2; i++ {
assert.Equal(t, searchOld(i), searchNew(i))
}
}
* Add test and benchmark for searchToken * Use slices.BinarySearch in util.searchToken * Validate that functionality is identical * Revert "Validate that functionality is identical" This reverts commit 9cec3d0.
What this PR does:
The
slices
package introduces a genericslices.BinarySearch
which looks to be faster thansort.Search
, especially as the number of items to search increases.Benchmark:
Which issue(s) this PR fixes:
(none)
Checklist
CHANGELOG.md
updated - the order of entries should be[CHANGE]
,[FEATURE]
,[ENHANCEMENT]
,[BUGFIX]