Repository: dotnet/aspnetcore
File: src/DataProtection/DataProtection/src/KeyManagement/KeyRingBasedDataProtector.cs (C#)

Code before:
                int purposeLength = EncodingUtil.SecureUtf8Encoding.GetByteCount(purpose);
                purposeLengthsPool[i] = purposeLength;

                var encoded7BitUIntLength = purposeLength.Measure7BitEncodedUIntLength();
                totalPurposeLen += purposeLength /* length of actual string */ + encoded7BitUIntLength /* length of 'string length' 7-bit encoded int */;
            }

            byte[] targetArr = new byte[totalPurposeLen];
            var targetSpan = targetArr.AsSpan();

            // index 0: magic header
            BinaryPrimitives.WriteUInt32BigEndian(targetSpan.Slice(0), MAGIC_HEADER_V0);
            // index 4: key (skipped for now, will be populated in `GetAadForKey()`)
            // index 4 + keySize: purposeCount
            BinaryPrimitives.WriteInt32BigEndian(targetSpan.Slice(4 + keySize), purposes.Length);

            int index = 4 + keySize + 4; // starting from first purpose
            for (int i = 0; i < purposes.Length; i++)
            {
                string purpose = purposes[i];

                // writing `utf8ByteCount (7-bit encoded integer) || utf8Text`
                // we have already calculated the lengths of the purpose strings, so just get it from the pool
                index += targetSpan.Slice(index).Write7BitEncodedInt(purposeLengthsPool[i]);

#if NET10_0_OR_GREATER
                index += EncodingUtil.SecureUtf8Encoding.GetBytes(purpose.AsSpan(), targetSpan.Slice(index));
#else
                index += EncodingUtil.SecureUtf8Encoding.GetBytes(purpose, charIndex: 0, charCount: purpose.Length, bytes: targetArr, byteIndex: index);
#endif
            }

            ArrayPool<int>.Shared.Return(purposeLengthsPool);
            Debug.Assert(index == targetArr.Length);

            return targetArr;
        }
    }

    private enum UnprotectStatus
    {
        Ok,
        DefaultEncryptionKeyChanged,
        DecryptionKeyWasRevoked
    }
}


Review comment (suggestion):
nit; since we have the array path *anyway*, I wonder if we should just use the second version in all cases, to avoid an unnecessary `#if` - it won't have any relevant perf delta - anything gained in one direction will be offset by things like the extra `Slice`

Code after:
                string purpose = purposes[i];

                int purposeLength = EncodingUtil.SecureUtf8Encoding.GetByteCount(purpose);
                purposeLengthsPool[i] = purposeLength;

                var encoded7BitUIntLength = purposeLength.Measure7BitEncodedUIntLength();
                totalPurposeLen += purposeLength /* length of actual string */ + encoded7BitUIntLength /* length of 'string length' 7-bit encoded int */;
            }

            byte[] targetArr = new byte[totalPurposeLen];
            var targetSpan = targetArr.AsSpan();

            // index 0: magic header
            BinaryPrimitives.WriteUInt32BigEndian(targetSpan.Slice(0), MAGIC_HEADER_V0);
            // index 4: key (skipped for now, will be populated in `GetAadForKey()`)
            // index 4 + keySize: purposeCount
            BinaryPrimitives.WriteInt32BigEndian(targetSpan.Slice(4 + keySize), targetLength);

            int index = 4 /* MAGIC_HEADER_V0 */ + keySize + 4 /* purposeLength */; // starting from first purpose
            for (int i = 0; i < targetLength; i++)
            {
                string purpose = purposes[i];

                // writing `utf8ByteCount (7-bit encoded integer)`
                // we have already calculated the lengths of the purpose strings, so just get it from the pool
                index += targetSpan.Slice(index).Write7BitEncodedInt(purposeLengthsPool[i]);

                // write the utf8text for the purpose
                index += EncodingUtil.SecureUtf8Encoding.GetBytes(purpose, charIndex: 0, charCount: purpose.Length, bytes: targetArr, byteIndex: index);
            }

            if (lease is not null)
            {
                ArrayPool<int>.Shared.Return(lease);
            }
            Debug.Assert(index == targetArr.Length);

            return targetArr;
        }
    }

    private enum UnprotectStatus
    {
        Ok,
        DefaultEncryptionKeyChanged,
        DecryptionKeyWasRevoked
    }
}