Skip to content

Commit

Permalink
Use long value so that cache sizes greater than 2Gigabytes can be sup…
Browse files Browse the repository at this point in the history
…ported. (#2558)

* Use long value so that cache sizes greater than 2Gigabytes can be supported
resign

* refactor to long diskcache sizes
  • Loading branch information
bhargavms authored and sjudd committed Nov 7, 2017
1 parent 7fccb32 commit d402780
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface BitmapPool {
/**
* Returns the current maximum size of the pool in bytes.
*/
int getMaxSize();
long getMaxSize();

/**
* Multiplies the initial size of the pool by the given multiplier to dynamically and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public class BitmapPoolAdapter implements BitmapPool {
@Override
public int getMaxSize() {
public long getMaxSize() {
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ public class LruBitmapPool implements BitmapPool {

private final LruPoolStrategy strategy;
private final Set<Bitmap.Config> allowedConfigs;
private final int initialMaxSize;
private final long initialMaxSize;
private final BitmapTracker tracker;

private int maxSize;
private int currentSize;
private long maxSize;
private long currentSize;
private int hits;
private int misses;
private int puts;
private int evictions;

// Exposed for testing only.
LruBitmapPool(int maxSize, LruPoolStrategy strategy, Set<Bitmap.Config> allowedConfigs) {
LruBitmapPool(long maxSize, LruPoolStrategy strategy, Set<Bitmap.Config> allowedConfigs) {
this.initialMaxSize = maxSize;
this.maxSize = maxSize;
this.strategy = strategy;
Expand All @@ -50,7 +50,7 @@ public class LruBitmapPool implements BitmapPool {
*
* @param maxSize The initial maximum size of the pool in bytes.
*/
public LruBitmapPool(int maxSize) {
public LruBitmapPool(long maxSize) {
this(maxSize, getDefaultStrategy(), getDefaultAllowedConfigs());
}

Expand All @@ -62,12 +62,12 @@ public LruBitmapPool(int maxSize) {
* allowed to be put into the pool. Configs not in the allowed put will be
* rejected.
*/
public LruBitmapPool(int maxSize, Set<Bitmap.Config> allowedConfigs) {
public LruBitmapPool(long maxSize, Set<Bitmap.Config> allowedConfigs) {
this(maxSize, getDefaultStrategy(), allowedConfigs);
}

@Override
public int getMaxSize() {
public long getMaxSize() {
return maxSize;
}

Expand Down Expand Up @@ -216,7 +216,7 @@ public void trimMemory(int level) {
}
}

private synchronized void trimToSize(int size) {
private synchronized void trimToSize(long size) {
while (currentSize > size) {
final Bitmap removed = strategy.removeLast();
// TODO: This shouldn't ever happen, see #331.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* disk cache directory.
*
* <p>If you need to make I/O access before returning the cache directory use the {@link
* DiskLruCacheFactory#DiskLruCacheFactory(CacheDirectoryGetter, int)} constructor variant.
* DiskLruCacheFactory#DiskLruCacheFactory(CacheDirectoryGetter, long)} constructor variant.
*/
public class DiskLruCacheFactory implements DiskCache.Factory {
private final int diskCacheSize;
private final long diskCacheSize;
private final CacheDirectoryGetter cacheDirectoryGetter;

/**
Expand All @@ -20,7 +20,7 @@ public interface CacheDirectoryGetter {
File getCacheDirectory();
}

public DiskLruCacheFactory(final String diskCacheFolder, int diskCacheSize) {
public DiskLruCacheFactory(final String diskCacheFolder, long diskCacheSize) {
this(new CacheDirectoryGetter() {
@Override
public File getCacheDirectory() {
Expand All @@ -30,7 +30,7 @@ public File getCacheDirectory() {
}

public DiskLruCacheFactory(final String diskCacheFolder, final String diskCacheName,
int diskCacheSize) {
long diskCacheSize) {
this(new CacheDirectoryGetter() {
@Override
public File getCacheDirectory() {
Expand All @@ -46,7 +46,7 @@ public File getCacheDirectory() {
* @param cacheDirectoryGetter Interface called out of UI thread to get the cache folder.
* @param diskCacheSize Desired max bytes size for the LRU disk cache.
*/
public DiskLruCacheFactory(CacheDirectoryGetter cacheDirectoryGetter, int diskCacheSize) {
public DiskLruCacheFactory(CacheDirectoryGetter cacheDirectoryGetter, long diskCacheSize) {
this.diskCacheSize = diskCacheSize;
this.cacheDirectoryGetter = cacheDirectoryGetter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* The default DiskCache implementation. There must be no more than one active instance for a given
* directory at a time.
*
* @see #get(java.io.File, int)
* @see #get(java.io.File, long)
*/
public class DiskLruCacheWrapper implements DiskCache {
private static final String TAG = "DiskLruCacheWrapper";
Expand All @@ -26,7 +26,7 @@ public class DiskLruCacheWrapper implements DiskCache {

private final SafeKeyGenerator safeKeyGenerator;
private final File directory;
private final int maxSize;
private final long maxSize;
private final DiskCacheWriteLocker writeLocker = new DiskCacheWriteLocker();
private DiskLruCache diskLruCache;

Expand All @@ -39,7 +39,7 @@ public class DiskLruCacheWrapper implements DiskCache {
* @param maxSize The max size for the disk cache
* @return The new disk cache with the given arguments, or the current cache if one already exists
*/
public static synchronized DiskCache get(File directory, int maxSize) {
public static synchronized DiskCache get(File directory, long maxSize) {
// TODO calling twice with different arguments makes it return the cache for the same
// directory, it's public!
if (wrapper == null) {
Expand All @@ -48,7 +48,7 @@ public static synchronized DiskCache get(File directory, int maxSize) {
return wrapper;
}

protected DiskLruCacheWrapper(File directory, int maxSize) {
protected DiskLruCacheWrapper(File directory, long maxSize) {
this.directory = directory;
this.maxSize = maxSize;
this.safeKeyGenerator = new SafeKeyGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public ExternalPreferredCacheDiskCacheFactory(Context context) {
DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
}

public ExternalPreferredCacheDiskCacheFactory(Context context, int diskCacheSize) {
public ExternalPreferredCacheDiskCacheFactory(Context context, long diskCacheSize) {
this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, diskCacheSize);
}

public ExternalPreferredCacheDiskCacheFactory(final Context context, final String diskCacheName,
final int diskCacheSize) {
final long diskCacheSize) {
super(new CacheDirectoryGetter() {
@Nullable
private File getInternalCacheDirectory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public InternalCacheDiskCacheFactory(Context context) {
DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
}

public InternalCacheDiskCacheFactory(Context context, int diskCacheSize) {
public InternalCacheDiskCacheFactory(Context context, long diskCacheSize) {
this(context, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, diskCacheSize);
}

public InternalCacheDiskCacheFactory(final Context context, final String diskCacheName,
int diskCacheSize) {
long diskCacheSize) {
super(new CacheDirectoryGetter() {
@Override
public File getCacheDirectory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class LruResourceCache extends LruCache<Key, Resource<?>> implements Memo
*
* @param size The maximum size in bytes the in memory cache can use.
*/
public LruResourceCache(int size) {
public LruResourceCache(long size) {
super(size);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ interface ResourceRemovedListener {
/**
* Returns the sum of the sizes of all the contents of the cache in bytes.
*/
int getCurrentSize();
long getCurrentSize();

/**
* Returns the current maximum size in bytes of the cache.
*/
int getMaxSize();
long getMaxSize();

/**
* Adjust the maximum size of the cache by multiplying the original size of the cache by the given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class MemoryCacheAdapter implements MemoryCache {
private ResourceRemovedListener listener;

@Override
public int getCurrentSize() {
public long getCurrentSize() {
return 0;
}

@Override
public int getMaxSize() {
public long getMaxSize() {
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private boolean isGcDetected(long startTimeMs) {
return clock.now() - startTimeMs >= MAX_DURATION_MS;
}

private int getFreeMemoryCacheBytes() {
private long getFreeMemoryCacheBytes() {
return memoryCache.getMaxSize() - memoryCache.getCurrentSize();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void preFill(PreFillType.Builder... bitmapAttributeBuilders) {

// Visible for testing.
PreFillQueue generateAllocationOrder(PreFillType... preFillSizes) {
final int maxSize =
final long maxSize =
memoryCache.getMaxSize() - memoryCache.getCurrentSize() + bitmapPool.getMaxSize();

int totalWeight = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ModelCache() {
this(DEFAULT_SIZE);
}

public ModelCache(int size) {
public ModelCache(long size) {
cache = new LruCache<ModelKey<A>, B>(size) {
@Override
protected void onItemEvicted(ModelKey<A> key, B item) {
Expand Down
14 changes: 7 additions & 7 deletions library/src/main/java/com/bumptech/glide/util/LruCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
*/
public class LruCache<T, Y> {
private final LinkedHashMap<T, Y> cache = new LinkedHashMap<>(100, 0.75f, true);
private final int initialMaxSize;
private int maxSize;
private int currentSize = 0;
private final long initialMaxSize;
private long maxSize;
private long currentSize = 0;

/**
* Constructor for LruCache.
*
* @param size The maximum size of the cache, the units must match the units used in {@link
* #getSize(Object)}.
*/
public LruCache(int size) {
public LruCache(long size) {
this.initialMaxSize = size;
this.maxSize = size;
}
Expand Down Expand Up @@ -75,14 +75,14 @@ protected void onItemEvicted(T key, Y item) {
/**
* Returns the current maximum size of the cache in bytes.
*/
public synchronized int getMaxSize() {
public synchronized long getMaxSize() {
return maxSize;
}

/**
* Returns the sum of the sizes of all items in the cache.
*/
public synchronized int getCurrentSize() {
public synchronized long getCurrentSize() {
return currentSize;
}

Expand Down Expand Up @@ -164,7 +164,7 @@ public void clearMemory() {
*
* @param size The size the cache should be less than.
*/
protected synchronized void trimToSize(int size) {
protected synchronized void trimToSize(long size) {
Map.Entry<T, Y> last;
while (currentSize > size) {
last = cache.entrySet().iterator().next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public void testPreFillHandlerDoesNotPostIfHasBitmapsButIsCancelled() {
@Test
public void testAddsBitmapsToMemoryCacheIfMemoryCacheHasEnoughSpaceRemaining() {
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
when(cache.getMaxSize()).thenReturn(Util.getBitmapByteSize(bitmap));
when(cache.getMaxSize()).thenReturn(Long.valueOf(Util.getBitmapByteSize(bitmap)));

PreFillType size =
new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()).setConfig(bitmap.getConfig())
Expand All @@ -214,7 +214,7 @@ public void testAddsBitmapsToMemoryCacheIfMemoryCacheHasEnoughSpaceRemaining() {
@Test
public void testAddsBitmapsToBitmapPoolIfMemoryCacheIsFull() {
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
when(cache.getMaxSize()).thenReturn(0);
when(cache.getMaxSize()).thenReturn(0L);

PreFillType size =
new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()).setConfig(bitmap.getConfig())
Expand All @@ -233,7 +233,7 @@ public void testAddsBitmapsToBitmapPoolIfMemoryCacheIsFull() {
@Test
public void testAddsBitmapsToPoolIfMemoryCacheIsNotFullButCannotFitBitmap() {
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
when(cache.getMaxSize()).thenReturn(Util.getBitmapByteSize(bitmap) / 2);
when(cache.getMaxSize()).thenReturn((long) Util.getBitmapByteSize(bitmap) / 2);

PreFillType size =
new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()).setConfig(bitmap.getConfig())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public class BitmapPreFillerTest {
private final Bitmap.Config defaultBitmapConfig = PreFillType.DEFAULT_CONFIG;
private final Bitmap defaultBitmap =
Bitmap.createBitmap(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT, defaultBitmapConfig);
private final int defaultBitmapSize = Util.getBitmapByteSize(defaultBitmap);
private final int poolSize = BITMAPS_IN_CACHE * defaultBitmapSize;
private final int cacheSize = BITMAPS_IN_POOL * defaultBitmapSize;
private final long defaultBitmapSize = Util.getBitmapByteSize(defaultBitmap);
private final long poolSize = BITMAPS_IN_CACHE * defaultBitmapSize;
private final long cacheSize = BITMAPS_IN_POOL * defaultBitmapSize;

@Mock BitmapPool pool;
@Mock MemoryCache cache;
Expand Down Expand Up @@ -88,7 +88,7 @@ public void testAllocationOrderThatDoesNotFitExactlyIntoGivenSizeRoundsDown() {
}

int expectedSize = 0;
int maxSize = poolSize + cacheSize;
long maxSize = poolSize + cacheSize;
for (PreFillType current : sizes) {
int currentSize =
Util.getBitmapByteSize(current.getWidth(), current.getHeight(), current.getConfig());
Expand All @@ -108,7 +108,7 @@ public void testAllocationOrderDoesNotOverFillWithMultipleSizes() {
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2)
.setConfig(defaultBitmapConfig).build() });

int byteSize = 0;
long byteSize = 0;
while (!allocationOrder.isEmpty()) {
PreFillType current = allocationOrder.remove();
byteSize +=
Expand All @@ -128,7 +128,7 @@ public void testAllocationOrderDoesNotOverFillWithMultipleSizesAndWeights() {
new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 3)
.setConfig(defaultBitmapConfig).setWeight(3).build() });

int byteSize = 0;
long byteSize = 0;
while (!allocationOrder.isEmpty()) {
PreFillType current = allocationOrder.remove();
byteSize +=
Expand Down

0 comments on commit d402780

Please sign in to comment.