Repository: microsoft/garnet
File: libs/server/Resp/RespCommand.cs (C#)

Code before:

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static T Read<T>(ReadOnlySpan<byte> span)
            {
                // Simplified version of MemoryMarshal.Read to help out .NET 6 JIT
                return Unsafe.ReadUnaligned<T>(ref MemoryMarshal.GetReference(span));
            }

            if (bytesRead - readHead >= 8 && (*(int*)ptr & Read<int>(smallArrayMask)) == Read<int>(smallArrayMask))
            {
                switch ((*(int*)ptr >> 8) & 0xFF)
                {
                    case '0':

                        if ((*(1 + (int*)ptr) & Read<int>(smallBulkStringMask)) == Read<int>(smallBulkStringMask))
                        {
                            switch ((*(1 + (int*)ptr) >> 8) & 0xFF)
                            {
                                case '6':
                                    if (bytesRead - readHead >= 16) // TODO: make sure this condition is appropriate per command
                                    {
                                        if (*(1 + (long*)ptr) == Read<long>("RUNTXP\r\n"u8))
                                            return RespCommand.RUNTXP;
                                    }

                                    break;
                            }
                        }

                        break;
                    case '1':

                        if ((*(1 + (int*)ptr) & Read<int>(smallBulkStringMask)) == Read<int>(smallBulkStringMask))
                        {
                            switch ((*(1 + (int*)ptr) >> 8) & 0xFF)
                            {
                                case '4':
                                    if (bytesRead - readHead >= 12) // TODO: make sure this condition is appropriate per command
                                    {
                                        if (*(2 + (int*)ptr) == Read<int>("EXEC"u8))
                                            return RespCommand.EXEC;
                                        if (*(2 + (int*)ptr) == Read<int>("PING"u8))
                                            return RespCommand.PING;
                                    }

                                    break;
                                case '5':
                                    if (bytesRead - readHead >= 13) // TODO: make sure this condition is appropriate per command
                                    {
                                        if (*(2 + (int*)ptr) == Read<int>("MULT"u8) && *(12 + ptr) == 'I')
                                            return RespCommand.MULTI;

Review comment (suggestion):
Some variation of following should be possible to reduce indentation. Roslyn should lower to same conditions. These switch exprenssions can be abused endlessly, e.g. just chaining all these conditions depending on the case but it might get back to unreadable state then 😆 
```suggestion
                                case '6' when bytesRead - readHead >= 16:
                                    if (*(1 + (long*)ptr) == Read<long>("RUNTXP\r\n"u8))
                                        return RespCommand.RUNTXP;
```

Code after:

        INVALID = 0xFF
    }

    /// <summary>
    /// RESP command options enum
    /// </summary>
    enum RespCommandOption : byte
    {
        EX, NX, XX, GET, PX, EXAT, PXAT, PERSIST, GT, LT
    }

    /// <summary>
    /// Server session for RESP protocol - command definitions and fast parsing
    /// </summary>
    internal sealed unsafe partial class RespServerSession : ServerSessionBase
    {
        /// <summary>
        /// Fast-parses command type for inline RESP commands, starting at the current read head in the receive buffer
        /// and advances read head.
        /// </summary>
        /// <param name="count">Outputs the number of arguments stored with the command.</param>
        /// <returns>RespCommand that was parsed or RespCommand.NONE, if no command was matched in this pass.</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private RespCommand FastParseInlineCommand(out int count)
        {
            byte* ptr = recvBufferPtr + readHead;
            count = 0;

            if (bytesRead - readHead >= 6)
            {
                if ((*(ushort*)(ptr + 4) == MemoryMarshal.Read<ushort>("\r\n"u8)))
                {
                    // Optimistically increase read head
                    readHead += 6;

                    if ((*(uint*)ptr) == MemoryMarshal.Read<uint>("PING"u8))
                    {
                        return RespCommand.PING;
                    }

                    if ((*(uint*)ptr) == MemoryMarshal.Read<uint>("QUIT"u8))
                    {
                        return RespCommand.QUIT;
                    }

                    // Decrease read head, if no match was found
                    readHead -= 6;
                }
            }