Repository: netty/netty
File: codec-http/src/main/java/io/netty/handler/codec/http/HttpUtil.java (Java)

Code before:
        }
    }

    /**
     * Validate that the given request line token is safe for verbatim encoding to the network.
     * This does not fully check that the token – HTTP method, version, or URI – is valid and formatted correctly.
     * Only that the token does not contain characters that would break or
     * desynchronize HTTP message parsing of the start line wherein the token would be included.
     * <p>
     * See <a href="https://datatracker.ietf.org/doc/html/rfc9112#name-request-line">RFC 9112, 3.</a>
     *
     * @param token The token to check.
     * @return {@code true} if the token is safe to encode verbatim into the HTTP message output stream,
     * otherwise {@code false}.
     */
    public static boolean isEncodingSafeStartLineToken(CharSequence token) {
        int i = 0;
        int lenBytes = token.length();
        int modulo = lenBytes % 4;
        int lenInts = modulo == 0 ? lenBytes : lenBytes - modulo;
        for (; i < lenInts; i += 4) {
            long chars = 1L << token.charAt(i) |
                    1L << token.charAt(i + 1) |
                    1L << token.charAt(i + 2) |
                    1L << token.charAt(i + 3);
            if ((chars & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        for (; i < lenBytes; i++) {
            long ch = 1L << token.charAt(i);
            if ((ch & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns {@code true} if and only if the connection can remain open and
     * thus 'kept alive'.  This methods respects the value of the.
     *
     * {@code "Connection"} header first and then the return value of
     * {@link HttpVersion#isKeepAliveDefault()}.
     */
    public static boolean isKeepAlive(HttpMessage message) {
        return !message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE, true) &&
               (message.protocolVersion().isKeepAliveDefault() ||
                message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE, true));
    }


Review comment (performance):
I am not sure batching would pay off here: validations are a strange affair!
If we assume is rare to return false, computing the 4 ors and one and with the illegal request line is still not ideal!
Just writing straight comparison chart per char (no need to or and batch) should perform 4 more ands (which are bad) and 4 comparisons, but will make the loads independent..in short comparison outcomes are processed even before CPU does fetch and decode and keeping the 4 char loads independent would make the pipeline able to keep on speculating and processing more chars.

Code after:
        }
    }

    /**
     * Validate that the given request line token is safe for verbatim encoding to the network.
     * This does not fully check that the token – HTTP method, version, or URI – is valid and formatted correctly.
     * Only that the token does not contain characters that would break or
     * desynchronize HTTP message parsing of the start line wherein the token would be included.
     * <p>
     * See <a href="https://datatracker.ietf.org/doc/html/rfc9112#name-request-line">RFC 9112, 3.</a>
     *
     * @param token The token to check.
     * @return {@code true} if the token is safe to encode verbatim into the HTTP message output stream,
     * otherwise {@code false}.
     */
    public static boolean isEncodingSafeStartLineToken(CharSequence token) {
        int i = 0;
        int lenBytes = token.length();
        int modulo = lenBytes % 4;
        int lenInts = modulo == 0 ? lenBytes : lenBytes - modulo;
        for (; i < lenInts; i += 4) {
            long chars = charMask(token, i) |
                    charMask(token, i + 1) |
                    charMask(token, i + 2) |
                    charMask(token, i + 3);
            if ((chars & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        for (; i < lenBytes; i++) {
            long ch = charMask(token, i);
            if ((ch & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
                return false;
            }
        }
        return true;
    }

    private static long charMask(CharSequence token, int i) {
        char c = token.charAt(i);
        return c < 64 ? 1L << c : 0;
    }

    /**
     * Returns {@code true} if and only if the connection can remain open and
     * thus 'kept alive'. This method respects the value of the
     * {@code "Connection"} header first and then the return value of
     * {@link HttpVersion#isKeepAliveDefault()}.
     */
    public static boolean isKeepAlive(HttpMessage message) {
        return !message.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE, true) &&