ronantakizawa/codereview-bench huggingface dataset:
Repository: netty/netty
File: codec-compression/src/test/java/io/netty/handler/codec/compression/ZstdEncoderTest.java (Java)

Code before:
        assertTrue(channel.finish());

        ByteBuf decompressed = readDecompressed(dataLength);
        assertEquals(data, decompressed);

        decompressed.release();
        data.release();
    }

    @ParameterizedTest
    @MethodSource("smallData")
    public void testCompressionOfSmallBatchedFlow(final ByteBuf data) throws Exception {
        testCompressionOfBatchedFlow(data);
    }

    @Test
    public void testCompressionOfTinyData() throws Exception {
        ByteBuf data = Unpooled.copiedBuffer(TEST_CONTENT, CharsetUtil.UTF_8);
        assertTrue(channel.writeOutbound(data.retain()));
        assertTrue(channel.finish());

        ByteBuf out = channel.readOutbound();
        assertThat(out.readableBytes()).isPositive();

        out.release();
        data.release();
    }

    @Override
    protected ByteBuf decompress(ByteBuf compressed, int originalLength) throws Exception {
        byte[] decompressed = new byte[originalLength];
        try (ZstdInputStream zstdIs = new ZstdInputStream(new ByteBufInputStream(compressed, true))) {

            int remaining = originalLength;
            while (remaining > 0) {
                int read = zstdIs.read(decompressed, originalLength - remaining, remaining);
                if (read > 0) {
                    remaining -= read;
                } else {
                    break;
                }
            }
            assertEquals(-1, zstdIs.read());
        }

        return Unpooled.wrappedBuffer(decompressed);
    }
}


Review comment (suggestion):
```suggestion
        asssertNull(out.readOutbound());
```

Code after:
        decompressed.release();
        data.release();
    }

    @ParameterizedTest
    @MethodSource("smallData")
    public void testCompressionOfSmallBatchedFlow(final ByteBuf data) throws Exception {
        testCompressionOfBatchedFlow(data);
    }

    @Test
    public void testCompressionOfTinyData() throws Exception {
        ByteBuf data = Unpooled.copiedBuffer("Hello, World", CharsetUtil.UTF_8);
        assertTrue(channel.writeOutbound(data));
        assertTrue(channel.finish());

        ByteBuf out = channel.readOutbound();
        assertThat(out.readableBytes()).isPositive();
        out.release();
        assertNull(channel.readOutbound());
    }

    @Override
    protected ByteBuf decompress(ByteBuf compressed, int originalLength) throws Exception {
        byte[] decompressed = new byte[originalLength];
        try (ZstdInputStream zstdIs = new ZstdInputStream(new ByteBufInputStream(compressed, true))) {

            int remaining = originalLength;
            while (remaining > 0) {
                int read = zstdIs.read(decompressed, originalLength - remaining, remaining);
                if (read > 0) {
                    remaining -= read;
                } else {
                    break;
                }
            }
            assertEquals(-1, zstdIs.read());
        }

        return Unpooled.wrappedBuffer(decompressed);
    }
}
Repository: netty/netty
File: codec-compression/src/main/java/io/netty/handler/codec/compression/ZstdConstants.java (Java)

Code before:
 */
package io.netty.handler.codec.compression;

import com.github.luben.zstd.Zstd;

final class ZstdConstants {

    /**
     * Default compression level
     */
    static final int DEFAULT_COMPRESSION_LEVEL = Zstd.defaultCompressionLevel();

    /**
     * Min compression level
     */
    static final int MIN_COMPRESSION_LEVEL = Zstd.minCompressionLevel();

    /**
     * Max compression level
     */
    static final int MAX_COMPRESSION_LEVEL = Zstd.maxCompressionLevel();

    /**
     * Max block size
     */
    static final int MAX_BLOCK_SIZE = Integer.MAX_VALUE;
    /**
     * Default block size
     */
    static final int DEFAULT_BLOCK_SIZE = 1 << 16;  // 64 KB

    private ZstdConstants() { }
}


Review comment (suggestion):
I have an issue interpreting the RFC.

https://datatracker.ietf.org/doc/html/rfc8478#section-3.1.1.2.3

I read:

>    The upper 21 bits of Block_Header represent the Block_Size.
   Block_Size is the size of the block excluding the header.  A block
   can contain any number of bytes (even zero), up to
   Block_Maximum_Decompressed_Size, which is the smallest of:
> -  Window_Size
> -  128 KB

Does it mean a blockSize has to be lower than 128KB ? It looks fairly low to me, and it may be unrelated to this PR change (I'm no expert in Zstd).

I mean, do we have a unitTest actually testing a large blockSize? (again blockSize != decodeSize, and I understand decodeSize (as worded in Netty) refers to windowSize (as referred in the RFC)).

Code after:
 */
package io.netty.handler.codec.compression;

import com.github.luben.zstd.Zstd;

final class ZstdConstants {

    /**
     * Default compression level
     */
    static final int DEFAULT_COMPRESSION_LEVEL = Zstd.defaultCompressionLevel();

    /**
     * Min compression level
     */
    static final int MIN_COMPRESSION_LEVEL = Zstd.minCompressionLevel();

    /**
     * Max compression level
     */
    static final int MAX_COMPRESSION_LEVEL = Zstd.maxCompressionLevel();

    /**
     * Max encode size
     */
    static final int DEFAULT_MAX_ENCODE_SIZE = Integer.MAX_VALUE;
    /**
     * Default block size
     */
    static final int DEFAULT_BLOCK_SIZE = 1 << 16;  // 64 KB

    private ZstdConstants() { }
}
Repository: netty/netty
File: buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java (Java)

Code before:
            int newArrSize = Math.max(size + (size >> 1), newSize);
            Component[] newArr;
            if (i == size) {
                newArr = Arrays.copyOf(components, newArrSize, Component[].class);
            } else {
                newArr = new Component[newArrSize];
                if (i > 0) {
                    System.arraycopy(components, 0, newArr, 0, i);
                }
                if (i < size) {
                    System.arraycopy(components, i, newArr, i + count, size - i);
                }
            }
            components = newArr;
        } else if (i < size) {
            System.arraycopy(components, i, components, i + count, size - i);
        }
        componentCount = newSize;
    }

    /**
     * Decreases the reference count by the specified {@code decrement} and deallocates this object if the reference
     * count reaches at {@code 0}.
     * <p>
     * This implementation only decreases the reference count of each internal component by 1,
     * regardless of the {@code decrement} value.
     *
     * @param decrement the number by which the reference count should be decreased
     * @return {@code true} if and only if the reference count became {@code 0} and this object has been deallocated
     */
    @Override
    public boolean release(final int decrement) {
        return super.release(decrement);
    }
}


Review comment (suggestion):
```suggestion
     * Decreases the reference count by the specified {@code decrement} and deallocates this object if the reference
     * count reaches at {@code 0}. At this point it will also decrement the reference count of each internal component by 1.
```

Code after:
            int newArrSize = Math.max(size + (size >> 1), newSize);
            Component[] newArr;
            if (i == size) {
                newArr = Arrays.copyOf(components, newArrSize, Component[].class);
            } else {
                newArr = new Component[newArrSize];
                if (i > 0) {
                    System.arraycopy(components, 0, newArr, 0, i);
                }
                if (i < size) {
                    System.arraycopy(components, i, newArr, i + count, size - i);
                }
            }
            components = newArr;
        } else if (i < size) {
            System.arraycopy(components, i, components, i + count, size - i);
        }
        componentCount = newSize;
    }

    /**
     * Decreases the reference count by the specified {@code decrement} and deallocates this object if the reference
     * count reaches at {@code 0}. At this point it will also decrement the reference count of each internal
     * component by {@code 1}.
     *
     * @param decrement the number by which the reference count should be decreased
     * @return {@code true} if and only if the reference count became {@code 0} and this object has been deallocated
     */
    @Override
    public boolean release(final int decrement) {
        return super.release(decrement);
    }
}
Repository: netty/netty
File: buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java (Java)

Code before:
        assert i >= 0 && i <= size && count > 0;
        if (newSize > components.length) {
            // grow the array
            int newArrSize = Math.max(size + (size >> 1), newSize);
            Component[] newArr;
            if (i == size) {
                newArr = Arrays.copyOf(components, newArrSize, Component[].class);
            } else {
                newArr = new Component[newArrSize];
                if (i > 0) {
                    System.arraycopy(components, 0, newArr, 0, i);
                }
                if (i < size) {
                    System.arraycopy(components, i, newArr, i + count, size - i);
                }
            }
            components = newArr;
        } else if (i < size) {
            System.arraycopy(components, i, components, i + count, size - i);
        }
        componentCount = newSize;
    }

    /**
     * Decreases the reference count by the specified {@code decrement} and deallocates this object if the reference
     * count reaches at {@code 0}. At this point it will also decrement the reference count of each internal 
     * component by {@code 1}.
     *
     * @param decrement the number by which the reference count should be decreased
     * @return {@code true} if and only if the reference count became {@code 0} and this object has been deallocated
     */
    @Override
    public boolean release(final int decrement) {
        return super.release(decrement);
    }
}


Review comment (suggestion):
```suggestion
     * count reaches at {@code 0}. At this point it will also decrement the reference count of each internal
```

Code after:
        assert i >= 0 && i <= size && count > 0;
        if (newSize > components.length) {
            // grow the array
            int newArrSize = Math.max(size + (size >> 1), newSize);
            Component[] newArr;
            if (i == size) {
                newArr = Arrays.copyOf(components, newArrSize, Component[].class);
            } else {
                newArr = new Component[newArrSize];
                if (i > 0) {
                    System.arraycopy(components, 0, newArr, 0, i);
                }
                if (i < size) {
                    System.arraycopy(components, i, newArr, i + count, size - i);
                }
            }
            components = newArr;
        } else if (i < size) {
            System.arraycopy(components, i, components, i + count, size - i);
        }
        componentCount = newSize;
    }

    /**
     * Decreases the reference count by the specified {@code decrement} and deallocates this object if the reference
     * count reaches at {@code 0}. At this point it will also decrement the reference count of each internal
     * component by {@code 1}.
     *
     * @param decrement the number by which the reference count should be decreased
     * @return {@code true} if and only if the reference count became {@code 0} and this object has been deallocated
     */
    @Override
    public boolean release(final int decrement) {
        return super.release(decrement);
    }
}
Repository: netty/netty
File: codec-http3/src/main/java/io/netty/handler/codec/http3/Http3SettingIdentifier.java (Java)

Code before:

    private final long id;

    Http3SettingIdentifier(long id) {
        this.id = id;
    }

    /**
     * Returns the Identifier of {@link Http3SettingIdentifier}
     * for example:
     * SETTINGS_QPACK_MAX_TABLE_CAPACITY = 0x1 = 1 in the settings frame
     * <br>
     * @return long(represented as hexadecimal above) value of the Identifier
     */
    public long id() {
        return id;
    }

    /**
     * Returns {@link Http3SettingIdentifier}
     * @param id
     * @return {@link Http3SettingIdentifier} enum which represents @param id, null otherwise
     */
    @Nullable
    public static Http3SettingIdentifier fromId(long id) {
        for (Http3SettingIdentifier s : values()) {
            if (s.id == id) {
                return s;
            }
        }
        return null; // unknown setting → ignored when receiving
    }
}


Review comment (performance):
`values()` clones the array, it would be good here to use a cache value of the array returned by values

Code after:
     * RFC 9220, Section 5 (IANA Considerations)</a> and registered in
     * the <a href="https://www.iana.org/assignments/http3-parameters/http3-parameters.xhtml#settings">
     * HTTP/3 SETTINGS registry (IANA)</a>.
     * <br>
     * Enables use of the CONNECT protocol in HTTP/3 when set to 1; disabled when 0.
     */
    HTTP3_SETTINGS_H3_DATAGRAM(0x33);

    private final long id;

    private static final Map<Long, Http3SettingIdentifier> LOOKUP = Collections.unmodifiableMap(
        Arrays.stream(values())
                    .collect(Collectors.toMap(
                            Http3SettingIdentifier::id,
                            Function.identity()
                    ))
    );

    Http3SettingIdentifier(long id) {
        this.id = id;
    }

    /**
     * Returns the Identifier of {@link Http3SettingIdentifier}
     * for example:
     * SETTINGS_QPACK_MAX_TABLE_CAPACITY = 0x1 = 1 in the settings frame
     * <br>
     * @return long(represented as hexadecimal above) value of the Identifier
     */
    public long id() {
        return id;
    }

    /**
     * Returns {@link Http3SettingIdentifier}
     * @param id
     * @return {@link Http3SettingIdentifier} enum which represents @param id, null otherwise
     */
    @Nullable
    public static Http3SettingIdentifier fromId(long id) {
        return LOOKUP.get(id);
    }
}
Repository: netty/netty
File: transport/src/main/java/io/netty/channel/ManualIoEventLoop.java (Java)

Code before:
    private class BlockingIoHandlerContext implements IoHandlerContext {
        // this is a positive amount of nanos or Long.MAX_VALUE for no limit
        long maxBlockingNanos = Long.MAX_VALUE;

        @Override
        public boolean canBlock() {
            assert inEventLoop();
            return !hasTasks() && !hasScheduledTasks() && ManualIoEventLoop.this.canBlock();
        }

        @Override
        public long delayNanos(long currentTimeNanos) {
            assert inEventLoop();
            return Math.min(maxBlockingNanos, ManualIoEventLoop.this.delayNanos(currentTimeNanos, maxBlockingNanos));
        }

        @Override
        public long deadlineNanos() {
            assert inEventLoop();
            long next = nextScheduledTaskDeadlineNanos();
            if (maxBlockingNanos == Long.MAX_VALUE) {
                // next == -1? -1 : next i.e. return next
                return next;
            }
            long now = ticker.nanoTime();
            // we cannot just check Math.min as nanoTime can be negative or wrap around!
            if (next == -1 || next - now > maxBlockingNanos) {
                return now + maxBlockingNanos;
            }
            return next;
        }
    }
}


Review comment (bug):
I want, similarly to the non blocking context, to return some "default" meaningful value to the `IoHandler` which would save it to setup any timed wait.
Performing the original computation would make the handler to believe there's some deadline involved, which is both a waste and actually, wrong.

Code after:
        }
    }

    private class BlockingIoHandlerContext implements IoHandlerContext {
        // this is a positive amount of nanos or Long.MAX_VALUE for no limit
        long maxBlockingNanos = Long.MAX_VALUE;

        @Override
        public boolean canBlock() {
            assert inEventLoop();
            return !hasTasks() && !hasScheduledTasks() && ManualIoEventLoop.this.canBlock();
        }

        @Override
        public long delayNanos(long currentTimeNanos) {
            assert inEventLoop();
            return Math.min(maxBlockingNanos, ManualIoEventLoop.this.delayNanos(currentTimeNanos, maxBlockingNanos));
        }

        @Override
        public long deadlineNanos() {
            assert inEventLoop();
            long next = nextScheduledTaskDeadlineNanos();
            if (maxBlockingNanos == Long.MAX_VALUE) {
                // next == -1? -1 : next i.e. return next
                return next;
            }
            long now = ticker.nanoTime();
            // we cannot just check Math.min as nanoTime can be negative or wrap around!
            if (next == -1 || next - now > maxBlockingNanos) {
                return now + maxBlockingNanos;
            }
            return next;
        }
    }
}
Repository: netty/netty
File: microbench/src/main/java/io/netty/microbench/search/UnalignedIndexOfBenchmark.java (Java)

Code before:
/*
 * Copyright 2025 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.microbench.search;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.microbench.util.AbstractMicrobenchmark;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;

Review comment (suggestion):
```suggestion
 * Copyright 2026 The Netty Project
```

Code after:
/*
 * Copyright 2026 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.microbench.search;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.microbench.util.AbstractMicrobenchmark;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
Repository: netty/netty
File: codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java (Java)

Code before:
            if (hasPseudoHeaderFormat(key)) {
                after = firstNonPseudo;
                before = firstNonPseudo.before();
            } else {
                after = head;
                before = head.before();
                if (firstNonPseudo == head) {
                    firstNonPseudo = this;
                }
            }
            pointNeighborsToThis();
        }

        @Override
        protected void remove() {
            if (this == firstNonPseudo) {
                firstNonPseudo = firstNonPseudo.after();
            }
            super.remove();
        }
    }

    /**
     * Default {@link io.netty.handler.codec.DefaultHeaders.NameValidator} used for HTTP/2.
     */
    static NameValidator<CharSequence> defaultHtt2NameValidator() {
        return HTTP2_NAME_VALIDATOR;
    }

    /**
     * Default {@link io.netty.handler.codec.DefaultHeaders.ValueValidator} used for HTTP/2.
     */
    static ValueValidator<CharSequence> defaultHttp2ValueValidator() {
        return VALUE_VALIDATOR;
    }
}


Review comment (suggestion):
```suggestion
    public static NameValidator<CharSequence> defaultHtt2NameValidator() {
```

Code after:
            if (hasPseudoHeaderFormat(key)) {
                after = firstNonPseudo;
                before = firstNonPseudo.before();
            } else {
                after = head;
                before = head.before();
                if (firstNonPseudo == head) {
                    firstNonPseudo = this;
                }
            }
            pointNeighborsToThis();
        }

        @Override
        protected void remove() {
            if (this == firstNonPseudo) {
                firstNonPseudo = firstNonPseudo.after();
            }
            super.remove();
        }
    }

    /**
     * Default {@link io.netty.handler.codec.DefaultHeaders.NameValidator} used for HTTP/2.
     */
    public static NameValidator<CharSequence> defaultHtt2NameValidator() {
        return HTTP2_NAME_VALIDATOR;
    }

    /**
     * Default {@link io.netty.handler.codec.DefaultHeaders.ValueValidator} used for HTTP/2.
     */
    public static ValueValidator<CharSequence> defaultHttp2ValueValidator() {
        return VALUE_VALIDATOR;
    }
}
Repository: netty/netty
File: codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java (Java)

Code before:
                    firstNonPseudo = this;
                }
            }
            pointNeighborsToThis();
        }

        @Override
        protected void remove() {
            if (this == firstNonPseudo) {
                firstNonPseudo = firstNonPseudo.after();
            }
            super.remove();
        }
    }

    /**
     * Default {@link io.netty.handler.codec.DefaultHeaders.NameValidator} used for HTTP/2.
     */
    static NameValidator<CharSequence> defaultHtt2NameValidator() {
        return HTTP2_NAME_VALIDATOR;
    }

    /**
     * Default {@link io.netty.handler.codec.DefaultHeaders.ValueValidator} used for HTTP/2.
     */
    static ValueValidator<CharSequence> defaultHttp2ValueValidator() {
        return VALUE_VALIDATOR;
    }
}


Review comment (suggestion):
```suggestion
    public static ValueValidator<CharSequence> defaultHttp2ValueValidator() {
```

Code after:
                    firstNonPseudo = this;
                }
            }
            pointNeighborsToThis();
        }

        @Override
        protected void remove() {
            if (this == firstNonPseudo) {
                firstNonPseudo = firstNonPseudo.after();
            }
            super.remove();
        }
    }

    /**
     * Default {@link io.netty.handler.codec.DefaultHeaders.NameValidator} used for HTTP/2.
     */
    public static NameValidator<CharSequence> defaultHtt2NameValidator() {
        return HTTP2_NAME_VALIDATOR;
    }

    /**
     * Default {@link io.netty.handler.codec.DefaultHeaders.ValueValidator} used for HTTP/2.
     */
    public static ValueValidator<CharSequence> defaultHttp2ValueValidator() {
        return VALUE_VALIDATOR;
    }
}