新的 C# Span&lt;T&gt;与 ArraySegment<T> 不同? [英] How is the new C# Span&lt;T&gt; different from ArraySegment&lt;T&gt;?

查看:20
本文介绍了新的 C# Span&lt;T&gt;与 ArraySegment<T> 不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法概念化新的跨度<T>在 C# 中.

  1. 它取代了什么结构?是 ArraySegment现在已经过时了吗?

  2. 它启用了哪些以前没有的功能?

  3. 是否跨度C# 数组的有效替代品?在哪些情况下是,在哪些情况下不是?

  4. 我什么时候使用 ArraySegment;而不是跨度?

我试图了解我的编码风格需要如何改变才能有效利用新的 Span.

解决方案

Span 不替换任何内容.是增值的.它提供了对连续内存段的类型安全视图,这些内存段可以以多种不同的方式分配:作为托管数组、基于堆栈的内存或非托管内存.

ArraySegment 仅限于托管数组.您不能使用它来包装使用 stackalloc 在堆栈上分配的数据.Span 允许您这样做.

ArraySegment 也不提供底层数组的只读视图.ReadOnlySpan 为您提供.

Span 不应该替换数组.归根结底,它只是数据视图.该数据必须以某种方式分配,并且在托管世界中,在大多数情况下,该分配将是数组分配.所以你仍然需要数组.

如果您希望您的代码能够操作的不仅仅是数组,您应该使用 Span.例如.考虑一个解析库.现在,为了让它能够处理数组、堆栈分配的内存和非托管内存,它必须在 API 中为每一个提供多个入口点,并使用不安全的代码来实际操作数据.它还可能需要公开一个基于 string 的 API,供那些将数据分配为字符串的人使用.使用 SpanReadOnlySpan,您可以将所有逻辑合并到一个基于 Span 的解决方案中,该解决方案将适用于所有这些场景.>

Span 绝对不会是每个人都经常使用的东西.它是 .NET 框架的一个高度专业化的部分,主要对库作者和非常高性能的关键场景有用.例如.Kestrel,ASP.NET Core 背后的 Web 服务将通过移动到 Span 获得很多性能优势,因为例如可以使用 Span 和堆栈分配的内存来解析请求,这对 GC 没有压力.但是您,基于 ASP.NET Core 编写网站和服务,没有必要使用它.

I am having trouble conceptualizing the usages for the new Span<T> in C#.

  1. What construct(s) does it replace? Is ArraySegment<T> now obsolete?

  2. What functionality does it enable that previously was not?

  3. Is Span<T> a valid replacement for C# Arrays? In which cases yes, in which cases no?

  4. When will I use an ArraySegment<T> instead of a Span<T>?

I'm trying to understand how my coding styles will need to change to make effective use of the new Span<T>.

解决方案

Span<T> does not replace anything. It's value-added. It provides a type-safe view into continuous segments of memory which can be allocated in many different ways: either as a managed array, a stack-based memory or unmanaged memory.

ArraySegment<T> is limited to managed arrays. You can't use it to wrap data allocated on the stack using stackalloc. Span<T> allows you to do that.

ArraySegment<T> also does not provide a read-only view into the underlying array. ReadOnlySpan<T> gives you that.

Span<T> is not supposed to replace arrays. At the end of the day it's just a view into data. That data has to be allocated somehow, and in managed world that allocation, for most cases, will be an array allocation. So you still need arrays.

You should use Span<T> if you want your code to be able to manipulate more than just arrays. E.g. consider a parsing library. Right now, to allow it to work with arrays, stack-allocated memory and unmanaged memory, it has to provide multiple entry points in the API for each of these, and use unsafe code to actually manipulate the data. It also probably would need to expose a string-based API to be used by people who have their data allocated as strings. With Span and ReadOnlySpan you can merge all that logic to a single, Span-based solution which will be applicable in all these scenarios.

Span<T> is definitely not going to be something that's used by everybody and very often. It's a highly specialized part of .NET framework useful mostly to library authors and in very high performance critical scenarios. E.g. Kestrel, the web service behind ASP.NET Core will get a lot of performance benefits from moving to Span<T> because e.g. parsing the request can be done using Span<T> and stack-allocated memory, which puts no pressure on GC. But you, writing websites and services based on ASP.NET Core, don't necessary have to use it.

这篇关于新的 C# Span&lt;T&gt;与 ArraySegment<T> 不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆