Repository: netty/netty
File: buffer/src/main/java/io/netty/buffer/ByteBufUtil.java (Java)

Code before:
            }
        }
    }

    static int lastIndexOf(final AbstractByteBuf buffer, int fromIndex, final int toIndex, final byte value) {
        assert fromIndex > toIndex;
        final int capacity = buffer.capacity();
        fromIndex = Math.min(fromIndex, capacity);
        if (fromIndex <= 0) { // fromIndex is the exclusive upper bound.
            return -1;
        }
        final int length = fromIndex - toIndex;
        buffer.checkIndex(toIndex, length);
        if (!PlatformDependent.isUnaligned()) {
            return linearLastIndexOf(buffer, fromIndex, toIndex, value);
        }
        final int longCount = length >>> 3;
        int offset = fromIndex;
        if (longCount > 0) {
            final ByteOrder nativeOrder = ByteOrder.nativeOrder();
            final boolean isNative = nativeOrder == buffer.order();
            final boolean useLE = nativeOrder == ByteOrder.LITTLE_ENDIAN;
            final long pattern = SWARUtil.compilePattern(value);
            for (int i = 0; i < longCount; i++) {
                // use the faster available getLong
                final long word = useLE? buffer._getLongLE(offset - Long.BYTES)
                        : buffer._getLong(offset - Long.BYTES);
                final long result = SWARUtil.applyPattern(word, pattern);
                if (result != 0) {
                    // used the oppoiste endianness since we are looking for the last index.
                    return offset - 1 - SWARUtil.getIndex(result, !isNative);
                }
                offset -= Long.BYTES;
            }
        }
        return unrolledLastIndexOf(buffer, offset, length & 7, value);
    }

    private static int linearLastIndexOf(final AbstractByteBuf buffer, final int fromIndex, final int toIndex,
                                         final byte value) {
        for (int i = fromIndex - 1; i >= toIndex; i--) {
            if (buffer._getByte(i) == value) {
                return i;
            }
        }
        return -1;
    }

    private static int unrolledLastIndexOf(final AbstractByteBuf buffer, final int fromIndex, final int byteCount,
                                           final byte value) {
        assert byteCount >= 0 && byteCount < 8;

Review comment (suggestion):
In the if-clause, you could do an initial `Long.BYTES` subtraction to avoid this computation in the loop.

Code after:
            }
        }
    }

    static int lastIndexOf(final AbstractByteBuf buffer, int fromIndex, final int toIndex, final byte value) {
        assert fromIndex > toIndex;
        final int capacity = buffer.capacity();
        fromIndex = Math.min(fromIndex, capacity);
        if (fromIndex <= 0) { // fromIndex is the exclusive upper bound.
            return -1;
        }
        final int length = fromIndex - toIndex;
        buffer.checkIndex(toIndex, length);
        if (!PlatformDependent.isUnaligned()) {
            return linearLastIndexOf(buffer, fromIndex, toIndex, value);
        }
        final int longCount = length >>> 3;
        if (longCount > 0) {
            final ByteOrder nativeOrder = ByteOrder.nativeOrder();
            final boolean isNative = nativeOrder == buffer.order();
            final boolean useLE = nativeOrder == ByteOrder.LITTLE_ENDIAN;
            final long pattern = SWARUtil.compilePattern(value);
            for (int i = 0, offset = fromIndex - Long.BYTES; i < longCount; i++, offset -= Long.BYTES) {
                // use the faster available getLong
                final long word = useLE? buffer._getLongLE(offset) : buffer._getLong(offset);
                final long result = SWARUtil.applyPattern(word, pattern);
                if (result != 0) {
                    // used the oppoiste endianness since we are looking for the last index.
                    return offset + Long.BYTES - 1 - SWARUtil.getIndex(result, !isNative);
                }
            }
        }
        return unrolledLastIndexOf(buffer, fromIndex - (longCount << 3), length & 7, value);
    }

    private static int linearLastIndexOf(final AbstractByteBuf buffer, final int fromIndex, final int toIndex,
                                         final byte value) {
        for (int i = fromIndex - 1; i >= toIndex; i--) {
            if (buffer._getByte(i) == value) {
                return i;
            }
        }
        return -1;
    }

    private static int unrolledLastIndexOf(final AbstractByteBuf buffer, final int fromIndex, final int byteCount,
                                           final byte value) {
        assert byteCount >= 0 && byteCount < 8;
        if (byteCount == 0) {
            return -1;
        }