From 680f5181d745e7f658aef8cb55a88d778db2f159 Mon Sep 17 00:00:00 2001 From: Christian Emmer <10749361+emmercm@users.noreply.github.com> Date: Mon, 10 Jun 2024 18:22:39 -0700 Subject: [PATCH] Refactor: limit the number of cached files checked for existence --- src/types/files/fileCache.ts | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/types/files/fileCache.ts b/src/types/files/fileCache.ts index ad025c97a..04aaa202a 100644 --- a/src/types/files/fileCache.ts +++ b/src/types/files/fileCache.ts @@ -47,19 +47,25 @@ export default class FileCache { // Delete keys for deleted files const disks = FsPoly.disksSync(); const semaphore = new Semaphore(Constants.MAX_FS_THREADS); - await Promise.all([...this.cache.keys()].map(async (cacheKey) => { - const cacheKeyFilePath = cacheKey.split('|')[1]; - if (!disks.some((disk) => cacheKeyFilePath.startsWith(disk))) { - // Don't delete the key if it's for a disk that isn't mounted right now - return; - } - await semaphore.runExclusive(async () => { - if (!await FsPoly.exists(cacheKeyFilePath)) { - // If the file no longer exists, then delete its key from the cache - await this.cache.delete(cacheKeyFilePath); - } - }); - })); + await Promise.all( + [...this.cache.keys()] + // Only process a reasonably sized subset of the keys + .sort(() => Math.random() - 0.5) + .slice(0, Constants.MAX_FS_THREADS * 100) + .map(async (cacheKey) => { + const cacheKeyFilePath = cacheKey.split('|')[1]; + if (!disks.some((disk) => cacheKeyFilePath.startsWith(disk))) { + // Don't delete the key if it's for a disk that isn't mounted right now + return; + } + await semaphore.runExclusive(async () => { + if (!await FsPoly.exists(cacheKeyFilePath)) { + // If the file no longer exists, then delete its key from the cache + await this.cache.delete(cacheKeyFilePath); + } + }); + }), + ); } public static async save(): Promise {