Skip to content

Commit f8a0eea

Browse files
committed
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.
1 parent 09484de commit f8a0eea

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

common/src/main/java/io/netty/util/concurrent/FastThreadLocal.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
package io.netty.util.concurrent;
1717

1818
import io.netty.util.internal.InternalThreadLocalMap;
19-
import io.netty.util.internal.PlatformDependent;
2019
import io.netty.util.internal.ObjectCleaner;
20+
import io.netty.util.internal.PlatformDependent;
2121

2222
import java.util.Collections;
2323
import java.util.IdentityHashMap;
@@ -195,10 +195,7 @@ private V initialize(InternalThreadLocalMap threadLocalMap) {
195195
public final void set(V value) {
196196
if (value != InternalThreadLocalMap.UNSET) {
197197
InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.get();
198-
boolean alreadySet = threadLocalMap.isIndexedVariableSet(index);
199-
set(threadLocalMap, value);
200-
201-
if (!alreadySet) {
198+
if (setKnownNotUnset(threadLocalMap, value)) {
202199
registerCleaner(threadLocalMap);
203200
}
204201
} else {
@@ -211,14 +208,23 @@ public final void set(V value) {
211208
*/
212209
public final void set(InternalThreadLocalMap threadLocalMap, V value) {
213210
if (value != InternalThreadLocalMap.UNSET) {
214-
if (threadLocalMap.setIndexedVariable(index, value)) {
215-
addToVariablesToRemove(threadLocalMap, this);
216-
}
211+
setKnownNotUnset(threadLocalMap, value);
217212
} else {
218213
remove(threadLocalMap);
219214
}
220215
}
221216

217+
/**
218+
* @return see {@link InternalThreadLocalMap#setIndexedVariable(int, Object)}.
219+
*/
220+
private boolean setKnownNotUnset(InternalThreadLocalMap threadLocalMap, V value) {
221+
if (threadLocalMap.setIndexedVariable(index, value)) {
222+
addToVariablesToRemove(threadLocalMap, this);
223+
return true;
224+
}
225+
return false;
226+
}
227+
222228
/**
223229
* Returns {@code true} if and only if this thread-local variable is set.
224230
*/

0 commit comments

Comments
 (0)