Search in sources :

Example 1 with IntToObjectArrayMap

use of rpc.turbo.util.IntToObjectArrayMap in project turbo-rpc by hank-whu.

the class FastClassResolver method readName.

protected Registration readName(Input input) {
    int nameId = input.readVarInt(true);
    if (nameIdToClass == null)
        nameIdToClass = new IntToObjectArrayMap<>();
    Class type = nameIdToClass.get(nameId);
    if (type == null) {
        // Only read the class name the first time encountered in object graph.
        String className = input.readString();
        type = getTypeByName(className);
        if (type == null) {
            try {
                type = Class.forName(className, false, kryo.getClassLoader());
            } catch (ClassNotFoundException ex) {
                if (WARN)
                    warn("kryo", "Unable to load class " + className + " with kryo's ClassLoader. Retrying with current..");
                try {
                    type = Class.forName(className);
                } catch (ClassNotFoundException e) {
                    throw new KryoException("Unable to find class: " + className, ex);
                }
            }
            if (nameToClass == null)
                nameToClass = new FastMap<>(32, 0.5F);
            nameToClass.put(className, type);
        }
        nameIdToClass.put(nameId, type);
        if (TRACE)
            trace("kryo", "Read class name: " + className);
    } else {
        if (TRACE)
            trace("kryo", "Read class name reference " + nameId + ": " + className(type));
    }
    return kryo.getRegistration(type);
}
Also used : KryoException(com.esotericsoftware.kryo.KryoException) FastMap(rpc.turbo.util.FastMap) Util.getWrapperClass(com.esotericsoftware.kryo.util.Util.getWrapperClass) IntToObjectArrayMap(rpc.turbo.util.IntToObjectArrayMap)

Example 2 with IntToObjectArrayMap

use of rpc.turbo.util.IntToObjectArrayMap in project turbo-rpc by hank-whu.

the class AttachmentThreadUtils method put.

/**
 * @param index
 *            通过 {@link #nextVarIndex()} 获得
 *
 * @param value
 */
public static <T> void put(int index, T value) {
    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();
        }
        threadLocalMap.setIndexedVariable(index, value);
        return;
    }
    if (currentThread instanceof AttachmentThread) {
        // 很快,1.5x ThreadLocal性能
        AttachmentThread attachmentThread = (AttachmentThread) currentThread;
        attachmentThread.put(index, value);
        return;
    }
    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<>());
        }
        varMap.put(index, value);
        return;
    }
    {
        // 很慢,0.7x ThreadLocal性能
        IntToObjectArrayMap<Object> varMap = SLOW_THREAD_LOCAL_HOLDER.get();
        varMap.put(index, value);
        return;
    }
}
Also used : InternalThreadLocalMap(io.netty.util.internal.InternalThreadLocalMap) FastThreadLocalThread(io.netty.util.concurrent.FastThreadLocalThread) FastThreadLocalThread(io.netty.util.concurrent.FastThreadLocalThread) IntToObjectArrayMap(rpc.turbo.util.IntToObjectArrayMap)

Example 3 with IntToObjectArrayMap

use of rpc.turbo.util.IntToObjectArrayMap 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);
        }
    }
}
Also used : InternalThreadLocalMap(io.netty.util.internal.InternalThreadLocalMap) FastThreadLocalThread(io.netty.util.concurrent.FastThreadLocalThread) Supplier(java.util.function.Supplier) FastThreadLocalThread(io.netty.util.concurrent.FastThreadLocalThread) IntToObjectArrayMap(rpc.turbo.util.IntToObjectArrayMap)

Example 4 with IntToObjectArrayMap

use of rpc.turbo.util.IntToObjectArrayMap 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);
    }
}
Also used : InternalThreadLocalMap(io.netty.util.internal.InternalThreadLocalMap) FastThreadLocalThread(io.netty.util.concurrent.FastThreadLocalThread) FastThreadLocalThread(io.netty.util.concurrent.FastThreadLocalThread) IntToObjectArrayMap(rpc.turbo.util.IntToObjectArrayMap)

Aggregations

IntToObjectArrayMap (rpc.turbo.util.IntToObjectArrayMap)4 FastThreadLocalThread (io.netty.util.concurrent.FastThreadLocalThread)3 InternalThreadLocalMap (io.netty.util.internal.InternalThreadLocalMap)3 KryoException (com.esotericsoftware.kryo.KryoException)1 Util.getWrapperClass (com.esotericsoftware.kryo.util.Util.getWrapperClass)1 Supplier (java.util.function.Supplier)1 FastMap (rpc.turbo.util.FastMap)1