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.
This commit is contained in:
JosJuice 2021-08-24 15:24:52 +02:00
parent fb265b610d
commit 719930bb39
1 changed files with 14 additions and 3 deletions

View File

@ -159,9 +159,20 @@ public final class GameFileCacheService extends IntentService
public static GameFile addOrGet(String gamePath) public static GameFile addOrGet(String gamePath)
{ {
// The existence of this one function, which is called from one // Common case: The game is in the cache, so just grab it from there.
// single place, forces us to use synchronization in onHandleIntent... // (Actually, addOrGet already checks for this case, but we want to avoid calling it if possible
// A bit annoying, but should be good enough for now // 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) synchronized (gameFileCache)
{ {
return gameFileCache.addOrGet(gamePath); return gameFileCache.addOrGet(gamePath);