use of io.netty.util.concurrent.FastThreadLocalThread in project turbo-rpc by hank-whu.
the class AttachmentThreadUtils method getOrUpdate.
/**
* @param index
* 通过 {@link #nextVarIndex()} 获得
*
* @param producer
* 尽量使用final常量以获得更好的性能
*
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T getOrUpdate(int index, Supplier<T> producer) {
Thread currentThread = Thread.currentThread();
if (currentThread instanceof FastThreadLocalThread) {
// 很快,1.5x ThreadLocal性能
FastThreadLocalThread fastThread = (FastThreadLocalThread) currentThread;
InternalThreadLocalMap threadLocalMap = fastThread.threadLocalMap();
if (threadLocalMap == null) {
// 会自动赋值的
threadLocalMap = InternalThreadLocalMap.get();
}
Object obj = threadLocalMap.indexedVariable(index);
if (obj != InternalThreadLocalMap.UNSET) {
return (T) obj;
}
obj = producer.get();
threadLocalMap.setIndexedVariable(index, obj);
return (T) obj;
}
if (currentThread instanceof AttachmentThread) {
// 很快,1.5x ThreadLocal性能
AttachmentThread attachmentThread = (AttachmentThread) currentThread;
T result = attachmentThread.get(index);
if (result != null) {
return result;
} else {
return attachmentThread.getOrUpdate(index, producer);
}
}
long currentThreadId = currentThread.getId();
if (currentThreadId < 1024L * 16L) {
// 跟直接使用ThreadLocal相同的性能
IntToObjectArrayMap<Object> varMap = threadAttachmentMap.get((int) currentThreadId);
if (varMap == null) {
varMap = // 会自动赋值的
threadAttachmentMap.getOrUpdate((int) currentThreadId, () -> new IntToObjectArrayMap<>());
}
Object obj = varMap.get(index);
if (obj != null) {
return (T) obj;
} else {
return (T) varMap.getOrUpdate(index, (Supplier<Object>) producer);
}
}
{
// 很慢,0.7x ThreadLocal性能
IntToObjectArrayMap<Object> varMap = SLOW_THREAD_LOCAL_HOLDER.get();
Object obj = varMap.get(index);
if (obj != null) {
return (T) obj;
} else {
return (T) varMap.getOrUpdate(index, (Supplier<Object>) producer);
}
}
}
use of io.netty.util.concurrent.FastThreadLocalThread in project turbo-rpc by hank-whu.
the class AttachmentThreadUtils method get.
/**
* @param index
* 通过 {@link #nextVarIndex()} 获得
*
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T get(int index) {
Thread currentThread = Thread.currentThread();
if (currentThread instanceof FastThreadLocalThread) {
// 很快,1.5x ThreadLocal性能
FastThreadLocalThread fastThread = (FastThreadLocalThread) currentThread;
InternalThreadLocalMap threadLocalMap = fastThread.threadLocalMap();
if (threadLocalMap == null) {
// 会自动赋值的
threadLocalMap = InternalThreadLocalMap.get();
}
Object obj = threadLocalMap.indexedVariable(index);
if (obj != InternalThreadLocalMap.UNSET) {
return (T) obj;
} else {
return null;
}
}
if (currentThread instanceof AttachmentThread) {
// 很快,1.5x ThreadLocal性能
AttachmentThread attachmentThread = (AttachmentThread) currentThread;
return attachmentThread.get(index);
}
long currentThreadId = currentThread.getId();
if (currentThreadId < 1024L * 16L) {
// 跟直接使用ThreadLocal相同的性能
IntToObjectArrayMap<Object> varMap = threadAttachmentMap.get((int) currentThreadId);
if (varMap == null) {
varMap = // 会自动赋值的
threadAttachmentMap.getOrUpdate((int) currentThreadId, () -> new IntToObjectArrayMap<>());
}
return (T) varMap.get(index);
}
{
// 很慢,0.7x ThreadLocal性能
IntToObjectArrayMap<Object> varMap = SLOW_THREAD_LOCAL_HOLDER.get();
return (T) varMap.get(index);
}
}
Aggregations