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

Code before:
            // #if DEBUG
            // logger?.LogTrace("RECV: [{recv}]", Encoding.UTF8.GetString(new Span<byte>(recvBufferPtr, bytesRead)).Replace("\n", "|").Replace("\r", ""));
            // Debug.WriteLine($"RECV: [{Encoding.UTF8.GetString(new Span<byte>(recvBufferPtr, bytesRead)).Replace("\n", "|").Replace("\r", "")}]");
            // #endif

            var _origReadHead = readHead;

            while (bytesRead - readHead >= 4)
            {
                // First, parse the command, making sure we have the entire command available
                // We use endReadHead to track the end of the current command
                // On success, readHead is left at the start of the command payload for legacy operators
                var cmd = ParseCommand(out bool commandReceived);

                // If the command was not fully received, reset addresses and break out
                if (!commandReceived)
                {
                    endReadHead = readHead = _origReadHead;
                    break;
                }

                // Check ACL permissions for the command
                if (cmd != RespCommand.INVALID && CheckACLPermissions(cmd))
                {

                    if (runningWithAOFWaitForCommitMode)
                    {
                        /* 
                            keeping the expensive call inside the conditional only adds ~4 MSIL instructions in hotpath

                            W.r.t AOF  Blocking
                            If a previous command marked AOF for blocking we should not change AOF blocking flag.
                            If no previous command marked AOF for blocking, then we only change AOF flag to block
                            if the current command is AOF dependent

                            Ordering for Truth Table:
                            WaitForAofBlocking || !(IsAofIndepenent) => Whether or not it should block AOF

                            Truth Table:
                            T || !F => T (Block AOF if WaitForAofBlocking was already set)
                            T || !T => T (Block AOF if WaitForAofBlocking was already set)
                            F || !T => F, (Don't Block AOF if WaitForAofBlocking was not set and cmd is Aof INDEPENDENT)
                            F || !F => T, (Block AOF if WaitForAofBlocking was not set and cmd is aof DEPENDENT)
                        */
                        waitForAofBlocking = waitForAofBlocking || !cmd.IsAofIndependent();
                    }

                    if (txnManager.state != TxnState.None)
                    {
                        if (txnManager.state == TxnState.Running)
                        {

Review comment (suggestion):
This is polluting the main ProcessMessages. Can it be moved to `ParseCommand` and tighten the long comment.

Code after:
            return readHead;
        }

        private void ProcessMessages()
        {
            // #if DEBUG
            // logger?.LogTrace("RECV: [{recv}]", Encoding.UTF8.GetString(new Span<byte>(recvBufferPtr, bytesRead)).Replace("\n", "|").Replace("\r", ""));
            // Debug.WriteLine($"RECV: [{Encoding.UTF8.GetString(new Span<byte>(recvBufferPtr, bytesRead)).Replace("\n", "|").Replace("\r", "")}]");
            // #endif

            var _origReadHead = readHead;

            while (bytesRead - readHead >= 4)
            {
                // First, parse the command, making sure we have the entire command available
                // We use endReadHead to track the end of the current command
                // On success, readHead is left at the start of the command payload for legacy operators
                var cmd = ParseCommand(out bool commandReceived);

                // If the command was not fully received, reset addresses and break out
                if (!commandReceived)
                {
                    endReadHead = readHead = _origReadHead;
                    break;
                }

                // Check ACL permissions for the command
                if (cmd != RespCommand.INVALID && CheckACLPermissions(cmd))
                {
                    if (txnManager.state != TxnState.None)
                    {
                        if (txnManager.state == TxnState.Running)
                        {
                            _ = ProcessBasicCommands(cmd, ref lockableGarnetApi);
                        }
                        else _ = cmd switch
                        {
                            RespCommand.EXEC => NetworkEXEC(),
                            RespCommand.MULTI => NetworkMULTI(),
                            RespCommand.DISCARD => NetworkDISCARD(),
                            RespCommand.QUIT => NetworkQUIT(),
                            _ => NetworkSKIP(cmd),
                        };
                    }
                    else
                    {
                        _ = ProcessBasicCommands(cmd, ref basicGarnetApi);
                    }
                }

                // Advance read head variables to process the next command