Java 8 migration. Removed custom LongCounter and LongAdderCounter classes and replaced with direct reference to LongAdder (#8750)

Motivation:

After switching to Java 8 we no longer need LongCounter and LongAdderCounter. We can directly replace them with LongAdder.

Modification:

Removed LongCounter and LongAdderCounter classes.

Result:

Less code to maintain
This commit is contained in:
Dmitriy Dumanskiy 2019-01-22 14:53:28 +02:00 committed by Norman Maurer
parent 82b0db5013
commit b2a9a8e9fe
5 changed files with 50 additions and 137 deletions

View File

@ -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<T> 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<T> implements PoolArenaMetric {
smallSubpagePools[i] = newSubpagePoolHead(pageSize);
}
q100 = new PoolChunkList<T>(this, null, 100, Integer.MAX_VALUE, chunkSize);
q075 = new PoolChunkList<T>(this, q100, 75, 100, chunkSize);
q050 = new PoolChunkList<T>(this, q075, 50, 100, chunkSize);
q025 = new PoolChunkList<T>(this, q050, 25, 75, chunkSize);
q000 = new PoolChunkList<T>(this, q025, 1, 50, chunkSize);
qInit = new PoolChunkList<T>(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<T> implements PoolArenaMetric {
q000.prevList(null);
qInit.prevList(qInit);
List<PoolChunkListMetric> metrics = new ArrayList<PoolChunkListMetric>(6);
List<PoolChunkListMetric> metrics = new ArrayList<>(6);
metrics.add(qInit);
metrics.add(q000);
metrics.add(q025);
@ -455,7 +455,7 @@ abstract class PoolArena<T> implements PoolArenaMetric {
}
private static List<PoolSubpageMetric> subPageMetricList(PoolSubpage<?>[] pages) {
List<PoolSubpageMetric> metrics = new ArrayList<PoolSubpageMetric>();
List<PoolSubpageMetric> metrics = new ArrayList<>();
for (PoolSubpage<?> head : pages) {
if (head.next == head) {
continue;
@ -478,17 +478,17 @@ abstract class PoolArena<T> 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<T> implements PoolArenaMetric {
synchronized (this) {
deallocs = deallocationsTiny + deallocationsSmall + deallocationsNormal;
}
return deallocs + deallocationsHuge.value();
return deallocs + deallocationsHuge.longValue();
}
@Override
@ -522,18 +522,18 @@ abstract class PoolArena<T> 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<T> 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<T> implements PoolArenaMetric {
@Override
protected PoolChunk<byte[]> newChunk(int pageSize, int maxOrder, int pageShifts, int chunkSize) {
return new PoolChunk<byte[]>(this, newByteArray(chunkSize), pageSize, maxOrder, pageShifts, chunkSize, 0);
return new PoolChunk<>(this, newByteArray(chunkSize), pageSize, maxOrder, pageShifts, chunkSize, 0);
}
@Override
protected PoolChunk<byte[]> newUnpooledChunk(int capacity) {
return new PoolChunk<byte[]>(this, newByteArray(capacity), capacity, 0);
return new PoolChunk<>(this, newByteArray(capacity), capacity, 0);
}
@Override
@ -741,13 +741,13 @@ abstract class PoolArena<T> implements PoolArenaMetric {
protected PoolChunk<ByteBuffer> newChunk(int pageSize, int maxOrder,
int pageShifts, int chunkSize) {
if (directMemoryCacheAlignment == 0) {
return new PoolChunk<ByteBuffer>(this,
return new PoolChunk<>(this,
allocateDirect(chunkSize), pageSize, maxOrder,
pageShifts, chunkSize, 0);
}
final ByteBuffer memory = allocateDirect(chunkSize
+ directMemoryCacheAlignment);
return new PoolChunk<ByteBuffer>(this, memory, pageSize,
return new PoolChunk<>(this, memory, pageSize,
maxOrder, pageShifts, chunkSize,
offsetCacheLine(memory));
}
@ -755,12 +755,12 @@ abstract class PoolArena<T> implements PoolArenaMetric {
@Override
protected PoolChunk<ByteBuffer> newUnpooledChunk(int capacity) {
if (directMemoryCacheAlignment == 0) {
return new PoolChunk<ByteBuffer>(this,
return new PoolChunk<>(this,
allocateDirect(capacity), capacity, 0);
}
final ByteBuffer memory = allocateDirect(capacity
+ directMemoryCacheAlignment);
return new PoolChunk<ByteBuffer>(this, memory, capacity,
return new PoolChunk<>(this, memory, capacity,
offsetCacheLine(memory));
}

View File

@ -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

View File

@ -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();
}
}

View File

@ -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();
}

View File

@ -328,7 +328,7 @@ public final class PlatformDependent {
if (hasUnsafe()) {
PlatformDependent0.throwException(t);
} else {
PlatformDependent.<RuntimeException>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 <K, V> ConcurrentMap<K, V> newConcurrentHashMap() {
return new ConcurrentHashMap<K, V>();
}
/**
* 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 <K, V> ConcurrentMap<K, V> newConcurrentHashMap(int initialCapacity) {
return new ConcurrentHashMap<K, V>(initialCapacity);
return new ConcurrentHashMap<>(initialCapacity);
}
/**
* Creates a new fastest {@link ConcurrentMap} implementation for the current platform.
*/
public static <K, V> ConcurrentMap<K, V> newConcurrentHashMap(int initialCapacity, float loadFactor) {
return new ConcurrentHashMap<K, V>(initialCapacity, loadFactor);
return new ConcurrentHashMap<>(initialCapacity, loadFactor);
}
/**
@ -374,14 +363,14 @@ public final class PlatformDependent {
*/
public static <K, V> ConcurrentMap<K, V> newConcurrentHashMap(
int initialCapacity, float loadFactor, int concurrencyLevel) {
return new ConcurrentHashMap<K, V>(initialCapacity, loadFactor, concurrencyLevel);
return new ConcurrentHashMap<>(initialCapacity, loadFactor, concurrencyLevel);
}
/**
* Creates a new fastest {@link ConcurrentMap} implementation for the current platform.
*/
public static <K, V> ConcurrentMap<K, V> newConcurrentHashMap(Map<? extends K, ? extends V> map) {
return new ConcurrentHashMap<K, V>(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<T>(MPSC_CHUNK_SIZE, capacity)
: new MpscGrowableAtomicArrayQueue<T>(MPSC_CHUNK_SIZE, capacity);
return USE_MPSC_CHUNKED_ARRAY_QUEUE ? new MpscChunkedArrayQueue<>(MPSC_CHUNK_SIZE, capacity)
: new MpscGrowableAtomicArrayQueue<>(MPSC_CHUNK_SIZE, capacity);
}
static <T> Queue<T> newMpscQueue() {
return USE_MPSC_CHUNKED_ARRAY_QUEUE ? new MpscUnboundedArrayQueue<T>(MPSC_CHUNK_SIZE)
: new MpscUnboundedAtomicArrayQueue<T>(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 <T> Queue<T> newSpscQueue() {
return hasUnsafe() ? new SpscLinkedQueue<T>() : new SpscLinkedAtomicQueue<T>();
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 <T> Queue<T> newFixedMpscQueue(int capacity) {
return hasUnsafe() ? new MpscArrayQueue<T>(capacity) : new MpscAtomicArrayQueue<T>(capacity);
return hasUnsafe() ? new MpscArrayQueue<>(capacity) : new MpscAtomicArrayQueue<>(capacity);
}
/**
@ -936,9 +925,9 @@ public final class PlatformDependent {
*/
public static <C> Deque<C> newConcurrentDeque() {
if (javaVersion() < 7) {
return new LinkedBlockingDeque<C>();
return new LinkedBlockingDeque<>();
} else {
return new ConcurrentLinkedDeque<C>();
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();
}