diff options
| author | cvium <clausvium@gmail.com> | 2021-05-24 00:30:41 +0200 |
|---|---|---|
| committer | cvium <clausvium@gmail.com> | 2021-05-24 00:30:41 +0200 |
| commit | 42a2cc1747c7859c63334a7a45792e0af1410e1a (patch) | |
| tree | 16e9587342a83948ac6742f05518d9a31836c235 /MediaBrowser.Controller/Providers/DirectoryService.cs | |
| parent | db9d3b8653d865459e5df5a2fba18f0c9462dbb6 (diff) | |
Remove some unnecessary allocations
Diffstat (limited to 'MediaBrowser.Controller/Providers/DirectoryService.cs')
| -rw-r--r-- | MediaBrowser.Controller/Providers/DirectoryService.cs | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/MediaBrowser.Controller/Providers/DirectoryService.cs b/MediaBrowser.Controller/Providers/DirectoryService.cs index 291a26883..ba5290763 100644 --- a/MediaBrowser.Controller/Providers/DirectoryService.cs +++ b/MediaBrowser.Controller/Providers/DirectoryService.cs @@ -25,15 +25,16 @@ namespace MediaBrowser.Controller.Providers public FileSystemMetadata[] GetFileSystemEntries(string path) { - return _cache.GetOrAdd(path, p => _fileSystem.GetFileSystemEntries(p).ToArray()); + return _cache.GetOrAdd(path, (p, fileSystem) => fileSystem.GetFileSystemEntries(p).ToArray(), _fileSystem); } public List<FileSystemMetadata> GetFiles(string path) { var list = new List<FileSystemMetadata>(); var items = GetFileSystemEntries(path); - foreach (var item in items) + for (var i = 0; i < items.Length; i++) { + var item = items[i]; if (!item.IsDirectory) { list.Add(item); @@ -48,10 +49,9 @@ namespace MediaBrowser.Controller.Providers if (!_fileCache.TryGetValue(path, out var result)) { var file = _fileSystem.GetFileInfo(path); - var res = file != null && file.Exists ? file : null; - if (res != null) + if (file.Exists) { - result = res; + result = file; _fileCache.TryAdd(path, result); } } @@ -60,16 +60,26 @@ namespace MediaBrowser.Controller.Providers } public IReadOnlyList<string> GetFilePaths(string path) - => GetFilePaths(path, false); + => GetFilePaths(path, false, false); - public IReadOnlyList<string> GetFilePaths(string path, bool clearCache) + public IReadOnlyList<string> GetSortedFilePaths(string path, bool clearCache) + => GetFilePaths(path, clearCache, true); + + public IReadOnlyList<string> GetFilePaths(string path, bool clearCache, bool sort = false) { if (clearCache) { _filePathCache.TryRemove(path, out _); } - return _filePathCache.GetOrAdd(path, p => _fileSystem.GetFilePaths(p).ToList()); + var filePaths = _filePathCache.GetOrAdd(path, (p, fileSystem) => fileSystem.GetFilePaths(p).ToList(), _fileSystem); + + if (sort) + { + filePaths.Sort(); + } + + return filePaths; } } } |
