Repository: microsoft/garnet
File: libs/server/Custom/CustomFunctions.cs (C#)

Code before:
        /// </summary>
        protected static unsafe void WriteSimpleString(ref MemoryResult<byte> output, ReadOnlySpan<char> simpleString)
        {
            var _output = (output.MemoryOwner, output.Length);
            WriteSimpleString(ref _output, simpleString);
            output.MemoryOwner = _output.MemoryOwner;
            output.Length = _output.Length;
        }

        /// <summary>
        /// Create output as an array of bulk strings, from given array of ArgSlice values
        /// </summary>
        protected static unsafe void WriteBulkStringArray(ref MemoryResult<byte> output, params ArgSlice[] values)
        {
            int totalLen = 1 + NumUtils.NumDigits(values.Length) + 2;
            for (int i = 0; i < values.Length; i++)
                totalLen += RespWriteUtils.GetBulkStringLength(values[i].Length);

            output.MemoryOwner?.Dispose();
            output.MemoryOwner = MemoryPool.Rent(totalLen);
            output.Length = totalLen;

            fixed (byte* ptr = output.MemoryOwner.Memory.Span)
            {
                var curr = ptr;
                _ = RespWriteUtils.WriteArrayLength(values.Length, ref curr, ptr + totalLen);
                for (var i = 0; i < values.Length; i++)
                    _ = RespWriteUtils.WriteBulkString(values[i].Span, ref curr, ptr + totalLen);
            }
        }

        /// <summary>
        /// Create output as an array of bulk strings, from given array of ArgSlice values
        /// </summary>
        protected static unsafe void WriteBulkStringArray(ref MemoryResult<byte> output, List<ArgSlice> values)
        {
            var totalLen = 1 + NumUtils.NumDigits(values.Count) + 2;
            for (var i = 0; i < values.Count; i++)
                totalLen += RespWriteUtils.GetBulkStringLength(values[i].Length);

            output.MemoryOwner?.Dispose();
            output.MemoryOwner = MemoryPool.Rent(totalLen);
            output.Length = totalLen;

            fixed (byte* ptr = output.MemoryOwner.Memory.Span)
            {
                var curr = ptr;
                // Safe to write response into rented buffer without boundary checks
                _ = RespWriteUtils.WriteArrayLength(values.Count, ref curr, ptr + totalLen);
                for (var i = 0; i < values.Count; i++)
                    // Safe to write response into rented buffer without boundary checks

Review comment (bug):
I like that you have added a note to remember that we are using pre-allocated memory and that `WriteArrayLength` should never fail. I would add that comment here as well and maybe check the output of the call to `RespWriteUtils.WriteArrayLength` in a debug assert (applies to the other locations as well, of course).

Code after:
        /// <summary>
        /// Create output as simple string, from given string
        /// </summary>
        protected static unsafe void WriteSimpleString(ref MemoryResult<byte> output, ReadOnlySpan<char> simpleString)
        {
            var _output = (output.MemoryOwner, output.Length);
            WriteSimpleString(ref _output, simpleString);
            output.MemoryOwner = _output.MemoryOwner;
            output.Length = _output.Length;
        }

        /// <summary>
        /// Create output as an array of bulk strings, from given array of ArgSlice values
        /// </summary>
        protected static unsafe void WriteBulkStringArray(ref MemoryResult<byte> output, params ArgSlice[] values)
        {
            var totalLen = 1 + NumUtils.NumDigits(values.Length) + 2;
            for (var i = 0; i < values.Length; i++)
                totalLen += RespWriteUtils.GetBulkStringLength(values[i].Length);

            output.MemoryOwner?.Dispose();
            output.MemoryOwner = MemoryPool.Rent(totalLen);
            output.Length = totalLen;

            fixed (byte* ptr = output.MemoryOwner.Memory.Span)
            {
                var curr = ptr;
                // NOTE: Expected to always have enough space to write into pre-allocated buffer
                var success = RespWriteUtils.WriteArrayLength(values.Length, ref curr, ptr + totalLen);
                Debug.Assert(success, "Insufficient space in pre-allocated buffer");
                for (var i = 0; i < values.Length; i++)
                {
                    // NOTE: Expected to always have enough space to write into pre-allocated buffer
                    success = RespWriteUtils.WriteBulkString(values[i].Span, ref curr, ptr + totalLen);
                    Debug.Assert(success, "Insufficient space in pre-allocated buffer");
                }
            }
        }

        /// <summary>
        /// Create output as an array of bulk strings, from given array of ArgSlice values
        /// </summary>
        protected static unsafe void WriteBulkStringArray(ref MemoryResult<byte> output, List<ArgSlice> values)
        {
            var totalLen = 1 + NumUtils.NumDigits(values.Count) + 2;
            for (var i = 0; i < values.Count; i++)
                totalLen += RespWriteUtils.GetBulkStringLength(values[i].Length);

            output.MemoryOwner?.Dispose();
            output.MemoryOwner = MemoryPool.Rent(totalLen);
            output.Length = totalLen;