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

Code before:
        /// <param name="count">Number of remaining tokens belonging to this command on the receive buffer.</param>
        /// <param name="errorMessage">Error message to print to result stream</param>
        /// <returns>true if the command was completely consumed, false if the input on the receive buffer was incomplete.</returns>
        private bool AbortWithErrorMessage(int count, ReadOnlySpan<byte> errorMessage)
        {
            // Abort command and discard any remaining tokens on the input buffer
            var bufSpan = new ReadOnlySpan<byte>(recvBufferPtr, bytesRead);

            if (!DrainCommands(bufSpan, count))
                return false;

            // Print error message to result stream
            while (!RespWriteUtils.WriteError(errorMessage, ref dcurr, dend))
                SendAndReset();

            return true;
        }

        /// <summary>
        /// Tries to parse the input as "LEFT" or "RIGHT" and returns the corresponding OperationDirection.
        /// If parsing fails, returns OperationDirection.Unknown.
        /// </summary>
        /// <param name="input">The input to parse.</param>
        /// <returns>The parsed OperationDirection, or OperationDirection.Unknown if parsing fails.</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public OperationDirection GetOperationDirection(ReadOnlySpan<byte> input)
        {
#if NET8_0_OR_GREATER
            if (Ascii.EqualsIgnoreCase(input, "RIGHT"))
            {
                return OperationDirection.Right;
            }
            else if (Ascii.EqualsIgnoreCase(input, "LEFT"))
            {
                return OperationDirection.Left;
            }
#else
            string inputString = Encoding.UTF8.GetString(input);
            if (inputString.Equals("RIGHT", StringComparison.OrdinalIgnoreCase))
            {
                return OperationDirection.Right;
            }
            else if (inputString.Equals("LEFT", StringComparison.OrdinalIgnoreCase))
            {
                return OperationDirection.Left;
            }
#endif
            else
            {
                return OperationDirection.Unknown;
            }

Review comment (performance):
Use this version to optimize for the common case and avoid strings for pre-NET8"

```cs
        public OperationDirection GetOperationDirection(ArgSlice input)
        {
            // Optimize for the common case
            if (input.ReadOnlySpan.SequenceEqual("LEFT"u8))
                return OperationDirection.Left;
            if (input.ReadOnlySpan.SequenceEqual("RIGHT"u8))
                return OperationDirection.Right;
            // Rare case: try making upper case and retry
            MakeUpperCase(input.ptr);
            if (input.ReadOnlySpan.SequenceEqual("LEFT"u8))
                return OperationDirection.Left;
            if (input.ReadOnlySpan.SequenceEqual("RIGHT"u8))
                return OperationDirection.Right;
            return OperationDirection.Unknown;
        }
```

Code after:
        /// <param name="errorMessage">Error message to print to result stream</param>
        /// <returns>true if the command was completely consumed, false if the input on the receive buffer was incomplete.</returns>
        private bool AbortWithErrorMessage(int count, ReadOnlySpan<byte> errorMessage)
        {
            // Abort command and discard any remaining tokens on the input buffer
            var bufSpan = new ReadOnlySpan<byte>(recvBufferPtr, bytesRead);

            if (!DrainCommands(bufSpan, count))
                return false;

            // Print error message to result stream
            while (!RespWriteUtils.WriteError(errorMessage, ref dcurr, dend))
                SendAndReset();

            return true;
        }

        /// <summary>
        /// Tries to parse the input as "LEFT" or "RIGHT" and returns the corresponding OperationDirection.
        /// If parsing fails, returns OperationDirection.Unknown.
        /// </summary>
        /// <param name="input">The input to parse.</param>
        /// <returns>The parsed OperationDirection, or OperationDirection.Unknown if parsing fails.</returns>
        public OperationDirection GetOperationDirection(ArgSlice input)
        {
            // Optimize for the common case
            if (input.ReadOnlySpan.SequenceEqual("LEFT"u8))
                return OperationDirection.Left;
            if (input.ReadOnlySpan.SequenceEqual("RIGHT"u8))
                return OperationDirection.Right;
            // Rare case: try making upper case and retry
            MakeUpperCase(input.ptr);
            if (input.ReadOnlySpan.SequenceEqual("LEFT"u8))
                return OperationDirection.Left;
            if (input.ReadOnlySpan.SequenceEqual("RIGHT"u8))
                return OperationDirection.Right;
            return OperationDirection.Unknown;
        }
    }
}