mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-05-03 23:36:38 +01:00
Optimize latest items grouping in UserViewManager
- Use dictionary lookup for O(1) container grouping instead of O(n) FirstOrDefault searches - Add optimized path for movies in GetLatestItemList - Reduce query limit multiplier from 5x to 2x - Update to collection expression syntax
This commit is contained in:
@@ -138,7 +138,7 @@ namespace Emby.Server.Implementations.Library
|
||||
list = list.Where(i => !user.GetPreferenceValues<Guid>(PreferenceKind.MyMediaExcludes).Contains(i.Id)).ToList();
|
||||
}
|
||||
|
||||
var sorted = _libraryManager.Sort(list, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
|
||||
var sorted = _libraryManager.Sort(list, user, [ItemSortBy.SortName], SortOrder.Ascending).ToList();
|
||||
var orders = user.GetPreferenceValues<Guid>(PreferenceKind.OrderedViews);
|
||||
|
||||
return list
|
||||
@@ -205,7 +205,7 @@ namespace Emby.Server.Implementations.Library
|
||||
var libraryItems = GetItemsForLatestItems(request.User, request, options);
|
||||
|
||||
var list = new List<Tuple<BaseItem, List<BaseItem>>>();
|
||||
|
||||
var containerIndexMap = new Dictionary<Guid, int>();
|
||||
foreach (var item in libraryItems)
|
||||
{
|
||||
// Only grab the index container for media
|
||||
@@ -213,20 +213,16 @@ namespace Emby.Server.Implementations.Library
|
||||
|
||||
if (container is null)
|
||||
{
|
||||
list.Add(new Tuple<BaseItem, List<BaseItem>>(null, new List<BaseItem> { item }));
|
||||
list.Add(new Tuple<BaseItem, List<BaseItem>>(null!, new List<BaseItem> { item }));
|
||||
}
|
||||
else if (containerIndexMap.TryGetValue(container.Id, out var existingIndex))
|
||||
{
|
||||
list[existingIndex].Item2.Add(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
var current = list.FirstOrDefault(i => i.Item1 is not null && i.Item1.Id.Equals(container.Id));
|
||||
|
||||
if (current is not null)
|
||||
{
|
||||
current.Item2.Add(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Add(new Tuple<BaseItem, List<BaseItem>>(container, new List<BaseItem> { item }));
|
||||
}
|
||||
containerIndexMap[container.Id] = list.Count;
|
||||
list.Add(new Tuple<BaseItem, List<BaseItem>>(container, new List<BaseItem> { item }));
|
||||
}
|
||||
|
||||
if (list.Count >= request.Limit)
|
||||
@@ -255,7 +251,7 @@ namespace Emby.Server.Implementations.Library
|
||||
return _channelManager.GetLatestChannelItemsInternal(
|
||||
new InternalItemsQuery(user)
|
||||
{
|
||||
ChannelIds = new[] { parentId },
|
||||
ChannelIds = [parentId],
|
||||
IsPlayed = request.IsPlayed,
|
||||
StartIndex = request.StartIndex,
|
||||
Limit = request.Limit,
|
||||
@@ -301,11 +297,11 @@ namespace Emby.Server.Implementations.Library
|
||||
{
|
||||
if (hasCollectionType.All(i => i.CollectionType == CollectionType.movies))
|
||||
{
|
||||
includeItemTypes = new[] { BaseItemKind.Movie };
|
||||
includeItemTypes = [BaseItemKind.Movie];
|
||||
}
|
||||
else if (hasCollectionType.All(i => i.CollectionType == CollectionType.tvshows))
|
||||
{
|
||||
includeItemTypes = new[] { BaseItemKind.Episode };
|
||||
includeItemTypes = [BaseItemKind.Episode];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -344,29 +340,29 @@ namespace Emby.Server.Implementations.Library
|
||||
}
|
||||
|
||||
var excludeItemTypes = includeItemTypes.Length == 0 && mediaTypes.Length == 0
|
||||
? new[]
|
||||
{
|
||||
?
|
||||
[
|
||||
BaseItemKind.Person,
|
||||
BaseItemKind.Studio,
|
||||
BaseItemKind.Year,
|
||||
BaseItemKind.MusicGenre,
|
||||
BaseItemKind.Genre
|
||||
}
|
||||
]
|
||||
: Array.Empty<BaseItemKind>();
|
||||
|
||||
var query = new InternalItemsQuery(user)
|
||||
{
|
||||
IncludeItemTypes = includeItemTypes,
|
||||
OrderBy = new[]
|
||||
{
|
||||
OrderBy =
|
||||
[
|
||||
(ItemSortBy.DateCreated, SortOrder.Descending),
|
||||
(ItemSortBy.SortName, SortOrder.Descending),
|
||||
(ItemSortBy.ProductionYear, SortOrder.Descending)
|
||||
},
|
||||
],
|
||||
IsFolder = includeItemTypes.Length == 0 ? false : null,
|
||||
ExcludeItemTypes = excludeItemTypes,
|
||||
IsVirtualItem = false,
|
||||
Limit = limit * 5,
|
||||
Limit = limit * 2,
|
||||
IsPlayed = isPlayed,
|
||||
DtoOptions = options,
|
||||
MediaTypes = mediaTypes
|
||||
@@ -394,6 +390,12 @@ namespace Emby.Server.Implementations.Library
|
||||
query.Limit = limit;
|
||||
return _libraryManager.GetLatestItemList(query, parents, CollectionType.music);
|
||||
}
|
||||
|
||||
if (collectionType == CollectionType.movies)
|
||||
{
|
||||
query.Limit = limit;
|
||||
return _libraryManager.GetLatestItemList(query, parents, CollectionType.movies);
|
||||
}
|
||||
}
|
||||
|
||||
return _libraryManager.GetItemList(query, parents);
|
||||
|
||||
Reference in New Issue
Block a user