Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Maurer committed Mar 5, 2014
1 parent 53035fd commit 7c22edd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
21 changes: 6 additions & 15 deletions buffer/src/main/java/io/netty/buffer/PoolThreadCache.java
Expand Up @@ -28,21 +28,18 @@ final class PoolThreadCache {
final PoolArena<byte[]> heapArena;
final PoolArena<ByteBuffer> directArena;

// Used for bitshifting when calculate the index of normal caches later
private final int valNormalDirect;
private final int valNormalHeap;

// We cold also create them lazy but this would make the code more clumby and also introduce more branches when
// check if allocation // adding is possible so not sure it worth it at all.
// Hold the caches for the different size classes, which are tiny, small and normal.
private final PoolChunkCache<byte[]>[] tinySubPageHeapCaches;
private final PoolChunkCache<byte[]>[] smallSubPageHeapCaches;
private final PoolChunkCache<ByteBuffer>[] tinySubPageDirectCaches;
private final PoolChunkCache<ByteBuffer>[] smallSubPageDirectCaches;

// Hold the caches for normal allocations
private final PoolChunkCache<byte[]>[] normalHeapCaches;
private final PoolChunkCache<ByteBuffer>[] normalDirectCaches;

// Used for bitshifting when calculate the index of normal caches later
private final int valNormalDirect;
private final int valNormalHeap;

// TODO: Test if adding padding helps under contention
//private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;

Expand Down Expand Up @@ -173,13 +170,7 @@ boolean add(PoolArena area, PoolChunk chunk, long handle, int normCapacity) {
PoolChunkCache cache;
if (area.isTinyOrSmall(normCapacity)) {
if (PoolArena.isTiny(normCapacity)) {
//cache = cacheForTiny(area, normCapacity);
int idx = PoolArena.tinyIdx(normCapacity);
if (area.isDirect()) {
cache = cache(tinySubPageDirectCaches, idx);
} else {
cache = cache(tinySubPageHeapCaches, idx);
}
cache = cacheForTiny(area, normCapacity);
} else {
cache = cacheForSmall(area, normCapacity);
}
Expand Down
23 changes: 12 additions & 11 deletions buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java
Expand Up @@ -42,7 +42,7 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {
private static final int DEFAULT_SMALL_CACHE_SIZE;
private static final int DEFAULT_NORMAL_CACHE_SIZE;
private static final int DEFAULT_FREEUP_INTERVAL;
private static final int DEFAULT_MAX_CACHE_SIZE;
private static final int DEFAULT_MAX_CACHE_BUFFER_SIZE;
private static final int DEFAULT_MAX_CACHE_ARRAY_SIZE;

private static final int MIN_PAGE_SIZE = 4096;
Expand Down Expand Up @@ -93,7 +93,7 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {

// 32 kb is the maximum of size which is cached. Similar to what is explained in
// 'Scalable memory allocation using jemalloc'
DEFAULT_MAX_CACHE_SIZE = SystemPropertyUtil.getInt("io.netty.allocator.maxCacheSize", 32 * 1024);
DEFAULT_MAX_CACHE_BUFFER_SIZE = SystemPropertyUtil.getInt("io.netty.allocator.maxCacheBufferSize", 32 * 1024);

// Maximal of 4 different size caches of normal allocations
DEFAULT_MAX_CACHE_ARRAY_SIZE = SystemPropertyUtil.getInt("io.netty.allocator.maxNormalCacheLevels", 4);
Expand All @@ -118,7 +118,7 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {
logger.debug("-Dio.netty.allocator.tinyCacheSize: {}", DEFAULT_TINY_CACHE_SIZE);
logger.debug("-Dio.netty.allocator.smallCacheSize: {}", DEFAULT_SMALL_CACHE_SIZE);
logger.debug("-Dio.netty.allocator.normalCacheSize: {}", DEFAULT_NORMAL_CACHE_SIZE);
logger.debug("-Dio.netty.allocator.maxCacheSize: {}", DEFAULT_MAX_CACHE_SIZE);
logger.debug("-Dio.netty.allocator.maxCacheBufferSize: {}", DEFAULT_MAX_CACHE_BUFFER_SIZE);
logger.debug("-Dio.netty.allocator.maxNormalCacheLevels: {}", DEFAULT_MAX_CACHE_ARRAY_SIZE);
logger.debug("-Dio.netty.allocator.freeUpCacheInterval: {}s", DEFAULT_FREEUP_INTERVAL);
}
Expand Down Expand Up @@ -159,18 +159,18 @@ protected PoolThreadCache initialValue() {
// easily free the cached stuff again once the EventExecutor completes later.
final PoolThreadCache cache = new PoolThreadCache(
heapArena, directArena, tinyCacheSize, smallCacheSize, normalCacheSize,
DEFAULT_MAX_CACHE_SIZE, DEFAULT_MAX_CACHE_ARRAY_SIZE);
DEFAULT_MAX_CACHE_BUFFER_SIZE, DEFAULT_MAX_CACHE_ARRAY_SIZE);

// free up cached resources when executor is terminated and so the Thread ends
// Free up cached resources when executor is terminated
executor.terminationFuture().addListener(new FutureListener<Object>() {
@Override
public void operationComplete(Future<Object> future) throws Exception {
cache.free();
}
});

// schedule task which will free up resources out of the cache when not needed
// TODO: Do we need to make the interval also confiurable via constructor?
// Schedule task which will free up resources out of the cache when not needed
// TODO: Do we need to make the interval also configurable via constructor?
executor.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
Expand All @@ -180,10 +180,11 @@ public void run() {
return cache;
} else {
// TODO: Maybe handle this with a ReferenceQueue and PhantomReferences and also cache for
// non EventThreads. This will need an extra Thread and I'm not sure yet if we really need this.
// Mainly all the allocations of ByteBuf are done out of the EventExecutor / EventLoop anyway.
// The only thing I can think of that would need this is when the PooledByteBufAllocator is used
// outside of netty itself. Not sure if this worth the extra overhead.
// Threads that are not used for an EventExecutor. This will need an extra Thread
// and I'm not sure yet if we really need this. Mainly all the allocations of ByteBuf
// are done out of the EventExecutor / EventLoop anyway. The only thing I can think of that
// would need this is when the PooledByteBufAllocator is used outside of netty itself.
// Not sure if this worth the extra overhead.
return new PoolThreadCache(heapArena, directArena, 0, 0, 0, 0, 0);
}
}
Expand Down

0 comments on commit 7c22edd

Please sign in to comment.