Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Quality: Improved loading speed of thumbnails #14573

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 52 additions & 16 deletions src/Files.App/Data/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Files.App.Data.Models
public sealed class ItemViewModel : ObservableObject, IDisposable
{
private readonly SemaphoreSlim enumFolderSemaphore;
private readonly SemaphoreSlim loadPropertiesSemaphore;
private readonly SemaphoreSlim getFileOrFolderSemaphore;
private readonly ConcurrentQueue<(uint Action, string FileName)> operationQueue;
private readonly ConcurrentQueue<uint> gitChangesQueue;
private readonly ConcurrentDictionary<string, bool> itemLoadQueue;
Expand Down Expand Up @@ -169,17 +169,57 @@ public async Task SetWorkingDirectoryAsync(string? value)
OnPropertyChanged(nameof(WorkingDirectory));
}

public Task<FilesystemResult<BaseStorageFolder>> GetFolderFromPathAsync(string value)
=> FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(value, workingRoot, currentStorageFolder));
public async Task<FilesystemResult<BaseStorageFolder>> GetFolderFromPathAsync(string value, CancellationToken cancellationToken = default)
{
await getFileOrFolderSemaphore.WaitAsync(cancellationToken);
try
{
return await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(value, workingRoot, currentStorageFolder));
}
finally
{
getFileOrFolderSemaphore.Release();
}
}

public Task<FilesystemResult<BaseStorageFile>> GetFileFromPathAsync(string value)
=> FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(value, workingRoot, currentStorageFolder));
public async Task<FilesystemResult<BaseStorageFile>> GetFileFromPathAsync(string value, CancellationToken cancellationToken = default)
{
await getFileOrFolderSemaphore.WaitAsync(cancellationToken);
try
{
return await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(value, workingRoot, currentStorageFolder));
}
finally
{
getFileOrFolderSemaphore.Release();
}
}

public Task<FilesystemResult<StorageFolderWithPath>> GetFolderWithPathFromPathAsync(string value)
=> FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderWithPathFromPathAsync(value, workingRoot, currentStorageFolder));
public async Task<FilesystemResult<StorageFolderWithPath>> GetFolderWithPathFromPathAsync(string value, CancellationToken cancellationToken = default)
{
await getFileOrFolderSemaphore.WaitAsync(cancellationToken);
try
{
return await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderWithPathFromPathAsync(value, workingRoot, currentStorageFolder));
}
finally
{
getFileOrFolderSemaphore.Release();
}
}

public Task<FilesystemResult<StorageFileWithPath>> GetFileWithPathFromPathAsync(string value)
=> FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileWithPathFromPathAsync(value, workingRoot, currentStorageFolder));
public async Task<FilesystemResult<StorageFileWithPath>> GetFileWithPathFromPathAsync(string value, CancellationToken cancellationToken = default)
{
await getFileOrFolderSemaphore.WaitAsync(cancellationToken);
try
{
return await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileWithPathFromPathAsync(value, workingRoot, currentStorageFolder));
}
finally
{
getFileOrFolderSemaphore.Release();
}
}

private EmptyTextType emptyTextType;
public EmptyTextType EmptyTextType
Expand Down Expand Up @@ -443,7 +483,7 @@ public ItemViewModel(LayoutPreferencesManager folderSettingsViewModel)
operationEvent = new AsyncManualResetEvent();
gitChangedEvent = new AsyncManualResetEvent();
enumFolderSemaphore = new SemaphoreSlim(1, 1);
loadPropertiesSemaphore = new SemaphoreSlim(100);
getFileOrFolderSemaphore = new SemaphoreSlim(50);
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
dispatcherQueue = DispatcherQueue.GetForCurrentThread();

UserSettingsService.OnSettingChangedEvent += UserSettingsService_OnSettingChangedEvent;
Expand Down Expand Up @@ -988,8 +1028,6 @@ await Task.Run(async () =>
ImageSource? groupImage = null;
GroupedCollection<ListedItem>? gp = null;

// We use the semaphore to limit the number of concurrent processes because loading properties is a heavy process
await loadPropertiesSemaphore.WaitAsync(cts.Token);
try
{
var isFileTypeGroupMode = folderSettings.DirectoryGroupOption == GroupOption.FileType;
Expand All @@ -1005,7 +1043,7 @@ await Task.Run(async () =>
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath))
{
cts.Token.ThrowIfCancellationRequested();
matchingStorageFile = await GetFileFromPathAsync(item.ItemPath);
matchingStorageFile = await GetFileFromPathAsync(item.ItemPath, cts.Token);
if (matchingStorageFile is not null)
{
cts.Token.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -1048,7 +1086,7 @@ await dispatcherQueue.EnqueueOrInvokeAsync(() =>
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath))
{
cts.Token.ThrowIfCancellationRequested();
BaseStorageFolder matchingStorageFolder = await GetFolderFromPathAsync(item.ItemPath);
BaseStorageFolder matchingStorageFolder = await GetFolderFromPathAsync(item.ItemPath, cts.Token);
if (matchingStorageFolder is not null)
{
cts.Token.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -1107,8 +1145,6 @@ await dispatcherQueue.EnqueueOrInvokeAsync(() =>
}
finally
{
loadPropertiesSemaphore.Release();

if (!wasSyncStatusLoaded)
{
cts.Token.ThrowIfCancellationRequested();
Expand Down
Loading