Remove some allocations (#7246)

This commit is contained in:
Claus Vium 2022-01-28 12:21:40 +01:00 committed by GitHub
parent 42724ef411
commit 488ce51032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 39 deletions

View File

@ -2686,7 +2686,7 @@ namespace Emby.Server.Implementations.Library
}; };
} }
public IEnumerable<BaseItem> FindExtras(BaseItem owner, List<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService) public IEnumerable<BaseItem> FindExtras(BaseItem owner, IReadOnlyList<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService)
{ {
var ownerVideoInfo = VideoResolver.Resolve(owner.Path, owner.IsFolder, _namingOptions); var ownerVideoInfo = VideoResolver.Resolve(owner.Path, owner.IsFolder, _namingOptions);
if (ownerVideoInfo == null) if (ownerVideoInfo == null)

View File

@ -5,6 +5,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
@ -1286,7 +1287,7 @@ namespace MediaBrowser.Controller.Entities
{ {
if (IsFileProtocol) if (IsFileProtocol)
{ {
requiresSave = await RefreshedOwnedItems(options, GetFileSystemChildren(options.DirectoryService).ToList(), cancellationToken).ConfigureAwait(false); requiresSave = await RefreshedOwnedItems(options, GetFileSystemChildren(options.DirectoryService), cancellationToken).ConfigureAwait(false);
} }
await LibraryManager.UpdateImagesAsync(this).ConfigureAwait(false); // ensure all image properties in DB are fresh await LibraryManager.UpdateImagesAsync(this).ConfigureAwait(false); // ensure all image properties in DB are fresh
@ -1363,7 +1364,7 @@ namespace MediaBrowser.Controller.Entities
/// <param name="fileSystemChildren">The list of filesystem children.</param> /// <param name="fileSystemChildren">The list of filesystem children.</param>
/// <param name="cancellationToken">The cancellation token.</param> /// <param name="cancellationToken">The cancellation token.</param>
/// <returns><c>true</c> if any items have changed, else <c>false</c>.</returns> /// <returns><c>true</c> if any items have changed, else <c>false</c>.</returns>
protected virtual async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken) protected virtual async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, IReadOnlyList<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
{ {
if (!IsFileProtocol || !SupportsOwnedItems || IsInMixedFolder || this is ICollectionFolder or UserRootFolder or AggregateFolder || this.GetType() == typeof(Folder)) if (!IsFileProtocol || !SupportsOwnedItems || IsInMixedFolder || this is ICollectionFolder or UserRootFolder or AggregateFolder || this.GetType() == typeof(Folder))
{ {
@ -1380,7 +1381,7 @@ namespace MediaBrowser.Controller.Entities
return directoryService.GetFileSystemEntries(path); return directoryService.GetFileSystemEntries(path);
} }
private async Task<bool> RefreshExtras(BaseItem item, MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken) private async Task<bool> RefreshExtras(BaseItem item, MetadataRefreshOptions options, IReadOnlyList<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
{ {
var extras = LibraryManager.FindExtras(item, fileSystemChildren, options.DirectoryService).ToArray(); var extras = LibraryManager.FindExtras(item, fileSystemChildren, options.DirectoryService).ToArray();
var newExtraIds = extras.Select(i => i.Id).ToArray(); var newExtraIds = extras.Select(i => i.Id).ToArray();
@ -2041,27 +2042,32 @@ namespace MediaBrowser.Controller.Entities
/// <summary> /// <summary>
/// Validates that images within the item are still on the filesystem. /// Validates that images within the item are still on the filesystem.
/// </summary> /// </summary>
/// <param name="directoryService">The directory service to use.</param>
/// <returns><c>true</c> if the images validate, <c>false</c> if not.</returns> /// <returns><c>true</c> if the images validate, <c>false</c> if not.</returns>
public bool ValidateImages(IDirectoryService directoryService) public bool ValidateImages()
{ {
var allFiles = ImageInfos List<ItemImageInfo> deletedImages = null;
.Where(i => i.IsLocalFile) foreach (var imageInfo in ImageInfos)
.Select(i => System.IO.Path.GetDirectoryName(i.Path)) {
.Distinct(StringComparer.OrdinalIgnoreCase) if (!imageInfo.IsLocalFile)
.SelectMany(path => directoryService.GetFilePaths(path)) {
.ToList(); continue;
}
var deletedImages = ImageInfos if (File.Exists(imageInfo.Path))
.Where(image => image.IsLocalFile && !allFiles.Contains(image.Path, StringComparison.OrdinalIgnoreCase)) {
.ToList(); continue;
}
if (deletedImages.Count > 0) (deletedImages ??= new List<ItemImageInfo>()).Add(imageInfo);
}
var anyImagesRemoved = deletedImages?.Count > 0;
if (anyImagesRemoved)
{ {
RemoveImages(deletedImages); RemoveImages(deletedImages);
} }
return deletedImages.Count > 0; return anyImagesRemoved;
} }
/// <summary> /// <summary>

View File

@ -1585,7 +1585,7 @@ namespace MediaBrowser.Controller.Entities
.Where(i => i.Item2 != null); .Where(i => i.Item2 != null);
} }
protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken) protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, IReadOnlyList<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
{ {
var changesFound = false; var changesFound = false;

View File

@ -419,7 +419,7 @@ namespace MediaBrowser.Controller.Entities
return updateType; return updateType;
} }
protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken) protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, IReadOnlyList<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
{ {
var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false); var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);

View File

@ -434,7 +434,7 @@ namespace MediaBrowser.Controller.Library
/// <param name="fileSystemChildren">The file system children.</param> /// <param name="fileSystemChildren">The file system children.</param>
/// <param name="directoryService">An instance of <see cref="IDirectoryService"/>.</param> /// <param name="directoryService">An instance of <see cref="IDirectoryService"/>.</param>
/// <returns>IEnumerable&lt;BaseItem&gt;.</returns> /// <returns>IEnumerable&lt;BaseItem&gt;.</returns>
IEnumerable<BaseItem> FindExtras(BaseItem owner, List<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService); IEnumerable<BaseItem> FindExtras(BaseItem owner, IReadOnlyList<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService);
/// <summary> /// <summary>
/// Gets the collection folders. /// Gets the collection folders.

View File

@ -291,7 +291,7 @@ namespace MediaBrowser.LocalMetadata.Images
foreach (var name in imageFileNames) foreach (var name in imageFileNames)
{ {
if (AddImage(files, images, imagePrefix + name, ImageType.Primary)) if (AddImage(files, images, name, ImageType.Primary, imagePrefix))
{ {
return; return;
} }
@ -317,7 +317,7 @@ namespace MediaBrowser.LocalMetadata.Images
if (!string.IsNullOrEmpty(name)) if (!string.IsNullOrEmpty(name))
{ {
AddImage(files, images, imagePrefix + name + "-fanart", ImageType.Backdrop); AddImage(files, images, name + "-fanart", ImageType.Backdrop, imagePrefix);
// Support without the prefix if it's in it's own folder // Support without the prefix if it's in it's own folder
if (!isInMixedFolder) if (!isInMixedFolder)
@ -436,7 +436,7 @@ namespace MediaBrowser.LocalMetadata.Images
private bool AddImage(List<FileSystemMetadata> files, List<LocalImageInfo> images, string name, string imagePrefix, bool isInMixedFolder, ImageType type) private bool AddImage(List<FileSystemMetadata> files, List<LocalImageInfo> images, string name, string imagePrefix, bool isInMixedFolder, ImageType type)
{ {
var added = AddImage(files, images, imagePrefix + name, type); var added = AddImage(files, images, name, type, imagePrefix);
if (!isInMixedFolder) if (!isInMixedFolder)
{ {
@ -449,12 +449,15 @@ namespace MediaBrowser.LocalMetadata.Images
return added; return added;
} }
private bool AddImage(List<FileSystemMetadata> files, List<LocalImageInfo> images, string name, ImageType type) private static bool AddImage(IReadOnlyList<FileSystemMetadata> files, List<LocalImageInfo> images, string name, ImageType type, string? prefix = null)
{ {
var image = GetImage(files, name); var image = GetImage(files, name, prefix);
if (image != null) if (image == null)
{ {
return false;
}
images.Add(new LocalImageInfo images.Add(new LocalImageInfo
{ {
FileInfo = image, FileInfo = image,
@ -464,17 +467,21 @@ namespace MediaBrowser.LocalMetadata.Images
return true; return true;
} }
return false; private static FileSystemMetadata? GetImage(IReadOnlyList<FileSystemMetadata> files, string name, string? prefix = null)
}
private static FileSystemMetadata? GetImage(IReadOnlyList<FileSystemMetadata> files, string name)
{ {
var fileNameLength = name.Length + (prefix?.Length ?? 0);
for (var i = 0; i < files.Count; i++) for (var i = 0; i < files.Count; i++)
{ {
var file = files[i]; var file = files[i];
if (!file.IsDirectory if (file.IsDirectory || file.Length <= 0)
&& file.Length > 0 {
&& Path.GetFileNameWithoutExtension(file.FullName.AsSpan()).Equals(name, StringComparison.OrdinalIgnoreCase)) continue;
}
var fileName = Path.GetFileNameWithoutExtension(file.FullName.AsSpan());
if (fileName.Length == fileNameLength
&& fileName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)
&& fileName.EndsWith(name, StringComparison.OrdinalIgnoreCase))
{ {
return file; return file;
} }

View File

@ -388,7 +388,7 @@ namespace MediaBrowser.Providers.Manager
/// <returns><c>true</c> if changes were made to the item; otherwise <c>false</c>.</returns> /// <returns><c>true</c> if changes were made to the item; otherwise <c>false</c>.</returns>
public bool MergeImages(BaseItem item, IReadOnlyList<LocalImageInfo> images) public bool MergeImages(BaseItem item, IReadOnlyList<LocalImageInfo> images)
{ {
var changed = item.ValidateImages(new DirectoryService(_fileSystem)); var changed = item.ValidateImages();
for (var i = 0; i < _singularImages.Length; i++) for (var i = 0; i < _singularImages.Length; i++)
{ {