mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
Performance improvements
Use arrays instead of lists; use Array.CopyTo to concat playlist items; only count number of duplicates once
This commit is contained in:
parent
7f96fce15d
commit
4d32b59a0b
@ -196,7 +196,7 @@ namespace Emby.Server.Implementations.Playlists
|
|||||||
// Retrieve all the items to be added to the playlist
|
// Retrieve all the items to be added to the playlist
|
||||||
var items = GetPlaylistItems(itemIds, playlist.MediaType, user, options)
|
var items = GetPlaylistItems(itemIds, playlist.MediaType, user, options)
|
||||||
.Where(i => i.SupportsAddingToPlaylist)
|
.Where(i => i.SupportsAddingToPlaylist)
|
||||||
.ToList();
|
.ToArray();
|
||||||
|
|
||||||
// Remove duplicates from the new items to be added
|
// Remove duplicates from the new items to be added
|
||||||
var existingIds = playlist.LinkedChildren.Select(c => c.ItemId).ToHashSet();
|
var existingIds = playlist.LinkedChildren.Select(c => c.ItemId).ToHashSet();
|
||||||
@ -204,18 +204,23 @@ namespace Emby.Server.Implementations.Playlists
|
|||||||
.Where(i => !existingIds.Contains(i.Id))
|
.Where(i => !existingIds.Contains(i.Id))
|
||||||
.GroupBy(i => i.Id)
|
.GroupBy(i => i.Id)
|
||||||
.Select(group => group.First())
|
.Select(group => group.First())
|
||||||
.ToList();
|
.Select(i => LinkedChild.Create(i))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
// Log duplicates that have been ignored, if any
|
// Log duplicates that have been ignored, if any
|
||||||
if (uniqueItems.Count != items.Count)
|
int numDuplicates = items.Length - uniqueItems.Length;
|
||||||
|
if (numDuplicates > 0)
|
||||||
{
|
{
|
||||||
var numDuplicates = items.Count - uniqueItems.Count;
|
|
||||||
_logger.LogWarning("Ignored adding {DuplicateCount} duplicate items to playlist {PlaylistName}.", numDuplicates, playlist.Name);
|
_logger.LogWarning("Ignored adding {DuplicateCount} duplicate items to playlist {PlaylistName}.", numDuplicates, playlist.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a new array with the updated playlist items
|
||||||
|
var newLinkedChildren = new LinkedChild[playlist.LinkedChildren.Length + uniqueItems.Length];
|
||||||
|
playlist.LinkedChildren.CopyTo(newLinkedChildren, 0);
|
||||||
|
uniqueItems.CopyTo(newLinkedChildren, playlist.LinkedChildren.Length);
|
||||||
|
|
||||||
// Update the playlist in the repository
|
// Update the playlist in the repository
|
||||||
var linkedItems = uniqueItems.Select(i => LinkedChild.Create(i));
|
playlist.LinkedChildren = newLinkedChildren;
|
||||||
playlist.LinkedChildren = playlist.LinkedChildren.Concat(linkedItems).ToArray();
|
|
||||||
playlist.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
|
playlist.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
|
||||||
|
|
||||||
// Update the playlist on disk
|
// Update the playlist on disk
|
||||||
|
Loading…
x
Reference in New Issue
Block a user