Skip to content

Commit

Permalink
Merge pull request #348 from adamantike/fix/avoid-contains-within-tig…
Browse files Browse the repository at this point in the history
…ht-loops

fix: Avoid slices.Contains within tight loops
  • Loading branch information
dweymouth authored Mar 19, 2024
2 parents 3d61659 + 80d7cf0 commit 668f2f4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
6 changes: 4 additions & 2 deletions sharedutil/sharedutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ func ReorderTracks(tracks []*mediaprovider.Track, idxToMove []int, op TrackReord
case MoveToTop:
topIdx := 0
botIdx := len(idxToMove)
idxToMoveSet := ToSet(idxToMove)
for i, t := range tracks {
if slices.Contains(idxToMove, i) {
if _, ok := idxToMoveSet[i]; ok {
newTracks[topIdx] = t
topIdx++
} else {
Expand All @@ -110,8 +111,9 @@ func ReorderTracks(tracks []*mediaprovider.Track, idxToMove []int, op TrackReord
case MoveToBottom:
topIdx := 0
botIdx := len(tracks) - len(idxToMove)
idxToMoveSet := ToSet(idxToMove)
for i, t := range tracks {
if slices.Contains(idxToMove, i) {
if _, ok := idxToMoveSet[i]; ok {
newTracks[botIdx] = t
botIdx++
} else {
Expand Down
3 changes: 2 additions & 1 deletion ui/browsing/nowplayingpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
9 changes: 4 additions & 5 deletions ui/browsing/playlistpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package browsing
import (
"fmt"
"log"
"slices"

"github.com/dweymouth/supersonic/backend"
"github.com/dweymouth/supersonic/backend/mediaprovider"
Expand Down Expand Up @@ -181,15 +180,15 @@ func (a *PlaylistPage) doSetNewTrackOrder(op sharedutil.TrackReorderOp) {
// Since the tracklist view may be sorted in a different order than the
// actual running order, we need to get the IDs of the selected tracks
// from the tracklist and convert them to indices in the *original* run order
ids := a.tracklist.SelectedTrackIDs()
idxs := make([]int, 0, len(ids))
idSet := sharedutil.ToSet(a.tracklist.SelectedTrackIDs())
idxs := make([]int, 0, len(idSet))
for i, tr := range a.tracks {
if slices.Contains(ids, tr.ID) {
if _, ok := idSet[tr.ID]; ok {
idxs = append(idxs, i)
}
}
newTracks := sharedutil.ReorderTracks(a.tracks, idxs, op)
ids = sharedutil.TracksToIDs(newTracks)
ids := sharedutil.TracksToIDs(newTracks)
if err := a.sm.Server.ReplacePlaylistTracks(a.playlistID, ids); err != nil {
log.Printf("error updating playlist: %s", err.Error())
} else {
Expand Down

0 comments on commit 668f2f4

Please sign in to comment.