Repository: netty/netty
File: common/src/main/java/io/netty/util/concurrent/FastThreadLocalThread.java (Java)

Code before:

    private static boolean isFastThreadLocalVirtualThread() {
        long[] arr = fallbackThreads.get();
        if (arr == null) {
            return false;
        }
        return Arrays.binarySearch(arr, Thread.currentThread().getId()) >= 0;
    }

    /**
     * Run the given task with {@link FastThreadLocal} support. This call should wrap the runnable for any thread that
     * is long-running enough to make treating it as a {@link FastThreadLocalThread} reasonable, but that can't
     * actually extend this class (e.g. because it's a virtual thread). Netty will use optimizations for recyclers and
     * allocators as if this was a {@link FastThreadLocalThread}.
     * <p>This method will clean up any {@link FastThreadLocal}s at the end, and
     * {@link #currentThreadWillCleanupFastThreadLocals()} will return {@code true}.
     * <p>At the moment, {@link FastThreadLocal} uses normal {@link ThreadLocal} as the backing storage here, but in
     * the future this may be replaced with scoped values, if semantics can be preserved and performance is good.
     *
     * @param runnable The task to run
     */
    public static void runWithFastThreadLocal(Runnable runnable) {
        long id = currentThread().getId();
        if (currentThread() instanceof FastThreadLocalThread) {
            throw new IllegalStateException("Caller is a real FastThreadLocalThread");
        }
        fallbackThreads.updateAndGet(arr -> {
            if (arr == null) {
                return new long[] { id };
            }
            int index = Arrays.binarySearch(arr, id);
            if (index >= 0) {
                throw new IllegalStateException("Reentrant call to run()");
            }
            index = ~index;
            long[] next = new long[arr.length + 1];
            System.arraycopy(arr, 0, next, 0, index);
            next[index] = id;
            System.arraycopy(arr, index, next, index + 1, arr.length - index);
            return next;
        });
        try {
            runnable.run();
        } finally {
            fallbackThreads.getAndUpdate(arr -> {
                if (arr == null) {
                    return null;
                }
                if (arr.length == 1 && arr[0] == id) {
                    return null;
                }

Review comment (suggestion):
```suggestion
        Thread current = currentThread();
        if (current instanceof FastThreadLocalThread) {
            throw new IllegalStateException("Caller is a real FastThreadLocalThread");
        }
        long id = current.getId();
```

Code after:
        long[] arr = fallbackThreads.get();
        if (arr == null) {
            return false;
        }
        return Arrays.binarySearch(arr, Thread.currentThread().getId()) >= 0;
    }

    /**
     * Run the given task with {@link FastThreadLocal} support. This call should wrap the runnable for any thread that
     * is long-running enough to make treating it as a {@link FastThreadLocalThread} reasonable, but that can't
     * actually extend this class (e.g. because it's a virtual thread). Netty will use optimizations for recyclers and
     * allocators as if this was a {@link FastThreadLocalThread}.
     * <p>This method will clean up any {@link FastThreadLocal}s at the end, and
     * {@link #currentThreadWillCleanupFastThreadLocals()} will return {@code true}.
     * <p>At the moment, {@link FastThreadLocal} uses normal {@link ThreadLocal} as the backing storage here, but in
     * the future this may be replaced with scoped values, if semantics can be preserved and performance is good.
     *
     * @param runnable The task to run
     */
    public static void runWithFastThreadLocal(Runnable runnable) {
        Thread current = currentThread();
        if (current instanceof FastThreadLocalThread) {
            throw new IllegalStateException("Caller is a real FastThreadLocalThread");
        }
        long id = current.getId();
        fallbackThreads.updateAndGet(arr -> {
            if (arr == null) {
                return new long[] { id };
            }
            int index = Arrays.binarySearch(arr, id);
            if (index >= 0) {
                throw new IllegalStateException("Reentrant call to run()");
            }
            index = ~index; // same as -(index + 1)
            long[] next = new long[arr.length + 1];
            System.arraycopy(arr, 0, next, 0, index);
            next[index] = id;
            System.arraycopy(arr, index, next, index + 1, arr.length - index);
            return next;
        });
        try {
            runnable.run();
        } finally {
            fallbackThreads.getAndUpdate(arr -> {
                if (arr == null || (arr.length == 1 && arr[0] == id)) {
                    return null;
                }
                int index = Arrays.binarySearch(arr, id);
                if (index < 0) {
                    return arr;
                }