From e125db4fe32712b1c291f50275dd3e7f04b355a3 Mon Sep 17 00:00:00 2001 From: Vasily Date: Thu, 27 Feb 2020 20:11:40 +0300 Subject: [PATCH 1/7] Bring back sorting when needed to fix PlayTo This is partial revert of https://github.com/jellyfin/jellyfin/pull/1011 --- MediaBrowser.Controller/Entities/Folder.cs | 24 ++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 07fbe6035..17786fc9a 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -809,9 +809,17 @@ namespace MediaBrowser.Controller.Entities public QueryResult GetItems(InternalItemsQuery query) { - if (query.ItemIds.Length > 0) + if (query.ItemIds.Length > 1) { - return LibraryManager.GetItemsResult(query); + var result = LibraryManager.GetItemsResult(query); + + if (query.OrderBy.Count == 0) + { + var ids = query.ItemIds.ToList(); + // Try to preserve order, "Play To" relies on it + result.Items = result.Items.OrderBy(i => ids.IndexOf(i.Id)).ToArray(); + } + return result; } return GetItemsInternal(query); @@ -821,9 +829,17 @@ namespace MediaBrowser.Controller.Entities { query.EnableTotalRecordCount = false; - if (query.ItemIds.Length > 0) + if (query.ItemIds.Length > 1) { - return LibraryManager.GetItemList(query); + var result = LibraryManager.GetItemList(query); + + if (query.OrderBy.Count == 0) + { + var ids = query.ItemIds.ToList(); + // Try to preserve order, "Play To" relies on it + return result.OrderBy(i => ids.IndexOf(i.Id)).ToArray(); + } + return result.ToArray(); } return GetItemsInternal(query).Items; From 0f0b89f3444faac1710a97866430b8f7fc38b0f2 Mon Sep 17 00:00:00 2001 From: Vasily Date: Thu, 27 Feb 2020 20:14:56 +0300 Subject: [PATCH 2/7] Trying to be more safe --- MediaBrowser.Controller/Entities/Folder.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 17786fc9a..9fd4ea740 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -809,11 +809,11 @@ namespace MediaBrowser.Controller.Entities public QueryResult GetItems(InternalItemsQuery query) { - if (query.ItemIds.Length > 1) + if (query.ItemIds.Length > 0) { var result = LibraryManager.GetItemsResult(query); - if (query.OrderBy.Count == 0) + if (query.OrderBy.Count == 0 && query.ItemIds.Length > 1) { var ids = query.ItemIds.ToList(); // Try to preserve order, "Play To" relies on it @@ -829,11 +829,11 @@ namespace MediaBrowser.Controller.Entities { query.EnableTotalRecordCount = false; - if (query.ItemIds.Length > 1) + if (query.ItemIds.Length > 0) { var result = LibraryManager.GetItemList(query); - if (query.OrderBy.Count == 0) + if (query.OrderBy.Count == 0 && query.ItemIds.Length > 1) { var ids = query.ItemIds.ToList(); // Try to preserve order, "Play To" relies on it From ae1f975b99c912d4bf059a9c3336edabea621067 Mon Sep 17 00:00:00 2001 From: Vasily Date: Thu, 27 Feb 2020 20:21:34 +0300 Subject: [PATCH 3/7] Implement fast sorting --- MediaBrowser.Controller/Entities/Folder.cs | 58 +++++++++++++++++++--- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 9fd4ea740..33e498c12 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -807,6 +807,56 @@ namespace MediaBrowser.Controller.Entities return false; } + private class OneTimeQueue + { + private List items; + private int start; + public OneTimeQueue(int capacity) + { + items = new List(capacity); + start = 0; + } + public OneTimeQueue(int capacity, T first) + { + items = new List(capacity); + items.Add(first); + start = 0; + } + public void Enqueue(T item) + { + items.Add(item); + } + public T Dequeue() + { + start++; + return items[start - 1]; + } + } + + private IReadOnlyList SortItemsByRequest(InternalItemsQuery query, IReadOnlyList items) + { + var ids = query.ItemIds.ToList(); + var positions = new Dictionary>(ids.Count); + for (int i = 0; i < ids.Count; i++) + { + if (positions.TryGetValue(ids[i], out var q)) + { + q.Enqueue(i); + } + else + { + positions.Add(ids[i], new OneTimeQueue(4 /* wild guess */, i)); + } + } + + var newItems = new BaseItem[ids.Count]; + foreach(var item in items) + { + newItems[positions[item.Id].Dequeue()] = item; + } + return newItems; + } + public QueryResult GetItems(InternalItemsQuery query) { if (query.ItemIds.Length > 0) @@ -815,9 +865,7 @@ namespace MediaBrowser.Controller.Entities if (query.OrderBy.Count == 0 && query.ItemIds.Length > 1) { - var ids = query.ItemIds.ToList(); - // Try to preserve order, "Play To" relies on it - result.Items = result.Items.OrderBy(i => ids.IndexOf(i.Id)).ToArray(); + result.Items = SortItemsByRequest(query, result.Items); } return result; } @@ -835,9 +883,7 @@ namespace MediaBrowser.Controller.Entities if (query.OrderBy.Count == 0 && query.ItemIds.Length > 1) { - var ids = query.ItemIds.ToList(); - // Try to preserve order, "Play To" relies on it - return result.OrderBy(i => ids.IndexOf(i.Id)).ToArray(); + return SortItemsByRequest(query, result); } return result.ToArray(); } From f81cd037f0a66e573262e3f666d6c8019b031966 Mon Sep 17 00:00:00 2001 From: Vasily Date: Fri, 28 Feb 2020 16:19:51 +0300 Subject: [PATCH 4/7] Small speed improvement - no need to convert an array to a list to just iterate over it --- MediaBrowser.Controller/Entities/Folder.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 33e498c12..b134dfde2 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -835,9 +835,9 @@ namespace MediaBrowser.Controller.Entities private IReadOnlyList SortItemsByRequest(InternalItemsQuery query, IReadOnlyList items) { - var ids = query.ItemIds.ToList(); - var positions = new Dictionary>(ids.Count); - for (int i = 0; i < ids.Count; i++) + var ids = query.ItemIds; + var positions = new Dictionary>(ids.Length); + for (int i = 0; i < ids.Length; i++) { if (positions.TryGetValue(ids[i], out var q)) { From 7f38af3701aa06e2281799ffeac59e875eaec5db Mon Sep 17 00:00:00 2001 From: Vasily Date: Fri, 28 Feb 2020 17:27:16 +0300 Subject: [PATCH 5/7] Remove custom queue class as it is not needed --- MediaBrowser.Controller/Entities/Folder.cs | 45 +++++----------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index b134dfde2..cda4c31ad 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -807,52 +807,27 @@ namespace MediaBrowser.Controller.Entities return false; } - private class OneTimeQueue - { - private List items; - private int start; - public OneTimeQueue(int capacity) - { - items = new List(capacity); - start = 0; - } - public OneTimeQueue(int capacity, T first) - { - items = new List(capacity); - items.Add(first); - start = 0; - } - public void Enqueue(T item) - { - items.Add(item); - } - public T Dequeue() - { - start++; - return items[start - 1]; - } - } - private IReadOnlyList SortItemsByRequest(InternalItemsQuery query, IReadOnlyList items) { var ids = query.ItemIds; - var positions = new Dictionary>(ids.Length); + int size = items.Count; + + // ids can potentially contain non-unique guids, but query result cannot, + // so we include only first occurrence of each guid + var positions = new Dictionary(size); + int index = 0; for (int i = 0; i < ids.Length; i++) { - if (positions.TryGetValue(ids[i], out var q)) + if (positions.TryAdd(ids[i], index)) { - q.Enqueue(i); - } - else - { - positions.Add(ids[i], new OneTimeQueue(4 /* wild guess */, i)); + index++; } } - var newItems = new BaseItem[ids.Count]; + var newItems = new BaseItem[size]; foreach(var item in items) { - newItems[positions[item.Id].Dequeue()] = item; + newItems[positions[item.Id]] = item; } return newItems; } From 9a9f2aa293b04ae315905ad7dff03184e281ae16 Mon Sep 17 00:00:00 2001 From: Vasily Date: Mon, 2 Mar 2020 12:36:44 +0300 Subject: [PATCH 6/7] Apply suggestions from code review Co-Authored-By: Bond-009 --- MediaBrowser.Controller/Entities/Folder.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index cda4c31ad..04e07b0f2 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -807,7 +807,7 @@ namespace MediaBrowser.Controller.Entities return false; } - private IReadOnlyList SortItemsByRequest(InternalItemsQuery query, IReadOnlyList items) + private static BaseItem[] SortItemsByRequest(InternalItemsQuery query, IReadOnlyList items) { var ids = query.ItemIds; int size = items.Count; @@ -825,10 +825,11 @@ namespace MediaBrowser.Controller.Entities } var newItems = new BaseItem[size]; - foreach(var item in items) + foreach (var item in items) { newItems[positions[item.Id]] = item; } + return newItems; } @@ -842,6 +843,7 @@ namespace MediaBrowser.Controller.Entities { result.Items = SortItemsByRequest(query, result.Items); } + return result; } @@ -860,6 +862,7 @@ namespace MediaBrowser.Controller.Entities { return SortItemsByRequest(query, result); } + return result.ToArray(); } From ba8d8cede9a06e58d3d082c129fb6e31a26317db Mon Sep 17 00:00:00 2001 From: Vasily Date: Mon, 2 Mar 2020 12:39:34 +0300 Subject: [PATCH 7/7] Replace foreach with for - MOAR SPEED --- MediaBrowser.Controller/Entities/Folder.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 04e07b0f2..64c2159e4 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -825,8 +825,9 @@ namespace MediaBrowser.Controller.Entities } var newItems = new BaseItem[size]; - foreach (var item in items) + for (int i = 0; i < size; i++) { + var item = items[i]; newItems[positions[item.Id]] = item; }