Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the new C# Span<T> different from ArraySegment<T>?

Tags:

c#

.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>.

like image 483
TheFastCat Avatar asked Feb 28 '18 02:02

TheFastCat


People also ask

How much is the new Mercedes C-Class?

How Much Does the Mercedes-Benz C-Class Cost? The 2022 C-Class sedan starts at $43,550. The midtier Exclusive model starts at $45,800, and the top Pinnacle trim has a starting MSRP of $47,500. All-wheel drive is available in all models for an additional $2,000.

Is there a new C-Class coming out?

In short: Everything. Even though the new C-class's styling isn't a huge departure from the 2021 model, the 2022 model year marks the start of a new generation with more luxury and a greater number of high-tech features.

Is c180 a good car?

Is a used Mercedes C-Class saloon reliable? In our most recent reliability survey, the Mercedes C-Class finished at the bottom of the executive car class in 19th place out of a class of 19. A third of C-Classes went wrong, according to owners, who reported a wide range of issues.


2 Answers

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.

like image 153
MarcinJuraszek Avatar answered Oct 13 '22 21:10

MarcinJuraszek


From MSDN Magazine: Span is defined in such a way that operations can be as efficient as on arrays: indexing into a span doesn’t require computation to determine the beginning from a pointer and its starting offset, as the ref field itself already encapsulates both. (By contrast, ArraySegment has a separate offset field, making it more expensive both to index into and to pass around.)

Also, while ArraySegment implements IEnumerable, Span doesn't.

like image 34
Furkan Kambay Avatar answered Oct 13 '22 20:10

Furkan Kambay