diff --git a/Core/FileLoaders/DiskCachingFileLoader.cpp b/Core/FileLoaders/DiskCachingFileLoader.cpp index 961b611ad8..f5c07aa8e5 100644 --- a/Core/FileLoaders/DiskCachingFileLoader.cpp +++ b/Core/FileLoaders/DiskCachingFileLoader.cpp @@ -27,6 +27,8 @@ static const char *CACHEFILE_MAGIC = "ppssppDC"; static const s64 SAFETY_FREE_DISK_SPACE = 768 * 1024 * 1024; // 768 MB +// Aim to allow this many files cached at once. +static const u32 CACHE_SPACE_FLEX = 4; std::string DiskCachingFileLoaderCache::cacheDir_; @@ -578,13 +580,35 @@ u32 DiskCachingFileLoaderCache::DetermineMaxBlocks() { const u64 availBytes = std::max(0LL, freeBytes - SAFETY_FREE_DISK_SPACE); const u64 freeBlocks = availBytes / (u64)DEFAULT_BLOCK_SIZE; - if (freeBlocks > MAX_BLOCKS_UPPER_BOUND) { - return MAX_BLOCKS_UPPER_BOUND; + const u32 alreadyCachedCount = CountCachedFiles(); + // This is how many more files of free space we will aim for. + const u32 flex = CACHE_SPACE_FLEX > alreadyCachedCount ? CACHE_SPACE_FLEX - alreadyCachedCount : 1; + + const u64 freeBlocksWithFlex = freeBlocks / flex; + if (freeBlocksWithFlex > MAX_BLOCKS_LOWER_BOUND) { + if (freeBlocksWithFlex > MAX_BLOCKS_UPPER_BOUND) { + return MAX_BLOCKS_UPPER_BOUND; + } + // This might be smaller than what's free, but if they try to launch a second game, + // they'll be happier when it can be cached too. + return freeBlocksWithFlex; } - // Might be lower than LOWER_BOUND, but that's okay. It means not enough space. + + // Might be lower than LOWER_BOUND, but that's okay. That means not enough space. + // We abandon the idea of flex since there's not enough space free anyway. return freeBlocks; } +u32 DiskCachingFileLoaderCache::CountCachedFiles() { + std::string dir = cacheDir_; + if (dir.empty()) { + dir = GetSysDirectory(DIRECTORY_CACHE); + } + + std::vector files; + return (u32)getFilesInDir(dir.c_str(), &files, "ppdc:"); +} + void DiskCachingFileLoaderCache::GarbageCollectCacheFiles(u64 goalBytes) { // We attempt to free up at least enough files from the cache to get goalBytes more space. const std::vector usedPaths = DiskCachingFileLoader::GetCachedPathsInUse(); diff --git a/Core/FileLoaders/DiskCachingFileLoader.h b/Core/FileLoaders/DiskCachingFileLoader.h index 45065fa5bd..5f647a7925 100644 --- a/Core/FileLoaders/DiskCachingFileLoader.h +++ b/Core/FileLoaders/DiskCachingFileLoader.h @@ -109,6 +109,7 @@ private: u64 FreeDiskSpace(); u32 DetermineMaxBlocks(); + u32 CountCachedFiles(); void GarbageCollectCacheFiles(u64 goalBytes); // File format: