Repository: microsoft/garnet
File: libs/server/Storage/Session/MainStore/MainStoreOps.cs (C#)

Code before:
            var status = context.RMW(ref sbKey, ref Unsafe.AsRef<SpanByte>(pbCmdInput), ref sbmOut);
            if (status.IsPending)
                CompletePendingForSession(ref status, ref sbmOut, ref context);

            Debug.Assert(sbmOut.IsSpanByte);
            output.length = sbmOut.Length;

            return GarnetStatus.OK;
        }

        public GarnetStatus Increment<TContext>(ArgSlice key, ArgSlice input, ref ArgSlice output, ref TContext context)
            where TContext : ITsavoriteContext<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, long>
        {
            var _key = key.SpanByte;
            var _input = input.SpanByte;
            SpanByteAndMemory _output = new(output.SpanByte);

            var status = context.RMW(ref _key, ref _input, ref _output);
            if (status.IsPending)
                CompletePendingForSession(ref status, ref _output, ref context);
            Debug.Assert(_output.IsSpanByte);
            output.length = _output.Length;
            return GarnetStatus.OK;
        }

        public unsafe GarnetStatus Increment<TContext>(ArgSlice key, long increment, out OperationError operationError, out long output, ref TContext context)
            where TContext : ITsavoriteContext<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, long>
        {
            var cmd = RespCommand.INCRBY;
            if (increment < 0)
            {
                cmd = RespCommand.DECRBY;
                increment = -increment;
            }

            var incrementNumDigits = NumUtils.NumDigitsInLong(increment);
            var inputByteSize = RespInputHeader.Size + incrementNumDigits;
            var input = stackalloc byte[inputByteSize];
            ((RespInputHeader*)input)->cmd = cmd;
            ((RespInputHeader*)input)->flags = 0;
            var longOutput = input + RespInputHeader.Size;
            NumUtils.LongToBytes(increment, incrementNumDigits, ref longOutput);

            const int outputBufferLength = NumUtils.MaximumFormatInt64Length + 1;
            byte* outputBuffer = stackalloc byte[outputBufferLength];

            var _key = key.SpanByte;
            var _input = SpanByte.FromPinnedPointer(input, inputByteSize);
            var _output = new SpanByteAndMemory(outputBuffer, outputBufferLength);

            var status = context.RMW(ref _key, ref _input, ref _output);

Review comment (bug):
This shouldn't return a success/error state as the input will always be of valid type, you can Debug.Assert(_output.Length == outputBufferLength)

Code after:
            var status = context.RMW(ref sbKey, ref Unsafe.AsRef<SpanByte>(pbCmdInput), ref sbmOut);
            if (status.IsPending)
                CompletePendingForSession(ref status, ref sbmOut, ref context);

            Debug.Assert(sbmOut.IsSpanByte);
            output.length = sbmOut.Length;

            return GarnetStatus.OK;
        }

        public GarnetStatus Increment<TContext>(ArgSlice key, ArgSlice input, ref ArgSlice output, ref TContext context)
            where TContext : ITsavoriteContext<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, long, MainStoreFunctions>
        {
            var _key = key.SpanByte;
            var _input = input.SpanByte;
            SpanByteAndMemory _output = new(output.SpanByte);

            var status = context.RMW(ref _key, ref _input, ref _output);
            if (status.IsPending)
                CompletePendingForSession(ref status, ref _output, ref context);
            Debug.Assert(_output.IsSpanByte);
            output.length = _output.Length;
            return GarnetStatus.OK;
        }

        public unsafe GarnetStatus Increment<TContext>(ArgSlice key, out long output, long increment, ref TContext context)
            where TContext : ITsavoriteContext<SpanByte, SpanByte, SpanByte, SpanByteAndMemory, long, MainStoreFunctions>
        {
            var cmd = RespCommand.INCRBY;
            if (increment < 0)
            {
                cmd = RespCommand.DECRBY;
                increment = -increment;
            }

            var incrementNumDigits = NumUtils.NumDigitsInLong(increment);
            var inputByteSize = RespInputHeader.Size + incrementNumDigits;
            var input = stackalloc byte[inputByteSize];
            ((RespInputHeader*)input)->cmd = cmd;
            ((RespInputHeader*)input)->flags = 0;
            var longOutput = input + RespInputHeader.Size;
            NumUtils.LongToBytes(increment, incrementNumDigits, ref longOutput);

            const int outputBufferLength = NumUtils.MaximumFormatInt64Length + 1;
            byte* outputBuffer = stackalloc byte[outputBufferLength];

            var _key = key.SpanByte;
            var _input = SpanByte.FromPinnedPointer(input, inputByteSize);
            var _output = new SpanByteAndMemory(outputBuffer, outputBufferLength);

            var status = context.RMW(ref _key, ref _input, ref _output);