1
0

append() was replaced with more efficient copy() (only 1 allocation with make).

This commit is contained in:
Alexander Andreev 2023-09-14 18:20:47 +04:00
parent 978b4c6454
commit 2cf6e1a6cb
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -80,8 +80,8 @@ func IcecastLastSongs(playlistPath string) []Song {
lastPlayedCacheMutex.Lock()
defer lastPlayedCacheMutex.Unlock()
if lpcLen := len(lastPlayedCache); lpcLen > 0 {
ret := make([]Song, 0, lpcLen)
ret = append(ret, lastPlayedCache...)
ret := make([]Song, lpcLen)
copy(ret, lastPlayedCache)
return ret
}
@ -181,10 +181,10 @@ func (pw *PlaylistLogWatcher) Watch(playlistPath string, n int) (err error) {
pw.watcher.WatchForMask(pw.changed, watcher.ModIgnMask)
if lastPlayedCache == nil {
lastPlayedCache = make([]Song, 0, n)
lastPlayedCache = make([]Song, n)
songs, err := icecastLastPlayedSongs(playlistPath, n)
if err == nil && len(songs) > 0 {
lastPlayedCache = append(lastPlayedCache, songs...)
copy(lastPlayedCache, songs)
} else if err != nil {
log.Fatalln("failed to retrieve last songs:", err)
}