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

Code before:
    /**
     * 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;
                }
                int index = Arrays.binarySearch(arr, id);
                if (index < 0) {
                    return arr;
                }
                long[] next = new long[arr.length - 1];
                System.arraycopy(arr, 0, next, 0, index);
                System.arraycopy(arr, index + 1, next, index, arr.length - index - 1);
                return next;
            });

Review comment (suggestion):
the javadoc states it returns 
```
-(insertion point) - 1
```
so, please add a line as
```
// same as -(index + 1)
```

Code after:
     * 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;
                }
                long[] next = new long[arr.length - 1];
                System.arraycopy(arr, 0, next, 0, index);
                System.arraycopy(arr, index + 1, next, index, arr.length - index - 1);
                return next;
            });
            FastThreadLocal.removeAll();
        }
    }