diff --git a/buffer/src/main/java/io/netty/buffer/PoolArena.java b/buffer/src/main/java/io/netty/buffer/PoolArena.java index 3bc948be41..8ef5d76ea8 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolArena.java +++ b/buffer/src/main/java/io/netty/buffer/PoolArena.java @@ -16,7 +16,6 @@ package io.netty.buffer; -import io.netty.util.internal.LongCounter; import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.StringUtil; @@ -25,6 +24,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.LongAdder; import static java.lang.Math.max; @@ -63,18 +63,18 @@ abstract class PoolArena implements PoolArenaMetric { // Metrics for allocations and deallocations private long allocationsNormal; - // We need to use the LongCounter here as this is not guarded via synchronized block. - private final LongCounter allocationsTiny = PlatformDependent.newLongCounter(); - private final LongCounter allocationsSmall = PlatformDependent.newLongCounter(); - private final LongCounter allocationsHuge = PlatformDependent.newLongCounter(); - private final LongCounter activeBytesHuge = PlatformDependent.newLongCounter(); + // We need to use the LongAdder here as this is not guarded via synchronized block. + private final LongAdder allocationsTiny = new LongAdder(); + private final LongAdder allocationsSmall = new LongAdder(); + private final LongAdder allocationsHuge = new LongAdder(); + private final LongAdder activeBytesHuge = new LongAdder(); private long deallocationsTiny; private long deallocationsSmall; private long deallocationsNormal; - // We need to use the LongCounter here as this is not guarded via synchronized block. - private final LongCounter deallocationsHuge = PlatformDependent.newLongCounter(); + // We need to use the LongAdder here as this is not guarded via synchronized block. + private final LongAdder deallocationsHuge = new LongAdder(); // Number of thread caches backed by this arena. final AtomicInteger numThreadCaches = new AtomicInteger(); @@ -103,12 +103,12 @@ abstract class PoolArena implements PoolArenaMetric { smallSubpagePools[i] = newSubpagePoolHead(pageSize); } - q100 = new PoolChunkList(this, null, 100, Integer.MAX_VALUE, chunkSize); - q075 = new PoolChunkList(this, q100, 75, 100, chunkSize); - q050 = new PoolChunkList(this, q075, 50, 100, chunkSize); - q025 = new PoolChunkList(this, q050, 25, 75, chunkSize); - q000 = new PoolChunkList(this, q025, 1, 50, chunkSize); - qInit = new PoolChunkList(this, q000, Integer.MIN_VALUE, 25, chunkSize); + q100 = new PoolChunkList<>(this, null, 100, Integer.MAX_VALUE, chunkSize); + q075 = new PoolChunkList<>(this, q100, 75, 100, chunkSize); + q050 = new PoolChunkList<>(this, q075, 50, 100, chunkSize); + q025 = new PoolChunkList<>(this, q050, 25, 75, chunkSize); + q000 = new PoolChunkList<>(this, q025, 1, 50, chunkSize); + qInit = new PoolChunkList<>(this, q000, Integer.MIN_VALUE, 25, chunkSize); q100.prevList(q075); q075.prevList(q050); @@ -117,7 +117,7 @@ abstract class PoolArena implements PoolArenaMetric { q000.prevList(null); qInit.prevList(qInit); - List metrics = new ArrayList(6); + List metrics = new ArrayList<>(6); metrics.add(qInit); metrics.add(q000); metrics.add(q025); @@ -455,7 +455,7 @@ abstract class PoolArena implements PoolArenaMetric { } private static List subPageMetricList(PoolSubpage[] pages) { - List metrics = new ArrayList(); + List metrics = new ArrayList<>(); for (PoolSubpage head : pages) { if (head.next == head) { continue; @@ -478,17 +478,17 @@ abstract class PoolArena implements PoolArenaMetric { synchronized (this) { allocsNormal = allocationsNormal; } - return allocationsTiny.value() + allocationsSmall.value() + allocsNormal + allocationsHuge.value(); + return allocationsTiny.longValue() + allocationsSmall.longValue() + allocsNormal + allocationsHuge.longValue(); } @Override public long numTinyAllocations() { - return allocationsTiny.value(); + return allocationsTiny.longValue(); } @Override public long numSmallAllocations() { - return allocationsSmall.value(); + return allocationsSmall.longValue(); } @Override @@ -502,7 +502,7 @@ abstract class PoolArena implements PoolArenaMetric { synchronized (this) { deallocs = deallocationsTiny + deallocationsSmall + deallocationsNormal; } - return deallocs + deallocationsHuge.value(); + return deallocs + deallocationsHuge.longValue(); } @Override @@ -522,18 +522,18 @@ abstract class PoolArena implements PoolArenaMetric { @Override public long numHugeAllocations() { - return allocationsHuge.value(); + return allocationsHuge.longValue(); } @Override public long numHugeDeallocations() { - return deallocationsHuge.value(); + return deallocationsHuge.longValue(); } @Override public long numActiveAllocations() { - long val = allocationsTiny.value() + allocationsSmall.value() + allocationsHuge.value() - - deallocationsHuge.value(); + long val = allocationsTiny.longValue() + allocationsSmall.longValue() + allocationsHuge.longValue() + - deallocationsHuge.longValue(); synchronized (this) { val += allocationsNormal - (deallocationsTiny + deallocationsSmall + deallocationsNormal); } @@ -566,7 +566,7 @@ abstract class PoolArena implements PoolArenaMetric { @Override public long numActiveBytes() { - long val = activeBytesHuge.value(); + long val = activeBytesHuge.longValue(); synchronized (this) { for (int i = 0; i < chunkListMetrics.size(); i++) { for (PoolChunkMetric m: chunkListMetrics.get(i)) { @@ -683,12 +683,12 @@ abstract class PoolArena implements PoolArenaMetric { @Override protected PoolChunk newChunk(int pageSize, int maxOrder, int pageShifts, int chunkSize) { - return new PoolChunk(this, newByteArray(chunkSize), pageSize, maxOrder, pageShifts, chunkSize, 0); + return new PoolChunk<>(this, newByteArray(chunkSize), pageSize, maxOrder, pageShifts, chunkSize, 0); } @Override protected PoolChunk newUnpooledChunk(int capacity) { - return new PoolChunk(this, newByteArray(capacity), capacity, 0); + return new PoolChunk<>(this, newByteArray(capacity), capacity, 0); } @Override @@ -741,13 +741,13 @@ abstract class PoolArena implements PoolArenaMetric { protected PoolChunk newChunk(int pageSize, int maxOrder, int pageShifts, int chunkSize) { if (directMemoryCacheAlignment == 0) { - return new PoolChunk(this, + return new PoolChunk<>(this, allocateDirect(chunkSize), pageSize, maxOrder, pageShifts, chunkSize, 0); } final ByteBuffer memory = allocateDirect(chunkSize + directMemoryCacheAlignment); - return new PoolChunk(this, memory, pageSize, + return new PoolChunk<>(this, memory, pageSize, maxOrder, pageShifts, chunkSize, offsetCacheLine(memory)); } @@ -755,12 +755,12 @@ abstract class PoolArena implements PoolArenaMetric { @Override protected PoolChunk newUnpooledChunk(int capacity) { if (directMemoryCacheAlignment == 0) { - return new PoolChunk(this, + return new PoolChunk<>(this, allocateDirect(capacity), capacity, 0); } final ByteBuffer memory = allocateDirect(capacity + directMemoryCacheAlignment); - return new PoolChunk(this, memory, capacity, + return new PoolChunk<>(this, memory, capacity, offsetCacheLine(memory)); } diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java b/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java index 6fe188ed8b..4a1d6ff0db 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java @@ -15,11 +15,11 @@ */ package io.netty.buffer; -import io.netty.util.internal.LongCounter; import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.StringUtil; import java.nio.ByteBuffer; +import java.util.concurrent.atomic.LongAdder; /** * Simplistic {@link ByteBufAllocator} implementation that does not pool anything. @@ -247,17 +247,17 @@ public final class UnpooledByteBufAllocator extends AbstractByteBufAllocator imp } private static final class UnpooledByteBufAllocatorMetric implements ByteBufAllocatorMetric { - final LongCounter directCounter = PlatformDependent.newLongCounter(); - final LongCounter heapCounter = PlatformDependent.newLongCounter(); + final LongAdder directCounter = new LongAdder(); + final LongAdder heapCounter = new LongAdder(); @Override public long usedHeapMemory() { - return heapCounter.value(); + return heapCounter.longValue(); } @Override public long usedDirectMemory() { - return directCounter.value(); + return directCounter.longValue(); } @Override diff --git a/common/src/main/java/io/netty/util/internal/LongAdderCounter.java b/common/src/main/java/io/netty/util/internal/LongAdderCounter.java deleted file mode 100644 index f7eeb81642..0000000000 --- a/common/src/main/java/io/netty/util/internal/LongAdderCounter.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2017 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.util.internal; - -import java.util.concurrent.atomic.LongAdder; - -final class LongAdderCounter extends LongAdder implements LongCounter { - - @Override - public long value() { - return longValue(); - } -} diff --git a/common/src/main/java/io/netty/util/internal/LongCounter.java b/common/src/main/java/io/netty/util/internal/LongCounter.java deleted file mode 100644 index b56938cb36..0000000000 --- a/common/src/main/java/io/netty/util/internal/LongCounter.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2015 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.util.internal; - -/** - * Counter for long. - */ -public interface LongCounter { - void add(long delta); - void increment(); - void decrement(); - long value(); -} diff --git a/common/src/main/java/io/netty/util/internal/PlatformDependent.java b/common/src/main/java/io/netty/util/internal/PlatformDependent.java index 1baeecbf7c..e16507a975 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent.java @@ -328,7 +328,7 @@ public final class PlatformDependent { if (hasUnsafe()) { PlatformDependent0.throwException(t); } else { - PlatformDependent.throwException0(t); + PlatformDependent.throwException0(t); } } @@ -341,32 +341,21 @@ public final class PlatformDependent { * Creates a new fastest {@link ConcurrentMap} implementation for the current platform. */ public static ConcurrentMap newConcurrentHashMap() { - return new ConcurrentHashMap(); - } - - /** - * Creates a new fastest {@link LongCounter} implementation for the current platform. - */ - public static LongCounter newLongCounter() { - if (javaVersion() >= 8) { - return new LongAdderCounter(); - } else { - return new AtomicLongCounter(); - } + return new ConcurrentHashMap<>(); } /** * Creates a new fastest {@link ConcurrentMap} implementation for the current platform. */ public static ConcurrentMap newConcurrentHashMap(int initialCapacity) { - return new ConcurrentHashMap(initialCapacity); + return new ConcurrentHashMap<>(initialCapacity); } /** * Creates a new fastest {@link ConcurrentMap} implementation for the current platform. */ public static ConcurrentMap newConcurrentHashMap(int initialCapacity, float loadFactor) { - return new ConcurrentHashMap(initialCapacity, loadFactor); + return new ConcurrentHashMap<>(initialCapacity, loadFactor); } /** @@ -374,14 +363,14 @@ public final class PlatformDependent { */ public static ConcurrentMap newConcurrentHashMap( int initialCapacity, float loadFactor, int concurrencyLevel) { - return new ConcurrentHashMap(initialCapacity, loadFactor, concurrencyLevel); + return new ConcurrentHashMap<>(initialCapacity, loadFactor, concurrencyLevel); } /** * Creates a new fastest {@link ConcurrentMap} implementation for the current platform. */ public static ConcurrentMap newConcurrentHashMap(Map map) { - return new ConcurrentHashMap(map); + return new ConcurrentHashMap<>(map); } /** @@ -867,13 +856,13 @@ public final class PlatformDependent { // This is forced by the MpscChunkedArrayQueue implementation as will try to round it // up to the next power of two and so will overflow otherwise. final int capacity = max(min(maxCapacity, MAX_ALLOWED_MPSC_CAPACITY), MIN_MAX_MPSC_CAPACITY); - return USE_MPSC_CHUNKED_ARRAY_QUEUE ? new MpscChunkedArrayQueue(MPSC_CHUNK_SIZE, capacity) - : new MpscGrowableAtomicArrayQueue(MPSC_CHUNK_SIZE, capacity); + return USE_MPSC_CHUNKED_ARRAY_QUEUE ? new MpscChunkedArrayQueue<>(MPSC_CHUNK_SIZE, capacity) + : new MpscGrowableAtomicArrayQueue<>(MPSC_CHUNK_SIZE, capacity); } static Queue newMpscQueue() { - return USE_MPSC_CHUNKED_ARRAY_QUEUE ? new MpscUnboundedArrayQueue(MPSC_CHUNK_SIZE) - : new MpscUnboundedAtomicArrayQueue(MPSC_CHUNK_SIZE); + return USE_MPSC_CHUNKED_ARRAY_QUEUE ? new MpscUnboundedArrayQueue<>(MPSC_CHUNK_SIZE) + : new MpscUnboundedAtomicArrayQueue<>(MPSC_CHUNK_SIZE); } } @@ -899,7 +888,7 @@ public final class PlatformDependent { * consumer (one thread!). */ public static Queue newSpscQueue() { - return hasUnsafe() ? new SpscLinkedQueue() : new SpscLinkedAtomicQueue(); + return hasUnsafe() ? new SpscLinkedQueue<>() : new SpscLinkedAtomicQueue<>(); } /** @@ -907,7 +896,7 @@ public final class PlatformDependent { * consumer (one thread!) with the given fixes {@code capacity}. */ public static Queue newFixedMpscQueue(int capacity) { - return hasUnsafe() ? new MpscArrayQueue(capacity) : new MpscAtomicArrayQueue(capacity); + return hasUnsafe() ? new MpscArrayQueue<>(capacity) : new MpscAtomicArrayQueue<>(capacity); } /** @@ -936,9 +925,9 @@ public final class PlatformDependent { */ public static Deque newConcurrentDeque() { if (javaVersion() < 7) { - return new LinkedBlockingDeque(); + return new LinkedBlockingDeque<>(); } else { - return new ConcurrentLinkedDeque(); + return new ConcurrentLinkedDeque<>(); } } @@ -1356,30 +1345,6 @@ public final class PlatformDependent { return "unknown"; } - private static final class AtomicLongCounter extends AtomicLong implements LongCounter { - private static final long serialVersionUID = 4074772784610639305L; - - @Override - public void add(long delta) { - addAndGet(delta); - } - - @Override - public void increment() { - incrementAndGet(); - } - - @Override - public void decrement() { - decrementAndGet(); - } - - @Override - public long value() { - return get(); - } - } - private interface ThreadLocalRandomProvider { Random current(); }