Skip to content

Commit ce9740b

Browse files
committed
Code Quality: Optimize large folder enumeration performance
1 parent 53466f5 commit ce9740b

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/Files.App/Utils/Storage/Enumerators/UniversalStorageEnumerator.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,28 @@ public static async Task<List<ListedItem>> ListEntries(
1919
Func<List<ListedItem>, Task> intermediateAction,
2020
Dictionary<string, BitmapImage> defaultIconPairs = null)
2121
{
22-
var sampler = new IntervalSampler(500);
22+
var sampler = new IntervalSampler(5000);
2323
var tempList = new List<ListedItem>();
2424
uint count = 0;
2525
var firstRound = true;
26+
var disableIntermediateUpdates = false;
2627

2728
IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
2829

2930
while (true)
3031
{
3132
IReadOnlyList<IStorageItem> items;
3233

33-
uint maxItemsToRetrieve = 300;
34+
uint maxItemsToRetrieve = 5000;
3435

3536
if (intermediateAction is null)
3637
{
3738
// without intermediate action increase batches significantly
38-
maxItemsToRetrieve = 1000;
39+
maxItemsToRetrieve = 10000;
3940
}
4041
else if (firstRound)
4142
{
42-
maxItemsToRetrieve = 32;
43+
maxItemsToRetrieve = 1000;
4344
firstRound = false;
4445
}
4546

@@ -116,7 +117,10 @@ ex is FileNotFoundException ||
116117
if (countLimit > -1 && count >= countLimit)
117118
break;
118119

119-
if (intermediateAction is not null && (items.Count == maxItemsToRetrieve || sampler.CheckNow()))
120+
if (count > 50000 && !disableIntermediateUpdates)
121+
disableIntermediateUpdates = true;
122+
123+
if (intermediateAction is not null && !disableIntermediateUpdates && (items.Count == maxItemsToRetrieve || sampler.CheckNow()))
120124
{
121125
await intermediateAction(tempList);
122126

src/Files.App/Utils/Storage/Enumerators/Win32StorageEnumerator.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public static async Task<List<ListedItem>> ListEntries(
2525
Func<List<ListedItem>, Task> intermediateAction
2626
)
2727
{
28-
var sampler = new IntervalSampler(500);
28+
var sampler = new IntervalSampler(5000);
2929
var tempList = new List<ListedItem>();
3030
var count = 0;
31+
var disableIntermediateUpdates = false;
3132

3233
IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
3334
bool CalculateFolderSizes = userSettingsService.FoldersSettingsService.CalculateFolderSizes;
@@ -89,7 +90,10 @@ Func<List<ListedItem>, Task> intermediateAction
8990
if (cancellationToken.IsCancellationRequested || count == countLimit)
9091
break;
9192

92-
if (intermediateAction is not null && (count == 32 || sampler.CheckNow()))
93+
if (count > 50000 && !disableIntermediateUpdates)
94+
disableIntermediateUpdates = true;
95+
96+
if (intermediateAction is not null && !disableIntermediateUpdates && (count % 10000 == 0 || sampler.CheckNow()))
9397
{
9498
await intermediateAction(tempList);
9599

0 commit comments

Comments
 (0)