From 5acc99f8fc7e772aeef8aa959ff2a9c203ab1aa6 Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Wed, 13 Jan 2021 10:52:18 -0800 Subject: [PATCH] Minimize stat() syscall on Glide init. PiperOrigin-RevId: 351620050 --- library/src/main/java/com/bumptech/glide/Glide.java | 8 ++++---- .../glide/load/engine/cache/DiskLruCacheFactory.java | 6 +++--- .../com/bumptech/glide/disklrucache/DiskLruCache.java | 4 +--- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/library/src/main/java/com/bumptech/glide/Glide.java b/library/src/main/java/com/bumptech/glide/Glide.java index 9e904f3807..4951007c00 100644 --- a/library/src/main/java/com/bumptech/glide/Glide.java +++ b/library/src/main/java/com/bumptech/glide/Glide.java @@ -162,11 +162,11 @@ public static File getPhotoCacheDir(@NonNull Context context, @NonNull String ca File cacheDir = context.getCacheDir(); if (cacheDir != null) { File result = new File(cacheDir, cacheName); - if (!result.mkdirs() && (!result.exists() || !result.isDirectory())) { - // File wasn't able to create a directory, or the result exists but not a directory - return null; + if (result.isDirectory() || result.mkdirs()) { + return result; } - return result; + // File wasn't able to create a directory, or the result exists but not a directory + return null; } if (Log.isLoggable(TAG, Log.ERROR)) { Log.e(TAG, "default disk cache dir is null"); diff --git a/library/src/main/java/com/bumptech/glide/load/engine/cache/DiskLruCacheFactory.java b/library/src/main/java/com/bumptech/glide/load/engine/cache/DiskLruCacheFactory.java index 03a783481d..591db06e29 100644 --- a/library/src/main/java/com/bumptech/glide/load/engine/cache/DiskLruCacheFactory.java +++ b/library/src/main/java/com/bumptech/glide/load/engine/cache/DiskLruCacheFactory.java @@ -65,10 +65,10 @@ public DiskCache build() { return null; } - if (!cacheDir.mkdirs() && (!cacheDir.exists() || !cacheDir.isDirectory())) { - return null; + if (cacheDir.isDirectory() || cacheDir.mkdirs()) { + return DiskLruCacheWrapper.create(cacheDir, diskCacheSize); } - return DiskLruCacheWrapper.create(cacheDir, diskCacheSize); + return null; } } diff --git a/third_party/disklrucache/src/main/java/com/bumptech/glide/disklrucache/DiskLruCache.java b/third_party/disklrucache/src/main/java/com/bumptech/glide/disklrucache/DiskLruCache.java index 2ca2e8bff9..2db4ebcfb3 100644 --- a/third_party/disklrucache/src/main/java/com/bumptech/glide/disklrucache/DiskLruCache.java +++ b/third_party/disklrucache/src/main/java/com/bumptech/glide/disklrucache/DiskLruCache.java @@ -804,9 +804,7 @@ public File getFile(int index) throws IOException { written[index] = true; } File dirtyFile = entry.getDirtyFile(index); - if (!directory.exists()) { - directory.mkdirs(); - } + directory.mkdirs(); return dirtyFile; } }