From adcba7b724de38b18ef13705711e7f2b65ee3ea6 Mon Sep 17 00:00:00 2001 From: Stepan Goremykin Date: Sun, 26 Mar 2023 20:40:51 -0700 Subject: [PATCH] Use Array.Empty and fix a few multiple enumerations (cherry picked from commit 11d91faaada0e70910c832ce405ddeed52a24172) Closes #3451 --- src/Lidarr.Api.V1/ProviderControllerBase.cs | 6 +++--- src/Lidarr.Http/REST/RestController.cs | 4 +++- src/NzbDrone.Common/Composition/AssemblyLoader.cs | 9 +++++---- src/NzbDrone.Common/OAuth/WebParameterCollection.cs | 4 ++-- src/NzbDrone.Core/Datastore/BasicRepository.cs | 2 +- .../Proxies/DownloadStationTaskProxyV1.cs | 3 ++- .../Proxies/DownloadStationTaskProxyV2.cs | 3 ++- src/NzbDrone.Core/Extras/Metadata/MetadataService.cs | 2 +- src/NzbDrone.Core/Indexers/HttpIndexerBase.cs | 6 +++--- src/NzbDrone.Core/Indexers/RssParser.cs | 2 +- .../MediaFiles/TrackImport/ImportApprovedTracks.cs | 11 ++++++----- 11 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/Lidarr.Api.V1/ProviderControllerBase.cs b/src/Lidarr.Api.V1/ProviderControllerBase.cs index 389a591de..54e5098d7 100644 --- a/src/Lidarr.Api.V1/ProviderControllerBase.cs +++ b/src/Lidarr.Api.V1/ProviderControllerBase.cs @@ -43,11 +43,11 @@ namespace Lidarr.Api.V1 [HttpGet] public List GetAll() { - var providerDefinitions = _providerFactory.All().OrderBy(p => p.ImplementationName); + var providerDefinitions = _providerFactory.All(); - var result = new List(providerDefinitions.Count()); + var result = new List(providerDefinitions.Count); - foreach (var definition in providerDefinitions) + foreach (var definition in providerDefinitions.OrderBy(p => p.ImplementationName)) { _providerFactory.SetProviderCharacteristics(definition); diff --git a/src/Lidarr.Http/REST/RestController.cs b/src/Lidarr.Http/REST/RestController.cs index 4a9f5d607..888e7571b 100644 --- a/src/Lidarr.Http/REST/RestController.cs +++ b/src/Lidarr.Http/REST/RestController.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using FluentValidation; using FluentValidation.Results; using Lidarr.Http.REST.Attributes; @@ -67,7 +68,8 @@ namespace Lidarr.Http.REST } } - var attributes = descriptor.MethodInfo.CustomAttributes; + var attributes = descriptor.MethodInfo.CustomAttributes as IReadOnlyCollection ?? + descriptor.MethodInfo.CustomAttributes.ToArray(); if (attributes.Any(x => VALIDATE_ID_ATTRIBUTES.Contains(x.AttributeType)) && !skipValidate) { if (context.ActionArguments.TryGetValue("id", out var idObj)) diff --git a/src/NzbDrone.Common/Composition/AssemblyLoader.cs b/src/NzbDrone.Common/Composition/AssemblyLoader.cs index c581d8cc3..e0afe64e6 100644 --- a/src/NzbDrone.Common/Composition/AssemblyLoader.cs +++ b/src/NzbDrone.Common/Composition/AssemblyLoader.cs @@ -17,16 +17,17 @@ namespace NzbDrone.Common.Composition RegisterSQLiteResolver(); } - public static IEnumerable Load(IEnumerable assemblies) + public static IList Load(IList assemblyNames) { - var toLoad = assemblies.ToList(); + var toLoad = assemblyNames.ToList(); toLoad.Add("Lidarr.Common"); toLoad.Add(OsInfo.IsWindows ? "Lidarr.Windows" : "Lidarr.Mono"); var startupPath = AppDomain.CurrentDomain.BaseDirectory; - return toLoad.Select(x => - AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(startupPath, $"{x}.dll"))); + return toLoad + .Select(x => AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(startupPath, $"{x}.dll"))) + .ToList(); } private static Assembly ContainerResolveEventHandler(object sender, ResolveEventArgs args) diff --git a/src/NzbDrone.Common/OAuth/WebParameterCollection.cs b/src/NzbDrone.Common/OAuth/WebParameterCollection.cs index cea68b114..9dbd90509 100644 --- a/src/NzbDrone.Common/OAuth/WebParameterCollection.cs +++ b/src/NzbDrone.Common/OAuth/WebParameterCollection.cs @@ -14,14 +14,14 @@ namespace NzbDrone.Common.OAuth { get { - var parameters = this.Where(p => p.Name.Equals(name)); + var parameters = this.Where(p => p.Name.Equals(name)).ToArray(); if (!parameters.Any()) { return null; } - if (parameters.Count() == 1) + if (parameters.Length == 1) { return parameters.Single(); } diff --git a/src/NzbDrone.Core/Datastore/BasicRepository.cs b/src/NzbDrone.Core/Datastore/BasicRepository.cs index 8f997ad8f..0ac96becf 100644 --- a/src/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/src/NzbDrone.Core/Datastore/BasicRepository.cs @@ -111,7 +111,7 @@ namespace NzbDrone.Core.Datastore { if (!ids.Any()) { - return new List(); + return Array.Empty(); } var result = Query(x => ids.Contains(x.Id)); diff --git a/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV1.cs b/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV1.cs index 466a8c49c..13e5131fb 100644 --- a/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV1.cs +++ b/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV1.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Net.Http; using NLog; @@ -61,7 +62,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies catch (DownloadClientException e) { _logger.Error(e); - return new List(); + return Array.Empty(); } } diff --git a/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV2.cs b/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV2.cs index fa76c1d0d..dfcdd68fb 100644 --- a/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV2.cs +++ b/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV2.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; @@ -135,7 +136,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies catch (DownloadClientException e) { _logger.Error(e); - return new List(); + return Array.Empty(); } } diff --git a/src/NzbDrone.Core/Extras/Metadata/MetadataService.cs b/src/NzbDrone.Core/Extras/Metadata/MetadataService.cs index 87a70dc8f..8c8279c0e 100644 --- a/src/NzbDrone.Core/Extras/Metadata/MetadataService.cs +++ b/src/NzbDrone.Core/Extras/Metadata/MetadataService.cs @@ -145,7 +145,7 @@ namespace NzbDrone.Core.Extras.Metadata if (artistFolder.IsNullOrWhiteSpace() && albumFolder.IsNullOrWhiteSpace()) { - return new List(); + return Array.Empty(); } var files = new List(); diff --git a/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs b/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs index 46a7ff250..1b22b78eb 100644 --- a/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs +++ b/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs @@ -42,7 +42,7 @@ namespace NzbDrone.Core.Indexers { if (!SupportsRss) { - return new List(); + return Array.Empty(); } return FetchReleases(g => g.GetRecentRequests(), true); @@ -52,7 +52,7 @@ namespace NzbDrone.Core.Indexers { if (!SupportsSearch) { - return new List(); + return Array.Empty(); } return FetchReleases(g => g.GetSearchRequests(searchCriteria)); @@ -62,7 +62,7 @@ namespace NzbDrone.Core.Indexers { if (!SupportsSearch) { - return new List(); + return Array.Empty(); } return FetchReleases(g => g.GetSearchRequests(searchCriteria)); diff --git a/src/NzbDrone.Core/Indexers/RssParser.cs b/src/NzbDrone.Core/Indexers/RssParser.cs index 9491365e3..0f7b7ba2d 100644 --- a/src/NzbDrone.Core/Indexers/RssParser.cs +++ b/src/NzbDrone.Core/Indexers/RssParser.cs @@ -85,7 +85,7 @@ namespace NzbDrone.Core.Indexers if (!PostProcess(indexerResponse, items, releases)) { - return new List(); + return Array.Empty(); } return releases; diff --git a/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs b/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs index 7988edda8..b2081223a 100644 --- a/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs +++ b/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs @@ -128,11 +128,12 @@ namespace NzbDrone.Core.MediaFiles.TrackImport _eventAggregator.PublishEvent(new AlbumEditedEvent(album, album)); } - var qualifiedImports = decisions.Where(c => c.Approved) - .GroupBy(c => c.Item.Artist.Id, (i, s) => s - .OrderByDescending(c => c.Item.Quality, new QualityModelComparer(s.First().Item.Artist.QualityProfile)) - .ThenByDescending(c => c.Item.Size)) - .SelectMany(c => c) + var qualifiedImports = decisions + .Where(decision => decision.Approved) + .GroupBy(decision => decision.Item.Artist.Id) + .SelectMany(group => group + .OrderByDescending(decision => decision.Item.Quality, new QualityModelComparer(group.First().Item.Artist.QualityProfile)) + .ThenByDescending(decision => decision.Item.Size)) .ToList(); _logger.ProgressInfo($"Importing {qualifiedImports.Count} tracks");