Skip to content

Commit

Permalink
Address comments of trustin
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Maurer committed Mar 18, 2014
1 parent bcd9e09 commit 29a613b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
30 changes: 15 additions & 15 deletions buffer/src/main/java/io/netty/buffer/PoolThreadCache.java
Expand Up @@ -162,7 +162,7 @@ private boolean allocate(MemoryRegionCache<?> cache, PooledByteBuf buf, int reqC
boolean allocated = cache.allocate(buf, reqCapacity);
if (++ allocations >= freeSweepAllocationThreshold) {
allocations = 0;
freeUpIfNecessary();
trim();
}
return allocated;
}
Expand Down Expand Up @@ -217,29 +217,29 @@ private static void free(MemoryRegionCache<?> cache) {
cache.free();
}

void freeUpIfNecessary() {
freeUpIfNecessary(tinySubPageDirectCaches);
freeUpIfNecessary(smallSubPageDirectCaches);
freeUpIfNecessary(normalDirectCaches);
freeUpIfNecessary(tinySubPageHeapCaches);
freeUpIfNecessary(smallSubPageHeapCaches);
freeUpIfNecessary(normalHeapCaches);
void trim() {
trim(tinySubPageDirectCaches);
trim(smallSubPageDirectCaches);
trim(normalDirectCaches);
trim(tinySubPageHeapCaches);
trim(smallSubPageHeapCaches);
trim(normalHeapCaches);
}

private static void freeUpIfNecessary(MemoryRegionCache<?>[] caches) {
private static void trim(MemoryRegionCache<?>[] caches) {
if (caches == null) {
return;
}
for (int i = 0; i < caches.length; i++) {
freeUpIfNecessary(caches[i]);
trim(caches[i]);
}
}

private static void freeUpIfNecessary(MemoryRegionCache<?> cache) {
private static void trim(MemoryRegionCache<?> cache) {
if (cache == null) {
return;
}
cache.freeUpIfNecessary();
cache.trim();
}

private MemoryRegionCache<?> cacheForTiny(PoolArena<?> area, int normCapacity) {
Expand Down Expand Up @@ -398,14 +398,14 @@ public void free() {
/**
* Free up cached {@link PoolChunk}s if not allocated frequently enough.
*/
private void freeUpIfNecessary() {
private void trim() {
int free = size() - maxEntriesInUse;
entriesInUse = 0;
maxEntriesInUse = 0;

if (free <= maxUnusedCached) {
return;
}
entriesInUse = 0;
maxEntriesInUse = 0;

int i = head;
for (; free > 0; free--) {
Expand Down
12 changes: 6 additions & 6 deletions buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java
Expand Up @@ -37,7 +37,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_MAX_CACHED_BUFFER_CAPACITY;
private static final int DEFAULT_CACHE_FREE_SWEEP_ALLOCATION_THRESHOLD;
private static final int DEFAULT_CACHE_TRIM_INTERVAL;

private static final int MIN_PAGE_SIZE = 4096;
private static final int MAX_CHUNK_SIZE = (int) (((long) Integer.MAX_VALUE + 1) / 2);
Expand Down Expand Up @@ -91,8 +91,8 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {
"io.netty.allocator.maxCachedBufferCapacity", 32 * 1024);

// the number of threshold of allocations when cached entries will be freed up if not frequently used
DEFAULT_CACHE_FREE_SWEEP_ALLOCATION_THRESHOLD = SystemPropertyUtil.getInt(
"io.netty.allocator.cacheFreeSweepAllocationThreshold", 8192);
DEFAULT_CACHE_TRIM_INTERVAL = SystemPropertyUtil.getInt(
"io.netty.allocator.cacheTrimInterval", 8192);

if (logger.isDebugEnabled()) {
logger.debug("-Dio.netty.allocator.numHeapArenas: {}", DEFAULT_NUM_HEAP_ARENA);
Expand All @@ -112,8 +112,8 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {
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.maxCachedBufferCapacity: {}", DEFAULT_MAX_CACHED_BUFFER_CAPACITY);
logger.debug("-Dio.netty.allocator.cacheFreeSweepAllocationThreshold: {}",
DEFAULT_CACHE_FREE_SWEEP_ALLOCATION_THRESHOLD);
logger.debug("-Dio.netty.allocator.cacheTrimInterval: {}",
DEFAULT_CACHE_TRIM_INTERVAL);
}
}

Expand Down Expand Up @@ -308,7 +308,7 @@ public PoolThreadCache get() {
// easily free the cached stuff again once the EventExecutor completes later.
cache = new PoolThreadCache(
heapArena, directArena, tinyCacheSize, smallCacheSize, normalCacheSize,
DEFAULT_MAX_CACHED_BUFFER_CAPACITY, DEFAULT_CACHE_FREE_SWEEP_ALLOCATION_THRESHOLD);
DEFAULT_MAX_CACHED_BUFFER_CAPACITY, DEFAULT_CACHE_TRIM_INTERVAL);
set(cache);
}
return cache;
Expand Down

0 comments on commit 29a613b

Please sign in to comment.