span and memory under. Net

Source: Internet
Author: User

The play of. NET Core 2.1 is performance, and the most important of the two classes is span and memory, and here's a brief introduction to the use of these two classes.

What is span<t>

Span<t> is a new type of new value. It represents a contiguous area, which is usually associated with an array, representing a portion of the memory in a set.

var???????? Arr??? =? new? BYTE[10];
Span<byte>?bytes?=?arr;

You can also take part of the array:

var? bytes?=? New? Span<byte> (arr,?3,?5);

At first glance,span<t> and arraysegment<t> are very similar, but span is much more powerful, it can be used not only to separate arrays, but also to reference the data on the stack.

Span<byte>?bytes?=? stackalloc? BYTE[2];

You can also reference pointer data,

Span<byte>?bytes;
Unsafe? {? Bytes?=? new? Span<byte> ((byte*)ptr,? 1);?}

In addition, Span also supports the reinterpret_cast idea that you can cast span<byte> to Span<int>, and use,span<t> with Memorymarshal classes Most of the time, you can replace the pointer.

In addition to being more powerful, span has gained more support in the BCL library, and most of the functions that support arrays now basically support span directly, such as:

var. inputspan?=?input. Asspan();
int? First????? =? int. Parse(Inputspan. Slice(3,?5));

In this function, int. The parse function can support span directly, and because it does not produce substrings, it performs better than using the substring method.

In addition, the system supports implicit conversion of array type to span, and also provides a asspan display extension method, which facilitates the conversion of array types to span.

In addition to being powerful, the performance of span is also very high, and the operation of span is essentially as high as accessing an array, eliminating the need for calculations to determine the beginning of the pointer and its starting offset, because the reference field itself is encapsulated. In contrast,,arraysegment<t> has a separate offset field, which increases the cost of indexing and data transfer operations.

What is memory<t>

Span<t> Although powerful and useful, but it can only exist on the stack, and can not exist on the heap, the main reasons are as follows two points:

    1. span contains the Reference field (the beginning of an array), which is referred to as an "internal pointer." For the. NET Runtime's garbage collector, tracking these pointers is a relatively expensive operation. Therefore, the runtime constrains these references to exist only on the stack, because it implicitly specifies the minimum number of internal pointers that can be present.
    2. Read and write operations performed on Span are not atomic operations. There is a "rip" risk if multiple threads perform read and write operations on the fields on the heap at the same time.

This restriction determines that Span cannot be boxed and cannot be used in conjunction with an existing reflection call API or as a generic parameter. \ span<t>

This does not have much impact on most of the synchronization functions, but because span<t> cannot be stored in the heap, it cannot be used in an asynchronous context. To solve this problem,. NET introduces a new type of memory<t>.

Memory and span are used in a similar way,

var. Arr??? =? new? BYTE[10];
var? bytes?=? New? Memory<byte> (arr,?3,?5);

The difference is that memory<t> is not similar to a reference structure that can exist on the heap. The. NET BCL Library supports Memory as well, such as Stream.readasync, which can support memory<byte> directly as a parameter.

In addition, you can create a span from the memory span property that points to that memory, so you can also use the powerful features of span.

Reference article:

C #-Span Comprehensive Introduction: Exploring the important components of. NET Additions

span and memory under. Net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.