fix: Avoid slices.Contains within tight loops

Having a `slices.Contains` check within a `for` loop has a complexity of
`O(n*m)`, with a worst case scenario of `O(n^2)` for when both
collections have the same size.

This is because `slices.Contains` internally runs just a regular loop,
checking for equality element by element.

Instead, this diff changes every occurence of this pattern to converting
the collection we compare against to a set, and then running a faster
lookup against it.
This commit is contained in:
Michael Manganiello
2024-03-17 13:21:51 -03:00
parent 3d61659d07
commit 80d7cf0b65
3 changed files with 10 additions and 8 deletions
+2 -1
View File
@@ -257,9 +257,10 @@ func (a *NowPlayingPage) Tapped(*fyne.PointEvent) {
}
func (a *NowPlayingPage) doSetNewTrackOrder(trackIDs []string, op sharedutil.TrackReorderOp) {
trackIDSet := sharedutil.ToSet(trackIDs)
idxs := make([]int, 0, len(trackIDs))
for i, tr := range a.queue {
if slices.Contains(trackIDs, tr.ID) {
if _, ok := trackIDSet[tr.ID]; ok {
idxs = append(idxs, i)
}
}