Skip to content

Commit

Permalink
Bind the number of decode threads to the flex pool params
Browse files Browse the repository at this point in the history
  • Loading branch information
tyronen committed Jul 20, 2015
1 parent 735a70f commit 2b74e36
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@
public class DefaultExecutorSupplier implements ExecutorSupplier {
// Allows for simultaneous reads and writes.
private static final int NUM_IO_BOUND_THREADS = 2;
private static final int NUM_CPU_BOUND_THREADS = Runtime.getRuntime().availableProcessors();
private static final int KEEP_ALIVE_SECONDS = 60;

private final Executor mIoBoundExecutor;
private final Executor mCpuBoundExecutor;
private final Executor mDecodeExecutor;

public DefaultExecutorSupplier() {
public DefaultExecutorSupplier(int numCpuBoundThreads) {
mIoBoundExecutor = Executors.newFixedThreadPool(NUM_IO_BOUND_THREADS);
mCpuBoundExecutor = new ThreadPoolExecutor(
1, // keep at least that many threads alive
NUM_CPU_BOUND_THREADS, // maximum number of allowed threads
numCpuBoundThreads, // maximum number of allowed threads
KEEP_ALIVE_SECONDS, // amount of seconds each cached thread waits before being terminated
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ private ImagePipelineConfig(Builder builder) {
builder.mEncodedMemoryCacheParamsSupplier == null ?
new DefaultEncodedMemoryCacheParamsSupplier() :
builder.mEncodedMemoryCacheParamsSupplier;
mExecutorSupplier =
builder.mExecutorSupplier == null ?
new DefaultExecutorSupplier() :
builder.mExecutorSupplier;
mImageCacheStatsTracker =
builder.mImageCacheStatsTracker == null ?
NoOpImageCacheStatsTracker.getInstance() :
Expand Down Expand Up @@ -149,6 +145,8 @@ public Boolean get() {
mMainDiskCacheConfig :
builder.mSmallImageDiskCacheConfig;

int decodeThreads = mPoolFactory.getFlexByteArrayPoolMaxNumThreads();

mAnimatedDrawableUtil = new AnimatedDrawableUtil();
AnimatedDrawableBackendProvider animatedDrawableBackendProvider =
new AnimatedDrawableBackendProvider() {
Expand All @@ -163,7 +161,7 @@ public AnimatedDrawableBackend get(AnimatedImageResult imageResult, Rect bounds)
mPoolFactory.getFlexByteArrayPool(),
isDownsampleEnabled());
ArtBitmapFactory factoryLollipop =
new ArtBitmapFactory(mPoolFactory.getBitmapPool(), 1, isDownsampleEnabled());
new ArtBitmapFactory(mPoolFactory.getBitmapPool(), decodeThreads, isDownsampleEnabled());
mPlatformBitmapFactory =
new PlatformBitmapFactory(
factoryGingerbread,
Expand All @@ -174,6 +172,10 @@ public AnimatedDrawableBackend get(AnimatedImageResult imageResult, Rect bounds)
new AnimatedImageFactory(animatedDrawableBackendProvider, mPlatformBitmapFactory) :
builder.mAnimatedImageFactory;

mExecutorSupplier =
builder.mExecutorSupplier == null ?
new DefaultExecutorSupplier(decodeThreads) : builder.mExecutorSupplier;

mImageDecoder =
builder.mImageDecoder == null ?
new ImageDecoder(mAnimatedImageFactory, mPlatformBitmapFactory) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DefaultFlexByteArrayPoolParams {
// the min buffer size we'll use
private static final int DEFAULT_MIN_BYTE_ARRAY_SIZE = 128 * ByteConstants.KB;
// the maximum number of threads permitted to touch this pool
public static final int DEFAULT_MAX_NUM_THREADS = 1;
public static final int DEFAULT_MAX_NUM_THREADS = Runtime.getRuntime().availableProcessors();

private DefaultFlexByteArrayPoolParams() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

package com.facebook.imagepipeline.memory;

import javax.annotation.concurrent.Immutable;

import com.facebook.common.internal.Preconditions;
import com.facebook.common.memory.MemoryTrimmableRegistry;
import com.facebook.common.memory.NoOpMemoryTrimmableRegistry;

/**
* Configuration class for pools.
*/
@Immutable
public class PoolConfig {

// There are a lot of parameters in this class. Please follow strict alphabetical order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public FlexByteArrayPool getFlexByteArrayPool() {
return mFlexByteArrayPool;
}

public int getFlexByteArrayPoolMaxNumThreads() {
return mConfig.getFlexByteArrayPoolParams().maxNumThreads;
}

public NativeMemoryChunkPool getNativeMemoryChunkPool() {
if (mNativeMemoryChunkPool == null) {
mNativeMemoryChunkPool = new NativeMemoryChunkPool(
Expand Down

0 comments on commit 2b74e36

Please sign in to comment.