FastThreadLocal#set remove duplicate isIndexedVariableSet call
Motivation: FastThreadLocal#set calls isIndexedVariableSet to determine if we need to register with the cleaner, but the set(InternalThreadLocalMap, V) method will also internally do this check so we can share code and only do the check a single time. Modifications: - extract code from set(InternalThreadLocalMap, V) so it can be called externally to determine if a new item was created Result: Less code duplication in FastThreadLocal#set.
This commit is contained in:
parent
09484de769
commit
f8a0eea765
@ -16,8 +16,8 @@
|
||||
package io.netty.util.concurrent;
|
||||
|
||||
import io.netty.util.internal.InternalThreadLocalMap;
|
||||
import io.netty.util.internal.PlatformDependent;
|
||||
import io.netty.util.internal.ObjectCleaner;
|
||||
import io.netty.util.internal.PlatformDependent;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.IdentityHashMap;
|
||||
@ -195,10 +195,7 @@ public class FastThreadLocal<V> {
|
||||
public final void set(V value) {
|
||||
if (value != InternalThreadLocalMap.UNSET) {
|
||||
InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.get();
|
||||
boolean alreadySet = threadLocalMap.isIndexedVariableSet(index);
|
||||
set(threadLocalMap, value);
|
||||
|
||||
if (!alreadySet) {
|
||||
if (setKnownNotUnset(threadLocalMap, value)) {
|
||||
registerCleaner(threadLocalMap);
|
||||
}
|
||||
} else {
|
||||
@ -211,14 +208,23 @@ public class FastThreadLocal<V> {
|
||||
*/
|
||||
public final void set(InternalThreadLocalMap threadLocalMap, V value) {
|
||||
if (value != InternalThreadLocalMap.UNSET) {
|
||||
if (threadLocalMap.setIndexedVariable(index, value)) {
|
||||
addToVariablesToRemove(threadLocalMap, this);
|
||||
}
|
||||
setKnownNotUnset(threadLocalMap, value);
|
||||
} else {
|
||||
remove(threadLocalMap);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return see {@link InternalThreadLocalMap#setIndexedVariable(int, Object)}.
|
||||
*/
|
||||
private boolean setKnownNotUnset(InternalThreadLocalMap threadLocalMap, V value) {
|
||||
if (threadLocalMap.setIndexedVariable(index, value)) {
|
||||
addToVariablesToRemove(threadLocalMap, this);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if this thread-local variable is set.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user