Permalink
Browse files

Remove usage of own Atomic*FieldUpdater in favor of JDKs

Motivation:

In later Java8 versions our Atomic*FieldUpdater are slower then the JDK implementations so we should not use ours anymore. Even worse the JDK implementations provide for example an optimized version of addAndGet(...) using intrinsics which makes it a lot faster for this use-case.

Modifications:

- Remove methods that return our own Atomic*FieldUpdaters.
- Use the JDK implementations everywhere.

Result:

Faster code.
  • Loading branch information...
1 parent 5fec897 commit 89e93968ac601cbe3d6ca105abbefc99adcb56be @normanmaurer normanmaurer committed with normanmaurer Dec 9, 2016
Showing with 37 additions and 441 deletions.
  1. +2 −11 buffer/src/main/java/io/netty/buffer/AbstractReferenceCountedByteBuf.java
  2. +2 −12 common/src/main/java/io/netty/util/AbstractReferenceCounted.java
  3. +2 −14 common/src/main/java/io/netty/util/DefaultAttributeMap.java
  4. +4 −19 common/src/main/java/io/netty/util/HashedWheelTimer.java
  5. +2 −9 common/src/main/java/io/netty/util/concurrent/DefaultPromise.java
  6. +5 −20 common/src/main/java/io/netty/util/concurrent/SingleThreadEventExecutor.java
  7. +0 −51 common/src/main/java/io/netty/util/internal/PlatformDependent.java
  8. +0 −15 common/src/main/java/io/netty/util/internal/PlatformDependent0.java
  9. +0 −62 common/src/main/java/io/netty/util/internal/UnsafeAtomicIntegerFieldUpdater.java
  10. +0 −62 common/src/main/java/io/netty/util/internal/UnsafeAtomicLongFieldUpdater.java
  11. +0 −63 common/src/main/java/io/netty/util/internal/UnsafeAtomicReferenceFieldUpdater.java
  12. +2 −8 handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java
  13. +2 −14 resolver-dns/src/main/java/io/netty/resolver/dns/RotationalDnsServerAddresses.java
  14. +2 −10 transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoop.java
  15. +2 −10 transport-native-epoll/src/main/java/io/netty/channel/unix/FileDescriptor.java
  16. +2 −12 transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java
  17. +4 −18 transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java
  18. +4 −19 transport/src/main/java/io/netty/channel/DefaultChannelConfig.java
  19. +2 −12 transport/src/main/java/io/netty/channel/local/LocalChannel.java
@@ -17,7 +17,6 @@
package io.netty.buffer;
import io.netty.util.IllegalReferenceCountException;
-import io.netty.util.internal.PlatformDependent;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
@@ -28,16 +27,8 @@
*/
public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
- private static final AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> refCntUpdater;
-
- static {
- AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> updater =
- PlatformDependent.newAtomicIntegerFieldUpdater(AbstractReferenceCountedByteBuf.class, "refCnt");
- if (updater == null) {
- updater = AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCountedByteBuf.class, "refCnt");
- }
- refCntUpdater = updater;
- }
+ private static final AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> refCntUpdater =
+ AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCountedByteBuf.class, "refCnt");
private volatile int refCnt = 1;
@@ -15,8 +15,6 @@
*/
package io.netty.util;
-import io.netty.util.internal.PlatformDependent;
-
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import static io.netty.util.internal.ObjectUtil.checkPositive;
@@ -26,16 +24,8 @@
*/
public abstract class AbstractReferenceCounted implements ReferenceCounted {
- private static final AtomicIntegerFieldUpdater<AbstractReferenceCounted> refCntUpdater;
-
- static {
- AtomicIntegerFieldUpdater<AbstractReferenceCounted> updater =
- PlatformDependent.newAtomicIntegerFieldUpdater(AbstractReferenceCounted.class, "refCnt");
- if (updater == null) {
- updater = AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCounted.class, "refCnt");
- }
- refCntUpdater = updater;
- }
+ private static final AtomicIntegerFieldUpdater<AbstractReferenceCounted> refCntUpdater =
+ AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCounted.class, "refCnt");
private volatile int refCnt = 1;
@@ -15,8 +15,6 @@
*/
package io.netty.util;
-import io.netty.util.internal.PlatformDependent;
-
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
@@ -28,18 +26,8 @@
public class DefaultAttributeMap implements AttributeMap {
@SuppressWarnings("rawtypes")
- private static final AtomicReferenceFieldUpdater<DefaultAttributeMap, AtomicReferenceArray> updater;
-
- static {
- @SuppressWarnings("rawtypes")
- AtomicReferenceFieldUpdater<DefaultAttributeMap, AtomicReferenceArray> referenceFieldUpdater =
- PlatformDependent.newAtomicReferenceFieldUpdater(DefaultAttributeMap.class, "attributes");
- if (referenceFieldUpdater == null) {
- referenceFieldUpdater = AtomicReferenceFieldUpdater
- .newUpdater(DefaultAttributeMap.class, AtomicReferenceArray.class, "attributes");
- }
- updater = referenceFieldUpdater;
- }
+ private static final AtomicReferenceFieldUpdater<DefaultAttributeMap, AtomicReferenceArray> updater =
+ AtomicReferenceFieldUpdater.newUpdater(DefaultAttributeMap.class, AtomicReferenceArray.class, "attributes");
private static final int BUCKET_SIZE = 4;
private static final int MASK = BUCKET_SIZE - 1;
@@ -81,15 +81,8 @@
private static final ResourceLeakDetector<HashedWheelTimer> leakDetector = ResourceLeakDetectorFactory.instance()
.newResourceLeakDetector(HashedWheelTimer.class, 1, Runtime.getRuntime().availableProcessors() * 4L);
- private static final AtomicIntegerFieldUpdater<HashedWheelTimer> WORKER_STATE_UPDATER;
- static {
- AtomicIntegerFieldUpdater<HashedWheelTimer> workerStateUpdater =
- PlatformDependent.newAtomicIntegerFieldUpdater(HashedWheelTimer.class, "workerState");
- if (workerStateUpdater == null) {
- workerStateUpdater = AtomicIntegerFieldUpdater.newUpdater(HashedWheelTimer.class, "workerState");
- }
- WORKER_STATE_UPDATER = workerStateUpdater;
- }
+ private static final AtomicIntegerFieldUpdater<HashedWheelTimer> WORKER_STATE_UPDATER =
+ AtomicIntegerFieldUpdater.newUpdater(HashedWheelTimer.class, "workerState");
private final ResourceLeakTracker<HashedWheelTimer> leak;
private final Worker worker = new Worker();
@@ -545,16 +538,8 @@ private long waitForNextTick() {
private static final int ST_INIT = 0;
private static final int ST_CANCELLED = 1;
private static final int ST_EXPIRED = 2;
- private static final AtomicIntegerFieldUpdater<HashedWheelTimeout> STATE_UPDATER;
-
- static {
- AtomicIntegerFieldUpdater<HashedWheelTimeout> updater =
- PlatformDependent.newAtomicIntegerFieldUpdater(HashedWheelTimeout.class, "state");
- if (updater == null) {
- updater = AtomicIntegerFieldUpdater.newUpdater(HashedWheelTimeout.class, "state");
- }
- STATE_UPDATER = updater;
- }
+ private static final AtomicIntegerFieldUpdater<HashedWheelTimeout> STATE_UPDATER =
+ AtomicIntegerFieldUpdater.newUpdater(HashedWheelTimeout.class, "state");
private final HashedWheelTimer timer;
private final TimerTask task;
@@ -38,20 +38,13 @@
private static final int MAX_LISTENER_STACK_DEPTH = Math.min(8,
SystemPropertyUtil.getInt("io.netty.defaultPromise.maxListenerStackDepth", 8));
@SuppressWarnings("rawtypes")
- private static final AtomicReferenceFieldUpdater<DefaultPromise, Object> RESULT_UPDATER;
+ private static final AtomicReferenceFieldUpdater<DefaultPromise, Object> RESULT_UPDATER =
+ AtomicReferenceFieldUpdater.newUpdater(DefaultPromise.class, Object.class, "result");
private static final Signal SUCCESS = Signal.valueOf(DefaultPromise.class, "SUCCESS");
private static final Signal UNCANCELLABLE = Signal.valueOf(DefaultPromise.class, "UNCANCELLABLE");
private static final CauseHolder CANCELLATION_CAUSE_HOLDER = new CauseHolder(ThrowableUtil.unknownStackTrace(
new CancellationException(), DefaultPromise.class, "cancel(...)"));
- static {
- @SuppressWarnings("rawtypes")
- AtomicReferenceFieldUpdater<DefaultPromise, Object> updater =
- PlatformDependent.newAtomicReferenceFieldUpdater(DefaultPromise.class, "result");
- RESULT_UPDATER = updater == null ? AtomicReferenceFieldUpdater.newUpdater(DefaultPromise.class,
- Object.class, "result") : updater;
- }
-
private volatile Object result;
private final EventExecutor executor;
/**
@@ -16,7 +16,6 @@
package io.netty.util.concurrent;
import io.netty.util.internal.ObjectUtil;
-import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.UnstableApi;
import io.netty.util.internal.logging.InternalLogger;
@@ -73,25 +72,11 @@ public void run() {
}
};
- private static final AtomicIntegerFieldUpdater<SingleThreadEventExecutor> STATE_UPDATER;
- private static final AtomicReferenceFieldUpdater<SingleThreadEventExecutor, ThreadProperties> PROPERTIES_UPDATER;
-
- static {
- AtomicIntegerFieldUpdater<SingleThreadEventExecutor> updater =
- PlatformDependent.newAtomicIntegerFieldUpdater(SingleThreadEventExecutor.class, "state");
- if (updater == null) {
- updater = AtomicIntegerFieldUpdater.newUpdater(SingleThreadEventExecutor.class, "state");
- }
- STATE_UPDATER = updater;
-
- AtomicReferenceFieldUpdater<SingleThreadEventExecutor, ThreadProperties> propertiesUpdater =
- PlatformDependent.newAtomicReferenceFieldUpdater(SingleThreadEventExecutor.class, "threadProperties");
- if (propertiesUpdater == null) {
- propertiesUpdater = AtomicReferenceFieldUpdater.newUpdater(SingleThreadEventExecutor.class,
- ThreadProperties.class, "threadProperties");
- }
- PROPERTIES_UPDATER = propertiesUpdater;
- }
+ private static final AtomicIntegerFieldUpdater<SingleThreadEventExecutor> STATE_UPDATER =
+ AtomicIntegerFieldUpdater.newUpdater(SingleThreadEventExecutor.class, "state");
+ private static final AtomicReferenceFieldUpdater<SingleThreadEventExecutor, ThreadProperties> PROPERTIES_UPDATER =
+ AtomicReferenceFieldUpdater.newUpdater(
+ SingleThreadEventExecutor.class, ThreadProperties.class, "threadProperties");
private final Queue<Runnable> taskQueue;
@@ -798,57 +798,6 @@ public static int hashCodeAscii(CharSequence bytes) {
}
}
- /**
- * Create a new optimized {@link AtomicReferenceFieldUpdater} or {@code null} if it
- * could not be created. Because of this the caller need to check for {@code null} and if {@code null} is returned
- * use {@link AtomicReferenceFieldUpdater#newUpdater(Class, Class, String)} as fallback.
- */
- public static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater(
- Class<? super U> tclass, String fieldName) {
- if (hasUnsafe()) {
- try {
- return PlatformDependent0.newAtomicReferenceFieldUpdater(tclass, fieldName);
- } catch (Throwable ignore) {
- // ignore
- }
- }
- return null;
- }
-
- /**
- * Create a new optimized {@link AtomicIntegerFieldUpdater} or {@code null} if it
- * could not be created. Because of this the caller need to check for {@code null} and if {@code null} is returned
- * use {@link AtomicIntegerFieldUpdater#newUpdater(Class, String)} as fallback.
- */
- public static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater(
- Class<? super T> tclass, String fieldName) {
- if (hasUnsafe()) {
- try {
- return PlatformDependent0.newAtomicIntegerFieldUpdater(tclass, fieldName);
- } catch (Throwable ignore) {
- // ignore
- }
- }
- return null;
- }
-
- /**
- * Create a new optimized {@link AtomicLongFieldUpdater} or {@code null} if it
- * could not be created. Because of this the caller need to check for {@code null} and if {@code null} is returned
- * use {@link AtomicLongFieldUpdater#newUpdater(Class, String)} as fallback.
- */
- public static <T> AtomicLongFieldUpdater<T> newAtomicLongFieldUpdater(
- Class<? super T> tclass, String fieldName) {
- if (hasUnsafe()) {
- try {
- return PlatformDependent0.newAtomicLongFieldUpdater(tclass, fieldName);
- } catch (Throwable ignore) {
- // ignore
- }
- }
- return null;
- }
-
private static final class Mpsc {
private static final boolean USE_MPSC_CHUNKED_ARRAY_QUEUE;
@@ -559,21 +559,6 @@ static int hashCodeAsciiSanitize(byte value) {
return value & 0x1f;
}
- static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater(
- Class<? super U> tclass, String fieldName) throws Exception {
- return new UnsafeAtomicReferenceFieldUpdater<U, W>(UNSAFE, tclass, fieldName);
- }
-
- static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater(
- Class<? super T> tclass, String fieldName) throws Exception {
- return new UnsafeAtomicIntegerFieldUpdater<T>(UNSAFE, tclass, fieldName);
- }
-
- static <T> AtomicLongFieldUpdater<T> newAtomicLongFieldUpdater(
- Class<? super T> tclass, String fieldName) throws Exception {
- return new UnsafeAtomicLongFieldUpdater<T>(UNSAFE, tclass, fieldName);
- }
-
static ClassLoader getClassLoader(final Class<?> clazz) {
if (System.getSecurityManager() == null) {
return clazz.getClassLoader();
@@ -1,62 +0,0 @@
-/*
- * Copyright 2014 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 sun.misc.Unsafe;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
-
-final class UnsafeAtomicIntegerFieldUpdater<T> extends AtomicIntegerFieldUpdater<T> {
- private final long offset;
- private final Unsafe unsafe;
-
- UnsafeAtomicIntegerFieldUpdater(Unsafe unsafe, Class<? super T> tClass, String fieldName)
- throws NoSuchFieldException {
- Field field = tClass.getDeclaredField(fieldName);
- if (!Modifier.isVolatile(field.getModifiers())) {
- throw new IllegalArgumentException("Must be volatile");
- }
- this.unsafe = unsafe;
- offset = unsafe.objectFieldOffset(field);
- }
-
- @Override
- public boolean compareAndSet(T obj, int expect, int update) {
- return unsafe.compareAndSwapInt(obj, offset, expect, update);
- }
-
- @Override
- public boolean weakCompareAndSet(T obj, int expect, int update) {
- return unsafe.compareAndSwapInt(obj, offset, expect, update);
- }
-
- @Override
- public void set(T obj, int newValue) {
- unsafe.putIntVolatile(obj, offset, newValue);
- }
-
- @Override
- public void lazySet(T obj, int newValue) {
- unsafe.putOrderedInt(obj, offset, newValue);
- }
-
- @Override
- public int get(T obj) {
- return unsafe.getIntVolatile(obj, offset);
- }
-}
@@ -1,62 +0,0 @@
-/*
- * Copyright 2014 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 sun.misc.Unsafe;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-import java.util.concurrent.atomic.AtomicLongFieldUpdater;
-
-final class UnsafeAtomicLongFieldUpdater<T> extends AtomicLongFieldUpdater<T> {
- private final long offset;
- private final Unsafe unsafe;
-
- UnsafeAtomicLongFieldUpdater(Unsafe unsafe, Class<? super T> tClass, String fieldName)
- throws NoSuchFieldException {
- Field field = tClass.getDeclaredField(fieldName);
- if (!Modifier.isVolatile(field.getModifiers())) {
- throw new IllegalArgumentException("Must be volatile");
- }
- this.unsafe = unsafe;
- offset = unsafe.objectFieldOffset(field);
- }
-
- @Override
- public boolean compareAndSet(T obj, long expect, long update) {
- return unsafe.compareAndSwapLong(obj, offset, expect, update);
- }
-
- @Override
- public boolean weakCompareAndSet(T obj, long expect, long update) {
- return unsafe.compareAndSwapLong(obj, offset, expect, update);
- }
-
- @Override
- public void set(T obj, long newValue) {
- unsafe.putLongVolatile(obj, offset, newValue);
- }
-
- @Override
- public void lazySet(T obj, long newValue) {
- unsafe.putOrderedLong(obj, offset, newValue);
- }
-
- @Override
- public long get(T obj) {
- return unsafe.getLongVolatile(obj, offset);
- }
-}
Oops, something went wrong.

0 comments on commit 89e9396

Please sign in to comment.