gpt4 book ai didi

.net - 在 C# 中连接 ReadOnlySpan 的最快方法

转载 作者:行者123 更新时间:2023-12-02 06:22:46 27 4
gpt4 key购买 nike

如果我已经只有 ReadOnlySpan 切片,那么连接字符串的最有效方法是什么?

简化示例:

public class Program {
public string ConcatSpans(string longstring) {
var span = longstring.AsSpan();
var sb = new StringBuilder(longstring.Length);
sb.Append(span.Slice(40, 10));
sb.Append(span.Slice(30, 10));
sb.Append(span.Slice(20, 10));
sb.Append(span.Slice(10, 10));
sb.Append(span.Slice(0, 10));
return sb.ToString();
}

[Benchmark]
public void ConcatSpansBenchmark() {
ConcatSpans("aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeee");
}

public static void Main(string[] args) {
var summary = BenchmarkRunner.Run<Program>();
}
}

结果:

BenchmarkDotNet=v0.11.2, OS=Windows 10.0.17134.345 (1803/April2018Update/Redstone4)
Intel Core i5-2500K CPU 3.30GHz (Sandy Bridge), 1 CPU, 4 logical and 4 physical cores
.NET Core SDK=2.1.403
[Host] : .NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT
DefaultJob : .NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT


Method | Mean | Error | StdDev | Gen 0/1k Op | Gen 1/1k Op | Gen 2/1k Op | Allocated Memory/Op |
--------------------- |---------:|---------:|---------:|------------:|------------:|------------:|--------------------:|
ConcatSpansBenchmark | 126.6 ns | 1.712 ns | 1.601 ns | 0.0966 | - | - | 304 B |

StringBuilder我们真的能做到最好吗?有没有比这更快的方法?分配甚至更少?毕竟StringBuilder对象本身是一个堆对象。

如果有一个ref struct StringBuilder这只会保留对 ReadOnlySpans 的引用并在决赛中ToString只分配一个字符串对象?

最佳答案

编辑:如 Tseng notes in the comments ,较新的 string.Create 方法是在它存在的平台上执行此操作的方法。

<小时/>

具有多个(但已知)输入跨度的场景非常适合“预分配虚拟字符串,然后假装字符串是可变的并在将其暴露给世界之前覆盖它”场景。这看起来很粗糙,但这个技巧在处理字符串(尤其是来自不连续缓冲区等)的 IO 代码中非常常见,因此它很好理解和支持。

开始(编辑:现在添加了“混合”方法,可以避免所有 Slice() 调用,而不需要 不安全):

                        Method |     Mean |     Error |    StdDev |   Median |
------------------------------ |---------:|----------:|----------:|---------:|
ConcatSpansBenchmark | 97.17 ns | 2.1335 ns | 4.0072 ns | 97.20 ns |
OverwiteStringBenchmark | 63.34 ns | 1.2914 ns | 2.0854 ns | 62.29 ns |
UnsafeOverwriteBenchmark | 17.95 ns | 0.3697 ns | 0.3796 ns | 17.80 ns |
OverwiteStringHybridBenchmark | 53.59 ns | 0.5534 ns | 0.5176 ns | 53.49 ns |

注意:任何涉及 MemoryMarshal.*Unsafe.*unsafe 关键字的内容都明确表示“我知道我在做什么。 ..任何爆炸的事情都可能是我的错”。

代码:

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

public class Program
{
public string ConcatSpans(string longstring)
{
var span = longstring.AsSpan();
var sb = new StringBuilder(longstring.Length);
sb.Append(span.Slice(40, 10));
sb.Append(span.Slice(30, 10));
sb.Append(span.Slice(20, 10));
sb.Append(span.Slice(10, 10));
sb.Append(span.Slice(0, 10));
return sb.ToString();
}

public string OverwiteString(string longstring)
{
var span = longstring.AsSpan();
var s = new string('\0', longstring.Length);
var writeable = MemoryMarshal.AsMemory(s.AsMemory()).Span;
span.Slice(40, 10).CopyTo(writeable);
writeable = writeable.Slice(10);
span.Slice(30, 10).CopyTo(writeable);
writeable = writeable.Slice(10);
span.Slice(20, 10).CopyTo(writeable);
writeable = writeable.Slice(10);
span.Slice(10, 10).CopyTo(writeable);
writeable = writeable.Slice(10);
span.Slice(0, 10).CopyTo(writeable);
return s;
}

public string OverwiteStringHybrid(string longstring)
{
var source = MemoryMarshal.AsBytes(MemoryMarshal.AsMemory(longstring.AsMemory()).Span);
var s = new string('\0', longstring.Length);
var target = MemoryMarshal.AsBytes(MemoryMarshal.AsMemory(s.AsMemory()).Span);

Unsafe.CopyBlock(ref target[0], ref source[40 * sizeof(char)], 10 * sizeof(char));
Unsafe.CopyBlock(ref target[10], ref source[30 * sizeof(char)], 10 * sizeof(char));
Unsafe.CopyBlock(ref target[20], ref source[20 * sizeof(char)], 10 * sizeof(char));
Unsafe.CopyBlock(ref target[30], ref source[10 * sizeof(char)], 10 * sizeof(char));
Unsafe.CopyBlock(ref target[40], ref source[0], 10 * sizeof(char));

return s;
}

public unsafe string UnsafeOverwrite(string longstring)
{
var s = new string('\0', longstring.Length);
fixed (char* source = longstring)
fixed (char* target = s)
{
Unsafe.CopyBlock(target, source + 40, 10 * sizeof(char));
Unsafe.CopyBlock(target + 10, source + 30, 10 * sizeof(char));
Unsafe.CopyBlock(target + 20, source + 20, 10 * sizeof(char));
Unsafe.CopyBlock(target + 30, source + 10, 10 * sizeof(char));
Unsafe.CopyBlock(target + 40, source, 10 * sizeof(char));
}
return s;
}

[Benchmark]
public void ConcatSpansBenchmark()
=> ConcatSpans("aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeee");
[Benchmark]
public void OverwiteStringBenchmark()
=> OverwiteString("aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeee");
[Benchmark]
public void UnsafeOverwriteBenchmark()
=> UnsafeOverwrite("aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeee");

[Benchmark]
public void OverwiteStringHybridBenchmark()
=> OverwiteStringHybrid("aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeee");

public static void Main(string[] args)
=> BenchmarkRunner.Run<Program>();
}

注意:在一般情况下 - 从切片获取不安全代码:

使用 C# 7.3:

fixed(char* p = theSpan)
{
...
}

否则:

fixed(char* p = &MemoryMarshal.GetReference(theSpan))
{

}

关于.net - 在 C# 中连接 ReadOnlySpan<char> 的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53180372/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com