网站首页 > 博客文章 正文
起因
在更新.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;
}
T currentValue = en.Current;
string? firstString = currentValue?.ToString(); //如果调用类型的ToString方法
if (!en.MoveNext())
{
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;
//计算List或者数组中的每个字符串的长度
foreach (string? value in values)
{
if (value != null)
{
totalLength += value.Length;
if (totalLength < 0) // Check for overflow
{
ThrowHelper.ThrowOutOfMemoryException();
}
}
}
//根据计算出的总长度进行内存分配, FastAllocateString是运行时方法,不是c#编写
string result = FastAllocateString(totalLength);
int copiedLength = 0;
for (int i = 0; i < values.Length; i++)
{
if (values[i] is string value)
{
int valueLen = value.Length;
if (valueLen > totalLength - copiedLength)
{
copiedLength = -1;
break;
}
//1. 将List或者数组的内容填充,分配好的result中
FillStringChecked(result, copiedLength, value);
copiedLength += valueLen;
}
//2.将分隔符填充到result
if (i < values.Length - 1)
{
//关于Unsafe <<在.Net Core中使用Span>>有提到
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;
}
}
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;
}
//这里依然使用栈,这里先不说ValueStringBuilder
var result = new ValueStringBuilder(stackalloc char[256]);
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;
}
// 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;
}
//根据计算出的所有字符串的长度,分配内存空间
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;
}
}
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,为了提高性能使用:
- 减少内存分配的次数
- 使用Span尽量避免生成临时的字符串.
- 使用运行时方法FastAllocateString进行了堆空间快速分配
- 使用栈内存,减少堆空间的分配,降低GC的压力
猜你喜欢
- 2024-10-15 Python 速度慢,试试这个方法提高 1000 倍
- 2024-10-15 C# 文件操作浅析(c#代码文件)
- 2024-10-15 从零开始自学C#基础的第十五天——数组的基本用法
- 2024-10-15 浅谈C#取消令牌CancellationTokenSource
- 2024-10-15 总结了才知道,原来channel有这么多用法
- 2024-10-15 面向对象(8-15)异常类-C#编程零基础到入门学习
- 2024-10-15 .NET 6 中 LINQ 的改进(.net 调优)
- 2024-10-15 Log4net配置文件 C#(c# log4j)
- 2024-10-15 C# BIN文件读取以及CRC校验(匹配STM32F103)
- 2024-10-15 .NET 中的值对象(DDD 基础知识)(.net的数据类型)
你 发表评论:
欢迎- 最近发表
-
- 给3D Slicer添加Python第三方插件库
- Python自动化——pytest常用插件详解
- Pycharm下安装MicroPython Tools插件(ESP32开发板)
- IntelliJ IDEA 2025.1.3 发布(idea 2020)
- IDEA+Continue插件+DeepSeek:开发者效率飙升的「三体组合」!
- Cursor:提升Python开发效率的必备IDE及插件安装指南
- 日本旅行时想借厕所、买香烟怎么办?便利商店里能解决大问题!
- 11天!日本史上最长黄金周来了!旅游万金句总结!
- 北川景子&DAIGO缘定1.11 召开记者会宣布结婚
- PIKO‘PPAP’ 洗脑歌登上美国告示牌
- 标签列表
-
- ifneq (61)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)