From 719930bb390ed0020b99a7942289d83218e99d69 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Tue, 24 Aug 2021 15:24:52 +0200 Subject: [PATCH] Android: Add fast path to addOrGet This path isn't really any faster in the normal case, but it does let us skip waiting for the lock to be available, which makes a huge difference if the lock is already taken. --- .../services/GameFileCacheService.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheService.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheService.java index a8ecc367c9..3be5fb039f 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheService.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheService.java @@ -159,9 +159,20 @@ public final class GameFileCacheService extends IntentService public static GameFile addOrGet(String gamePath) { - // The existence of this one function, which is called from one - // single place, forces us to use synchronization in onHandleIntent... - // A bit annoying, but should be good enough for now + // Common case: The game is in the cache, so just grab it from there. + // (Actually, addOrGet already checks for this case, but we want to avoid calling it if possible + // because onHandleIntent may hold a lock on gameFileCache for extended periods of time.) + GameFile[] allGames = gameFiles.get(); + for (GameFile game : allGames) + { + if (game.getPath().equals(gamePath)) + { + return game; + } + } + + // Unusual case: The game wasn't found in the cache. + // Scan the game and add it to the cache so that we can return it. synchronized (gameFileCache) { return gameFileCache.addOrGet(gamePath);