String中的Contact/Join方法

起因

在更新.Net源码时发现有对String.Join做性能优化.便看了一下是如何优化的.来看这个代码提交.
看到.Net源码有对String.Join的性能优化
看到.Net源码对String.Join具体优化的代码

String.Join方法源码

public static string Join<T>(char separator, IEnumerable<T> values) =>
    JoinCore(MemoryMarshal.CreateReadOnlySpan(ref separator, 1), values);

public static string Join<T>(string? separator, IEnumerable<T> values) =>
    JoinCore(separator.AsSpan(), values);

private static string JoinCore<T>(ReadOnlySpan<char> separator, IEnumerable<T> values)
{
    if (typeof(T) == typeof(string))  //如果类型是字符串,则优先处理
    {
        if (values is List<string?> valuesList) //如果是List<string>
        {
            return JoinCore(separator, CollectionsMarshal.AsSpan(valuesList));
        }

        if (values is string?[] valuesArray) //如果是string数组
        {
            return JoinCore(separator, new ReadOnlySpan<string?>(valuesArray));
        }
    }

    if (values == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.values);
    }
    //如果是其他类型,进行遍历
    using (IEnumerator<T> en = values.GetEnumerator())
    {
        if (!en.MoveNext())
        {
            return Empty;
        }

        // We called MoveNext once, so this will be the first item
        T currentValue = en.Current;

        // Call ToString before calling MoveNext again, since
        // we want to stay consistent with the below loop
        // Everything should be called in the order
        // MoveNext-Current-ToString, unless further optimizations
        // can be made, to avoid breaking changes
        string? firstString = currentValue?.ToString();  //如果调用类型的ToString方法

        // If there's only 1 item, simply call ToString on that
        if (!en.MoveNext())
        {
            // We have to handle the case of either currentValue
            // or its ToString being null
            return firstString ?? Empty;
        }
                //这里使用栈,预先分配256字节,将在栈上分配字符数组,传递给ValueStringBuilder        //这里先不说ValueStringBuilder,下边会源码进行解析
        var result = new ValueStringBuilder(stackalloc char[256]);   
 
        result.Append(firstString);

        do
        {
            currentValue = en.Current;

            result.Append(separator);
            if (currentValue != null)
            {
                result.Append(currentValue.ToString());
            }
        }
        while (en.MoveNext());

        return result.ToString();
    }
}

private static string JoinCore(ReadOnlySpan<char> separator, ReadOnlySpan<string?> values)
{
    if (values.Length <= 1)
    {
        return values.IsEmpty ?
            Empty :
            values[0] ?? Empty;
    }

    long totalSeparatorsLength = (long)(values.Length - 1) * separator.Length;
    if (totalSeparatorsLength > int.MaxValue)
    {
        ThrowHelper.ThrowOutOfMemoryException();
    }
    int totalLength = (int)totalSeparatorsLength;

    // Calculate the length of the resultant string so we know how much space to allocate.
    //计算List或者数组中的每个字符串的长度
    foreach (string? value in values)
    {
        if (value != null)
        {
            totalLength += value.Length;
            if (totalLength < 0) // Check for overflow
            {
                ThrowHelper.ThrowOutOfMemoryException();
            }
        }
    }

    // Copy each of the strings into the result buffer, interleaving with the separator.
    //根据计算出的总长度进行内存分配, FastAllocateString是运行时方法,不是c#编写
    string result = FastAllocateString(totalLength);
    int copiedLength = 0;

    for (int i = 0; i < values.Length; i++)
    {
        // It's possible that another thread may have mutated the input array
        // such that our second read of an index will not be the same string
        // we got during the first read.

        // We range check again to avoid buffer overflows if this happens.

        if (values[i] is string value)
        {
            int valueLen = value.Length;
            if (valueLen > totalLength - copiedLength)
            {
                copiedLength = -1;
                break;
            }

            // Fill in the value.
            //1. 将List或者数组的内容填充,分配好的result中
            FillStringChecked(result, copiedLength, value);
            copiedLength += valueLen;
        }

        //2.将分隔符填充到result
        if (i < values.Length - 1)
        {
            // Fill in the separator.
            // Special-case length 1 to avoid additional overheads of CopyTo.
            // This is common due to the char separator overload.

            ref char dest = ref Unsafe.Add(ref result._firstChar, copiedLength);
            //2.1 如果分隔符的长度为1,直接填充
            if (separator.Length == 1)
            {
                dest = separator[0];
            }
            else
            {
                //2.2 如果分隔符不为1,则通过span根据长度进行填充
                separator.CopyTo(new Span<char>(ref dest, separator.Length));
            }

            copiedLength += separator.Length;
        }
    }

    // If we copied exactly the right amount, return the new string.  Otherwise,
    // something changed concurrently to mutate the input array: fall back to
    // doing the concatenation again, but this time with a defensive copy. This
    // fall back should be extremely rare.
    return copiedLength == totalLength ?
        result :
        JoinCore(separator, values.ToArray().AsSpan());
}

String.Concat方法源码

因为Concat重载方法比较多,这里只是列出3个Concat方法.
public static string Concat(IEnumerable<string?> values)
{
    if (values == null)
        throw new ArgumentNullException(nameof(values));

    using (IEnumerator<string?> en = values.GetEnumerator())
    {
        if (!en.MoveNext())
            return string.Empty;

        string? firstValue = en.Current;

        if (!en.MoveNext())
        {
            return firstValue ?? string.Empty;
        }

        var result = new ValueStringBuilder(stackalloc char[256]);  //使用栈,这里先不说ValueStringBuilder

        result.Append(firstValue);

        do
        {
            result.Append(en.Current);
        }
        while (en.MoveNext());

        return result.ToString();
    }
}

public static string Concat(string? str0, string? str1)
{
    if (IsNullOrEmpty(str0))
    {
        if (IsNullOrEmpty(str1))
        {
            return string.Empty;
        }
        return str1;
    }

    if (IsNullOrEmpty(str1))
    {
        return str0;
    }

    int str0Length = str0.Length;

    string result = FastAllocateString(str0Length + str1.Length);  //计算两个字符串的长度,分配内存空间

    FillStringChecked(result, 0, str0);               //将第一个字符串拷贝到新分配的字符串result中
    FillStringChecked(result, str0Length, str1);      //将第二个字符串拷贝到新分配的字符串result中

    return result;
}

public static string Concat(params string?[] values)
{
    if (values == null)
        throw new ArgumentNullException(nameof(values));

    if (values.Length <= 1)
    {
        return values.Length == 0 ?
            string.Empty :
            values[0] ?? string.Empty;
    }

    // It's possible that the input values array could be changed concurrently on another
    // thread, such that we can't trust that each read of values[i] will be equivalent.
    // Worst case, we can make a defensive copy of the array and use that, but we first
    // optimistically try the allocation and copies assuming that the array isn't changing,
    // which represents the 99.999% case, in particular since string.Concat is used for
    // string concatenation by the languages, with the input array being a params array.

    // Sum the lengths of all input strings
    //1. 计算字符串数组中的每个字符串的长度
    long totalLengthLong = 0;
    for (int i = 0; i < values.Length; i++)
    {
        string? value = values[i];
        if (value != null)
        {
            totalLengthLong += value.Length;
        }
    }

    // If it's too long, fail, or if it's empty, return an empty string.
    if (totalLengthLong > int.MaxValue)
    {
        throw new OutOfMemoryException();
    }
    int totalLength = (int)totalLengthLong;
    if (totalLength == 0)
    {
        return string.Empty;
    }

    // Allocate a new string and copy each input string into it
    //根据计算出的所有字符串的长度,分配内存空间
    string result = FastAllocateString(totalLength);
    int copiedLength = 0;
    for (int i = 0; i < values.Length; i++)
    {
        string? value = values[i];
        if (!string.IsNullOrEmpty(value))
        {
            int valueLen = value.Length;
            if (valueLen > totalLength - copiedLength)
            {
                copiedLength = -1;
                break;
            }
            //依次进行填充字符串内容
            FillStringChecked(result, copiedLength, value);
            copiedLength += valueLen;
        }
    }

    // If we copied exactly the right amount, return the new string.  Otherwise,
    // something changed concurrently to mutate the input array: fall back to
    // doing the concatenation again, but this time with a defensive copy. This
    // fall back should be extremely rare.
    return copiedLength == totalLength ? result : Concat((string?[])values.Clone());
}

说说ValueStringBuilder

ValueStringBuilder类是用internal标记的,意味着在我们的项目中无法直接使用,但不妨碍我们对ValueStringBuilder源码的学习.
//使用internal修饰表示在程序集内部使用
//使用struct,说明ValueStringBuilder为结构体,在struct前面使用ref声明,表示该结构体实现接口,也不能装箱,最典型的就是Span也是这样使用的
internal ref partial struct ValueStringBuilder
{
    //在使用数组池,从数组池租用空间
    private char[]? _arrayToReturnToPool;

    //指向字符串第一个字符串,记录字符串的大小
    private Span<char> _chars;

    //存放内容记录位置
    private int _pos;

    //通过构造函数将字符串内容指向Span类型的_chars,不管内容是否来自栈上,还是堆上
    //如果初始化内容来自栈,长度的不够,还是通过数组池租用空间,在将内容拷贝到_arrayToReturnToPool
    public ValueStringBuilder(Span<char> initialBuffer)
    {
        _arrayToReturnToPool = null;
        _chars = initialBuffer;
        _pos = 0;
    }

    //如果直接通过构造函数指定初始化长度,直接通过数组池租用空间
    public ValueStringBuilder(int initialCapacity)
    {
        _arrayToReturnToPool = ArrayPool<char>.Shared.Rent(initialCapacity);
        _chars = _arrayToReturnToPool;
        _pos = 0;
    }

    //将字符串内容进行追加
    public void Append(string? s)
    {
        if (s == null)
        {
            return;
        }

        int pos = _pos;
        //如果当前字符串是一个字符时直接修改内容和位置
        if (s.Length == 1 && (uint)pos < (uint)_chars.Length) //
        {
            _chars[pos] = s[0];
            _pos = pos + 1;
        }
        else
        {
            AppendSlow(s);
        }
    }

    //追加字符串
    private void AppendSlow(string s)
    {
        int pos = _pos;
        if (pos > _chars.Length - s.Length) //判断空间是否够用,不够用,重新分配空间
        {
            Grow(s.Length);
        }

        //将新的字符串追加到_chars,修改字符串的位置
        s
#if !NET6_0_OR_GREATER
            .AsSpan()
#endif
            .CopyTo(_chars.Slice(pos));
        _pos += s.Length;
    }

    //当空间不够用的时候调用
    private void Grow(int additionalCapacityBeyondPos)
    {
        //重新先数组池租用空间,大小为当前字符串的2倍,如果当前位置加要增加字符串的长度要大于当前字符串的长度2倍的话,则大小为 当前位置加要增加字符串的长度
        char[] poolArray = ArrayPool<char>.Shared.Rent((int)Math.Max((uint)(_pos + additionalCapacityBeyondPos), (uint)_chars.Length * 2));

        //将内容拷贝到从数组池租用的新的字符数组中
        _chars.Slice(0, _pos).CopyTo(poolArray);

        //获取数组池指向老地址
        char[]? toReturn = _arrayToReturnToPool;

        //将新的租用字符数组指向_chars和_arrayToReturnToPool
        _chars = _arrayToReturnToPool = poolArray;
        if (toReturn != null)
        {
            //在将老的指向空间进行归还
            ArrayPool<char>.Shared.Return(toReturn);
        }
    }
}

看看FillStringChecked源码

private static void FillStringChecked(string dest, int destPos, string src)
{
    Debug.Assert(dest != null);
    Debug.Assert(src != null);
    if (src.Length > dest.Length - destPos)
    {
        throw new IndexOutOfRangeException();
    }

    //内部有优化,这里就不是Memmove源码了
    Buffer.Memmove(
        destination: ref Unsafe.Add(ref dest._firstChar, destPos),
        source: ref src._firstChar,
        elementCount: (uint)src.Length);
}

结论

不管Join还是Contact,为了提高性能使用:

  1. 减少内存分配的次数
  2. 使用Span尽量避免生成临时的字符串.
  3. 使用运行时方法FastAllocateString进行了堆空间快速分配
  4. 使用栈内存,减少堆空间的分配,降低GC的压力

秋风 2021-08-11