Modifier

Partager via


Microsoft.Toolkit.HighPerformance.Buffers Namespace

Classes

ArrayPoolBufferWriter<T>

Represents a heap-based, array-backed output sink into which T data can be written.

MemoryBufferWriter<T>

Represents an output sink into which T data can be written, backed by a Memory<T> instance.

MemoryOwner<T>

An IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor.

StringPool

A configurable pool for String instances. This can be used to minimize allocations when creating multiple String instances from buffers of Char values. The GetOrAdd(ReadOnlySpan<Char>) method provides a best-effort alternative to just creating a new String instance every time, in order to minimize the number of duplicated instances. The StringPool type will internally manage a highly efficient priority queue for the cached String instances, so that when the full capacity is reached, the least frequently used values will be automatically discarded to leave room for new values to cache.

Structs

SpanOwner<T>

A stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. This type mirrors MemoryOwner<T> but without allocations and with further optimizations. As this is a stack-only type, it relies on the duck-typed IDisposable pattern introduced with C# 8. It should be used like so:

using (SpanOwner<byte> buffer = SpanOwner<byte>.Allocate(1024))
{
    // Use the buffer here...
}

As soon as the code leaves the scope of that using block, the underlying buffer will automatically be disposed. The APIs in SpanOwner<T> rely on this pattern for extra performance, eg. they don't perform the additional checks that are done in MemoryOwner<T> to ensure that the buffer hasn't been disposed before returning a Memory<T> or Span<T> instance from it. As such, this type should always be used with a using block or expression. Not doing so will cause the underlying buffer not to be returned to the shared pool.

Interfaces

IBuffer<T>

An interface that expands IBufferWriter<T> with the ability to also inspect the written data, and to reset the underlying buffer to write again from the start.

Enums

AllocationMode

An enum that indicates a mode to use when allocating buffers.