From deb4320ff1d9d753e2bc47d78c939bf5f9617491 Mon Sep 17 00:00:00 2001 From: Fares Alhassen Date: Thu, 28 May 2026 08:20:51 -0700 Subject: [PATCH] This section here just because the diffbase contains changes in Glide, which require a section by this name in order for presubmits to pass. PiperOrigin-RevId: 922784488 --- .../engine/cache/DiskLruCacheWrapper.java | 29 ++++++- .../glide/disklrucache/DiskLruCache.java | 77 ++++++++++++++----- 2 files changed, 84 insertions(+), 22 deletions(-) diff --git a/library/src/main/java/com/bumptech/glide/load/engine/cache/DiskLruCacheWrapper.java b/library/src/main/java/com/bumptech/glide/load/engine/cache/DiskLruCacheWrapper.java index fbabb0cb2a..01164d7722 100644 --- a/library/src/main/java/com/bumptech/glide/load/engine/cache/DiskLruCacheWrapper.java +++ b/library/src/main/java/com/bumptech/glide/load/engine/cache/DiskLruCacheWrapper.java @@ -27,6 +27,7 @@ public class DiskLruCacheWrapper implements DiskCache { private final SafeKeyGenerator safeKeyGenerator; private final File directory; private final long maxSize; + private final boolean memoizePathNames; private final DiskCacheWriteLocker writeLocker = new DiskCacheWriteLocker(); private DiskLruCache diskLruCache; @@ -60,7 +61,24 @@ public static synchronized DiskCache get(File directory, long maxSize) { */ @SuppressWarnings("deprecation") public static DiskCache create(File directory, long maxSize) { - return new DiskLruCacheWrapper(directory, maxSize); + return new DiskLruCacheWrapper(directory, maxSize, /* memoizePathNames= */ true); + } + + /** + * Create a new DiskCache in the given directory with a specified max size and memoization + * behavior. + * + * @param directory The directory for the disk cache + * @param maxSize The max size for the disk cache + * @param memoizePathNames Whether to memoize path names + * @return The new disk cache with the given arguments + * @deprecated Disabling the memoization is an experimental setting that may be removed in a + * future version. + */ + @SuppressWarnings("deprecation") + @Deprecated + public static DiskCache create(File directory, long maxSize, boolean memoizePathNames) { + return new DiskLruCacheWrapper(directory, maxSize, memoizePathNames); } /** @@ -70,14 +88,21 @@ public static DiskCache create(File directory, long maxSize) { // Deprecated public API. @SuppressWarnings({"WeakerAccess", "DeprecatedIsStillUsed"}) protected DiskLruCacheWrapper(File directory, long maxSize) { + this(directory, maxSize, /* memoizePathNames= */ true); + } + + protected DiskLruCacheWrapper(File directory, long maxSize, boolean memoizePathNames) { this.directory = directory; this.maxSize = maxSize; + this.memoizePathNames = memoizePathNames; this.safeKeyGenerator = new SafeKeyGenerator(); } private synchronized DiskLruCache getDiskCache() throws IOException { if (diskLruCache == null) { - diskLruCache = DiskLruCache.open(directory, APP_VERSION, VALUE_COUNT, maxSize); + diskLruCache = + DiskLruCache.experimentalOpen( + directory, APP_VERSION, VALUE_COUNT, maxSize, memoizePathNames); } return diskLruCache; } 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 2db4ebcfb3..225903e742 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 @@ -146,6 +146,7 @@ public final class DiskLruCache implements Closeable { private final int appVersion; private long maxSize; private final int valueCount; + private final boolean memoizePathNames; private long size = 0; private Writer journalWriter; private final LinkedHashMap lruEntries = @@ -179,7 +180,8 @@ public Void call() throws Exception { } }; - private DiskLruCache(File directory, int appVersion, int valueCount, long maxSize) { + private DiskLruCache( + File directory, int appVersion, int valueCount, long maxSize, boolean memoizePathNames) { this.directory = directory; this.appVersion = appVersion; this.journalFile = new File(directory, JOURNAL_FILE); @@ -187,19 +189,41 @@ private DiskLruCache(File directory, int appVersion, int valueCount, long maxSiz this.journalFileBackup = new File(directory, JOURNAL_FILE_BACKUP); this.valueCount = valueCount; this.maxSize = maxSize; + this.memoizePathNames = memoizePathNames; } /** - * Opens the cache in {@code directory}, creating a cache if none exists - * there. + * Opens the cache in {@code directory}, creating a cache if none exists there. * * @param directory a writable directory + * @param appVersion the application's current version code * @param valueCount the number of values per cache entry. Must be positive. * @param maxSize the maximum number of bytes this cache should use to store * @throws IOException if reading or writing the cache directory fails */ public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize) throws IOException { + return experimentalOpen( + directory, appVersion, valueCount, maxSize, /* memoizePathNames= */ true); + } + + /** + * Opens the cache in {@code directory}, creating a cache if none exists there, with explicit + * control over path name memoization. + * + *

Disabling the memoization is an experimental setting that may be removed in a future + * version. + * + * @param directory a writable directory + * @param appVersion the application's current version code + * @param valueCount the number of values per cache entry. Must be positive. + * @param maxSize the maximum number of bytes this cache should use to store + * @param memoizePathNames whether to memoize path names + * @return The new disk cache with the given arguments + */ + public static DiskLruCache experimentalOpen( + File directory, int appVersion, int valueCount, long maxSize, boolean memoizePathNames) + throws IOException { if (maxSize <= 0) { throw new IllegalArgumentException("maxSize <= 0"); } @@ -220,7 +244,8 @@ public static DiskLruCache open(File directory, int appVersion, int valueCount, } // Prefer to pick up where we left off. - DiskLruCache cache = new DiskLruCache(directory, appVersion, valueCount, maxSize); + DiskLruCache cache = + new DiskLruCache(directory, appVersion, valueCount, maxSize, memoizePathNames); if (cache.journalFile.exists()) { try { cache.readJournal(); @@ -239,7 +264,7 @@ public static DiskLruCache open(File directory, int appVersion, int valueCount, // Create a new empty cache. directory.mkdirs(); - cache = new DiskLruCache(directory, appVersion, valueCount, maxSize); + cache = new DiskLruCache(directory, appVersion, valueCount, maxSize, memoizePathNames); cache.rebuildJournal(); return cache; } @@ -420,7 +445,8 @@ public synchronized Value get(String key) throws IOException { return null; } - for (File file : entry.cleanFiles) { + for (int i = 0; i < valueCount; i++) { + File file = entry.getCleanFile(i); // A file must have been deleted manually! if (!file.exists()) { return null; @@ -740,12 +766,15 @@ public Editor edit() throws IOException { } public File getFile(int index) { + if (files != null) { return files[index]; + } + return new File(directory, key + "." + index); } /** Returns the string value for {@code index}. */ public String getString(int index) throws IOException { - InputStream is = new FileInputStream(files[index]); + InputStream is = new FileInputStream(getFile(index)); return inputStreamToString(is); } @@ -874,18 +903,20 @@ private final class Entry { private Entry(String key) { this.key = key; this.lengths = new long[valueCount]; - cleanFiles = new File[valueCount]; - dirtyFiles = new File[valueCount]; - // The names are repetitive so re-use the same builder to avoid allocations. - StringBuilder fileBuilder = new StringBuilder(key).append('.'); - int truncateTo = fileBuilder.length(); - for (int i = 0; i < valueCount; i++) { - fileBuilder.append(i); - cleanFiles[i] = new File(directory, fileBuilder.toString()); - fileBuilder.append(".tmp"); - dirtyFiles[i] = new File(directory, fileBuilder.toString()); - fileBuilder.setLength(truncateTo); + if (memoizePathNames) { + cleanFiles = new File[valueCount]; + dirtyFiles = new File[valueCount]; + // The names are repetitive so re-use the same builder to avoid allocations. + StringBuilder fileBuilder = new StringBuilder(key).append('.'); + int truncateTo = fileBuilder.length(); + for (int i = 0; i < valueCount; i++) { + fileBuilder.append(i); + cleanFiles[i] = new File(directory, fileBuilder.toString()); + fileBuilder.append(".tmp"); + dirtyFiles[i] = new File(directory, fileBuilder.toString()); + fileBuilder.setLength(truncateTo); + } } } @@ -917,11 +948,17 @@ private IOException invalidLengths(String[] strings) throws IOException { } public File getCleanFile(int i) { - return cleanFiles[i]; + if (cleanFiles != null) { + return cleanFiles[i]; + } + return new File(directory, key + "." + i); } public File getDirtyFile(int i) { - return dirtyFiles[i]; + if (dirtyFiles != null) { + return dirtyFiles[i]; + } + return new File(directory, key + "." + i + ".tmp"); } }