新的C#Span< T>与ArraySegment< T>有何不同? [英] How is the new C# Span<T> different from ArraySegment<T>?

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

问题描述

我在概念化 C#中的新Span的用法时遇到了麻烦.

  1. 它将替换哪些构造?ArraySegment现在过时了吗?

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

  3. Span是C#数组的有效替代品吗?在什么情况下是,在什么情况下否?

  4. 我何时将使用ArraySegment代替Span?

我试图了解我的编码样式将需要进行更改以有效利用新的Span.

解决方案

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

ArraySegment< T> 仅限于托管数组.您不能使用它包装使用 stackalloc 在堆栈上分配的数据. Span< T> 允许您执行此操作.

ArraySegment< T> 也不提供对基础数组的只读视图. ReadOnlySpan< T> 为您提供了此功能.

Span< T> 不应替换数组.归根结底,这只是数据视图.必须以某种方式分配数据,在托管环境中,在大多数情况下,该分配将是数组分配.因此,您仍然需要数组.

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

Span< T> 绝对不会成为每个人都经常使用的东西.它是.NET框架的高度专业化的部分,主要用于库作者以及在性能非常关键的情况下.例如.Kestrel是ASP.NET Core背后的Web服务,通​​过迁移到 Span< T> 会获得很多性能优势,因为可以使用 Span< T> 和堆栈分配的内存来完成对请求的解析,这不会给GC带来任何压力.但是,您不必基于ASP.NET Core编写网站和服务.

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

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

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

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

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

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

解决方案

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&lt; T&gt;有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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