diff --git a/docs/high-performance/Introduction.md b/docs/high-performance/Introduction.md new file mode 100644 index 000000000..b359b6e35 --- /dev/null +++ b/docs/high-performance/Introduction.md @@ -0,0 +1,57 @@ +--- +title: Introduction to the High Performance package +author: Sergio0694 +description: An overview of how to get started with High Performance package and to the APIs it contains +keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, get started, visual studio, high performance, net core, net standard +--- + +# Introduction to the High Performance package + +This package can be installed through NuGet, and it has the following multi-targets: +- .NET Standard 1.4 +- .NET Standard 2.0 +- .NET Standard 2.1 +- .NET Core 2.1 +- .NET Core 3.1 + +This means that you can use it from anything from UWP or legacy .NET Framework applications, games written in Unity, cross-platform mobile applications using Xamarin, to .NET Standard libraries and modern .NET Core 2.1 or .NET Core 3.1 applications. The API surface is almost identical in all cases, and lots of work has been put into backporting as many features as possible to older targets like .NET Standard 1.4 and .NET Standard 2.0. Except for some minor differences, you can expect the same APIs to be available on all target frameworks. + +The reason why multi-targeting has been used is to allow the package to leverage all the latest APIs on modern runtimes (like .NET Core 3.1) whenever possible, while still offering most of its functionalities to all target platforms. It also makes it possible for .NET Core 2.1 to use all the APIs that it has in common with .NET Standard 2.1, even though the runtime itself doesn't fully implement .NET Standard 2.1. All of this would not have been possible without multi-targeting to both .NET Standard as well as individual runtimes. + +Follow these steps to install the High Performance package: + +1. Open an existing project in Visual studio, targeting any of the following: + - UWP (>= 10.0) + - .NET Standard (>= 1.4) + - .NET Core (>= 1.0) + - Any other framework supporting .NET Standard 1.4 and up + +2. In Solution Explorer panel, right click on your project name and select **Manage NuGet Packages**. Search for **Microsoft.Toolkit.HighPerformance** and install it. + + ![NuGet Packages](../resources/images/ManageNugetPackages.png "Manage NuGet Packages Image") + +3. Add a using directive in your C# files to use the new APIs: + + ```c# + using Microsoft.Toolkit.HighPerformance; + ``` + +4. If you want so see some code samples, you can either read through the other docs pages for the High Performance package, or have a look at the various [unit tests](https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/UnitTests/UnitTests.HighPerformance.Shared) for the project. + +## When should I use this package? + +As the name suggests, the High Performance package contains a set of APIs that are heavily focused on optimization. All the new APIs have been carefully crafted to achieve the best possible performance when using them, either through reduced memory allocation, micro-optimizations at the assembly level, or by structuring the APIs in a way that facilitates writing performance oriented code in general. + +This package makes heavy use of APIs such as: +- [`System.Buffers.ArrayPool`](https://docs.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1) +- [`System.Runtime.CompilerServices.Unsafe`](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.unsafe) +- [`System.Runtime.InteropServices.MemoryMarshal`](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.memorymarshal) +- [`System.Threading.Tasks.Parallel`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel) + +If you are already familiar with these APIs or even if you're just getting started with writing high performance code in C# and want a set of well tested helpers to use in your own projects, have a look at what's included in this package to see how you can use it in your own projects! + +## Where to start? + +Here are some APIs you could look at first, if you were already using one of those types mentioned above: +- [MemoryOwner](MemoryOwner.md) and [SpanOwner](SpanOwner.md), if you were using `System.Buffers.ArrayPool`. +- [ParallelHelper](ParallelHelper.md), if you were using `System.Threading.Tasks.Parallel`. \ No newline at end of file diff --git a/docs/high-performance/MemoryOwner.md b/docs/high-performance/MemoryOwner.md new file mode 100644 index 000000000..5b1b5960f --- /dev/null +++ b/docs/high-performance/MemoryOwner.md @@ -0,0 +1,132 @@ +--- +title: MemoryOwner<T> +author: Sergio0694 +description: A buffer type implementing `IMemoryOwner` that rents memory from a shared pool +keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, parallel, high performance, net core, net standard +dev_langs: + - csharp +--- + +# MemoryOwner<T> + +The [MemoryOwner<T>](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.highperformance.buffers.memoryowner-1) is a buffer type implementing [`IMemoryOwner`](https://docs.microsoft.com/en-us/dotnet/api/system.buffers.imemoryowner-1), an embedded length property and a series of performance oriented APIs. It is essentially a lightweight wrapper around the [`ArrayPool`](https://docs.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1) type, with some additional helper utilities. + +## How it works + +`MemoryOwner` has the following main features: + +- One of the main issues of arrays returned by the `ArrayPool` APIs and of the `IMemoryOwner` instances returned by the `MemoryPool` APIs is that the size specified by the user is only being used as a _minum_ size: the actual size of the returned buffers might actually be greater. `MemoryOwner` solves this by also storing the original requested size, so that [`Memory`](https://docs.microsoft.com/en-us/dotnet/api/system.memory-1) and [`Span`](https://docs.microsoft.com/en-us/dotnet/api/system.span-1) instances retrieved from it will never need to be manually sliced. +- When using `IMemoryOwner`, getting a `Span` for the underlying buffer requires first to get a `Memory` instance, and then a `Span`. This is fairly expensive, and often unnecessary, as the intermediate `Memory` might actually not be needed at all. `MemoryOwner` instead has an additional `Span` property which is extremely lightweight, as it directly wraps the internal `T[]` array being rented from the pool. +- Buffers rented from the pool are not cleared by default, which means that if they were not cleared when being previous returned to the pool, they might contain garbage data. Normally, users are required to clear these rented buffers manually, which can be verbose especially when done frequently. `MemoryOwner` has a more flexible approach to this, through the `Allocate(int, AllocationMode)` API. This method not only allocates a new instance of exactly the requested size, but can also be used to specify which allocation mode to use: either the same one as `ArrayPool`, or one that automatically clears the rented buffer. +- There are cases where a buffer might be rented with a greater size than what is actually needed, and then resized afterwards. This would normally require users to rent a new buffer and copy the region of interest from the old buffer. Instead, `MemoryOwner` exposes a `Slice(int, int)` API that simply return a new instance wrapping the specified area of interest. This allows to skip renting a new buffer and copying the items entirely. + +## Syntax + +Here is an example of how to rent a buffer and retrieve a `Memory` instance: + +```csharp +// Be sure to include this using at the top of the file: +using Microsoft.Toolkit.HighPerformance.Buffers; + +using (MemoryOwner buffer = MemoryOwner.Allocate(42)) +{ + // Both memory and span have exactly 42 items + Memory memory = buffer.Memory; + Span span = buffer.Span; + + // Writing to the span modifies the underlying buffer + span[0] = 42; +} +``` + +In this example, we used a `using` block to declare the `MemoryOwner` buffer: this is particularly useful as the underlying array will automatically be returned to the pool at the end of the block. If instead we don't have direct control over the lifetime of a `MemoryOwner` instance, the buffer will simply be returned to the pool when the object is finalized by the garbage collector. In both cases, rented buffers will always be correctly returned to the shared pool. + +## When should this be used? + +`MemoryOwner` can be used as a general purpose buffer type, which has the advantage of minimizing the number of allocations done over time, as it internally reuses the same arrays from a shared pool. A common use case is to replace `new T[]` array allocations, especially when doing repeated operations that either require a temporary buffer to work on, or that produce a buffer as a result. + +Suppose we have a dataset consisting of a series of binary files, and that we need to read all these files and process them in some way. To properly separate our code, we might end up writing a method that simply reads one binary file, which might look like this: + +```csharp +public static byte[] GetBytesFromFile(string path) +{ + using Stream stream = File.OpenRead(path); + + byte[] buffer = new byte[(int)stream.Length]; + + stream.Read(buffer, 0, buffer.Length); + + return buffer; +} +``` + +Note that `new byte[]` expression. If we read a large number of files, we'll end up allocating a lot of new arrays, which will put a lot of pressure over the garbage collector. We might want to refactor this code using buffers rented from a pool, like so: + +```csharp +public static (byte[] Buffer, int Length) GetBytesFromFile(string path) +{ + using Stream stream = File.OpenRead(path); + + byte[] buffer = ArrayPool.Shared.Rent((int)stream.Length); + + stream.Read(buffer, 0, (int)stream.Length); + + return (buffer, (int)stream.Length); +} +``` + +Using this approach, buffers are now rented from a pool, which means that in most cases we're able to skip an allocation. Additionally, since rented buffers are not cleared by default, we can also save the time needed to fill them with zeros, which gives us another small performance improvement. In the example above, loading 1000 files would bring the total allocation size from around 1MB down to just 1024 bytes - just a single buffer would effectively be allocated, and then reused automatically. + +There are two main issues with the code above: +- `ArrayPool` might return buffers that have a size greater than the requested one. To work around this issue, we need to return a tuple which also indicates the actual used size into our rented buffer. +- By simply returning an array, we need to be extra careful to properly track its lifetime and to return it to the appropriate pool. We might work around this issue by using [`MemoryPool`](https://docs.microsoft.com/en-us/dotnet/api/system.buffers.memorypool-1) instead and by returning an `IMemoryOwner` instance, but we still have the problem of rented buffers having a greater size than what we need. Additionally, `IMemoryOwner` has some overhead when retrieving a `Span` to work on, due to it being an interface, and the fact that we always need to get a `Memory` instance first, and then a `Span`. + +To solve both these issues, we can refactor this code again by using `MemoryOwner`: + +```csharp +public static MemoryOwner GetBytesFromFile(string path) +{ + using Stream stream = File.OpenRead(path); + + MemoryOwner buffer = MemoryOwner.Allocate((int)stream.Length); + + stream.Read(buffer.Span); + + return buffer; +} +``` + +The returned `IMemoryOwner` instance will take care of disposing the underlying buffer and returning it to the pool when its [`IDisposable.Dispose`](https://docs.microsoft.com/en-us/dotnet/api/system.idisposable.dispose) method is invoked. We can use it to get a `Memory` or `Span` instance to interact with the loaded data, and then dispose the instance when we no longer need it. Additionally, all the `MemoryOwner` properties (like `MemoryOwner.Span`) respect the initial requested size we used, so we no longer need to manually keep track of the effective size within the rented buffer. + +## Properties + +| Property | Return Type | Description | +| -- | -- | -- | +| Length | int | Gets the number of items in the current instance | +| Memory | System.Memory<T> | Gets the memory belonging to this owner | +| Span | System.Span<T> | Gets a span wrapping the memory belonging to the current instance | +| Empty | MemoryOwner<T> | Gets an empty `MemoryOwner` instance | + +## Methods + +| Method | Return Type | Description | +| -- | -- | -- | +| Allocate(int) | Memory<T> | Creates a new `MemoryOwner` instance with the specified parameters | +| Allocate(int, AllocationMode) | Memory<T> | Creates a new `MemoryOwner` instance with the specified parameters | +| DangerousGetReference() | ref T | Returns a reference to the first element within the current instance, with no bounds check | +| Slice(int, int) | MemoryOwner<T> | Slices the buffer currently in use and returns a new `MemoryOwner` instance | + +## Sample Code + +You can find more examples in our [unit tests](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/UnitTests/UnitTests.HighPerformance.Shared/Buffers) + +## Requirements + +| Device family | Universal, 10.0.16299.0 or higher | +| --- | --- | +| Namespace | Microsoft.Toolkit.HighPerformance | +| NuGet package | [Microsoft.Toolkit.HighPerformance](https://www.nuget.org/packages/Microsoft.Toolkit.HighPerformance/) | + +## API + +* [MemoryOwner<T> source code](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/Microsoft.Toolkit.HighPerformance/Buffers) diff --git a/docs/high-performance/ParallelHelper.md b/docs/high-performance/ParallelHelper.md new file mode 100644 index 000000000..64e89727a --- /dev/null +++ b/docs/high-performance/ParallelHelper.md @@ -0,0 +1,118 @@ +--- +title: ParallelHelper +author: Sergio0694 +description: Helpers to work with parallel code in a highly optimized manner +keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, parallel, high performance, net core, net standard +dev_langs: + - csharp +--- + +# ParallelHelper + +The [ParallelHelper](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.highperformance.helpers.parallelhelper) contains high performance APIs to work with parallel code. It contains performance oriented methods that can be used to quickly setup and execute paralell operations over a given data set or iteration range or area. + +## How it works + +`ParallelHelper` is built around three main concepts: + +- It performs automatic batching over the target iteration range. This means that it automatically schedules the right number of working units based on the number of available CPU cores. This is done to reduce the overhead of invoking the parallel callback once for every single parallel iteration. +- It heavily leverages the way generic types are implemented in C#, and uses `struct` types implementing specific interfaces instead of delegates like `Action`. This is done so that the JIT compiler will be able to "see" each individual callback type being used, which allows it to inline the callback entirely, when possible. This can greatly reduce the overhead of each parallel iteration, especially when using very small callbacks, which would have a trivial cost with respect to the delegate invocation alone. Additionally, using a `struct` type as callback requires developers to manually handle variables that are being captured in the closure, which prevents accidental captures of the `this` pointer from instance methods and other values that could considerably slowdown each callback invocation. This is the same approach that is used in other performance-oriented libraries such as [`ImageSharp`](https://github.com/SixLabors/ImageSharp). +- It exposes 4 types of APIs that represent 4 different types of iterations: 1D and 2D loops, items iteration with side effect and items iteration without side effect. Each type of action has a corresponding `interface` type that needs to be applied to the `struct` callbacks being passed to the `ParallelHelper` APIs: these are `IAction`, `IAction2D`, `IRefAction` and `IInAction`. This helps developers to write code that is clearer regarding its intent, and allows the APIs to perform further optimizations internally. + +## Syntax + +Let's say we're interested in processing all the items in some `float[]` array, and to multiply each of them by `2`. In this case we don't need to capture any variables: we can just use the `IRefAction` `interface` and `ParallelHelper` will load each item to feed to our callback automatically. All that's needed is to define our callback, that will receive a `ref float` argument and perform the necessary operation: + +```csharp +// Be sure to include this using at the top of the file: +using Microsoft.Toolkit.HighPerformance.Helpers; + +// First declare the struct callback +public readonly struct ByTwoMultiplier : IRefAction +{ + public void Invoke(ref float x) => x *= 2; +} + +// Create an array and run the callback +float[] array = new float[10000]; + +ParallelHelper.ForEach(array); +``` + +With the `ForEach` API, we don't need to specify the iteration ranges: `ParallelHelper` will batch the collection and process each input item automatically. Furthermore, in this specific example we didn't even have to pass our `struct` as an argument: since it didn't contain any fields we needed to initialize, we could just specify its type as a type argument when invoking `ParallelHelper.ForEach`: that API will then create a new instance of that `struct` on its own, and use that to process the various items. + +To introduce the concept of closures, suppose we want to multiply the array elements by a value that is specified at runtime. To do so, we need to "capture" that value in our callback `struct` type. We can do that like so: + +```csharp +public readonly struct ItemsMultiplier : IRefAction +{ + private readonly float factor; + + public ItemsMultiplier(float factor) + { + this.factor = factor; + } + + public void Invoke(ref float x) => x *= this.factor; +} + +// ... + +ParallelHelper.ForEach(array, new ItemsMultiplier(3.14f)); +``` + +We can see that the `struct` now contains a field that represents the factor we want to use to multiply elements, instead of using a constant. And when invoking `ForEach`, we're explicitly creating an instance of our callback type, with the factor we're interested in. Furthermore, in this case the C# compiler is also able to automatically recognize the type arguments we're using, so we can omit them together from the method invocation. + +This approach of creating fields for values we need to access from a callback lets us explicitly declare what values we want to capture, which helps makes the code more expressive. This is exactly the same thing that the C# compiler does behind the scenes when we declare a lambda function or local function that accesses some local variable as well. + +Here is another example, this time using the `For` API to initialize all the items of an array in parallel. Note how this time we're capturing the target array directly, and we're using the `IAction` `interface` for our callback, which gives our method the current parallel iteration index as argument: + +```csharp +public readonly struct ArrayInitializer : IAction +{ + private int[] array; + + public ArrayInitializer(int[] array) + { + this.array = array; + } + + public void Invoke(int i) + { + this.array[i] = i; + } +} + +// ... + +ParallelHelper.For(0, array.Length, new ArrayInitializer(array)); +``` + +> [!NOTE] +> Since the callback types are `struct`-s, they're passed _by copy_ to each thread running parallel, not by reference. This means that value types being stored as fields in a callback types will be copied as well. A good practice to remember that detail and avoid errors is to mark the callback `struct` as `readonly`, so that the C# compiler will not let us modify the values of its fields. This only applies to _instance_ fields of a value type: if a callback `struct` has a `static` field of any type, or a reference field, then that value will correctly be shared between parallel threads. + +## Methods + +These are the 4 main APIs exposed by `ParallelHelper`, corresponding to the `IAction`, `IAction2D`, `IRefAction` and `IInAction` interfaces. The `ParallelHelper` type also exposes a number of overloads for these methods, that offer a number of ways to specify the iteration range(s), or the type of input callback. + +| Method | Return Type | Description | +| -- | -- | -- | +| For<TAction>(int, int, in TAction) | void | Executes a specified action in an optimized parallel loop | +| For2D<TAction>(int, int, int, int, in TAction) | void | Executes a specified action in an optimized parallel loop | +| ForEach<TItem,TAction>(Memory, in TAction) | void | Executes a specified action in an optimized parallel loop over the input data | +| ForEach<TItem,TAction>(ReadOnlyMemory, in TAction) | void | Executes a specified action in an optimized parallel loop over the input data | + +## Sample Code + +You can find more examples in our [unit tests](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/UnitTests/UnitTests.HighPerformance.Shared/Helpers) + +## Requirements + +| Device family | Universal, 10.0.16299.0 or higher | +| --- | --- | +| Namespace | Microsoft.Toolkit.HighPerformance | +| NuGet package | [Microsoft.Toolkit.HighPerformance](https://www.nuget.org/packages/Microsoft.Toolkit.HighPerformance/) | + +## API + +* [ParallelHelper source code](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/Microsoft.Toolkit.HighPerformance/Helpers) diff --git a/docs/high-performance/Ref.md b/docs/high-performance/Ref.md new file mode 100644 index 000000000..21299cd35 --- /dev/null +++ b/docs/high-performance/Ref.md @@ -0,0 +1,112 @@ +--- +title: Ref<T> and ReadOnlyRef<T> +author: Sergio0694 +description: Two stack-only types that can store a reference to a value of a specified type +keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, parallel, high performance, net core, net standard +dev_langs: + - csharp +--- + +# Ref<T> + +The [Ref<T>](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.highperformance.ref-1) is a stack-only type that can store a reference to a value of a specified type. It is semantically equivalent to a `ref T` value, with the difference that it can also be used as a type of field in another stack-only `struct` type. It can be used in place of proper `ref T` fields, which are currently not supported in C#. + +## How it works + +> [!NOTE] +> Due to how it's implemented on different .NET Standard contracts, the `Ref` has some minor differences on .NET Standard 1.4 and .NET Standard 2.1 (and equivalent .NET Core runtimes). In particular, on .NET Standard 1.4 it can only be used to reference fields _within an object_, instead of arbitrary values pointed by a managed reference. This is because the underlying APIs being used on .NET Standard 1.4 don't have built-in support for the `Span` type. + +### `Ref` on .NET Standard 1.4 + +As mentioned before, on .NET Standard 1.4, `Ref` can only point to locations within a given `object`. Consider the following code: + +```csharp +// Be sure to include this using at the top of the file: +using Microsoft.Toolkit.HighPerformance; + +// Define a sample model +class MyModel +{ + public int Number = 42; +} + +var model = new MyModel(); + +// Create a Ref pointing to the MyModel.Number field +Ref byRef = new Ref(model, ref model.Number); + +// Modify the field indirectly +byRef.Value++; + +Console.WriteLine(model.Number); // Prints 43! +``` + +> [!WARNING] +> The `Ref` constructor doesn't validate the input arguments, meaning that it is your responsability to pass a valid `object` and a reference to a field within that object. Failing to do so might cause the `Ref.Value` property to return an invalid reference. + +### `Ref` on .NET Standard 2.1 + +On .NET Standard 2.1, `Ref` can point to any `ref T` value: + +```csharp +int number = 42; + +// Create a Ref pointing to 'number' +Ref byRef1 = new Ref(ref number); + +// Modify 'number' +byRef1.Value++; + +Console.WriteLine(number); // Prints 43! +``` + +> [!WARNING] +> This type comes with a few caveats and should be used carefully, as it can lead to runtime crashes if a `Ref` instance is created with an invalid reference. In particular, you should only create a `Ref` instance pointing to values that have a lifetime that is greater than that of the `Ref` in use. Consider the following snippet: + +```csharp +public static ref int GetDummyReference() +{ + int number = 42; + + Ref byRef = new Ref(ref number); + + return ref byRef.Value; +} +``` + +This will compile and run fine, but the returned `ref int` will be invalid (as in, it will not point to a valid location) and could cause crashes if it's dereferenced. It is your responsability to track the lifetime of values being referenced by new `Ref` values. + +> [!NOTE] +> Although it is possible to create a `Ref` value wrapping a `null` reference, by using the `default(Ref)` expression, the `Ref` type is not designed to be used with nullable references and does not include proper features to validate the internal reference. If you need to return a reference that can be set to `null`, use the `NullableRef` and `NullableReadOnlyRef` types. + +## Properties + +| Property | Return Type | Description | +| -- | -- | -- | +| Value | ref T | Gets the `T` reference represented by the current `Ref{T}` instance | + +# ReadOnlyRef<T> + +The [ReadOnlyRef<T>](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.highperformance.readonlyref-1) is a stack-only type that mirrors `Ref`, with the exception that its constructor takes an `in T` parameter (a readonly reference), instead of a `ref T` one. Similarly, its `Value` property has a `ref readonly T` return type instead of `ref T`. + +## Properties + +| Property | Return Type | Description | +| -- | -- | -- | +| Value | ref readonly T | Gets the `T` reference represented by the current `ReadOnlyRef{T}` instance | + +## Sample Code + +You can find more examples in our [unit tests](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/UnitTests/UnitTests.HighPerformance.Shared) + +## Requirements + +| Device family | Universal, 10.0 or higher | +| --- | --- | +| Namespace | Microsoft.Toolkit.HighPerformance | +| NuGet package | [Microsoft.Toolkit.HighPerformance](https://www.nuget.org/packages/Microsoft.Toolkit.HighPerformance/) | + +## API + +* [Ref<T> source code](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/Microsoft.Toolkit.HighPerformance) +* [ReadOnlyRef<T> source code](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/Microsoft.Toolkit.HighPerformance) diff --git a/docs/high-performance/SpanOwner.md b/docs/high-performance/SpanOwner.md new file mode 100644 index 000000000..8f7625a56 --- /dev/null +++ b/docs/high-performance/SpanOwner.md @@ -0,0 +1,94 @@ +--- +title: Spanowner<T> +author: Sergio0694 +description: A stack-only buffer type renting memory from a shared pool +keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, parallel, high performance, net core, net standard +dev_langs: + - csharp +--- + +# SpanOwner<T> + +The [SpanOwner<T>](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.highperformance.buffers.spanowner-1) is a stack-only buffer type that rents buffers from a shared memory pool. It essentially mirrors the functionality of `MemoryOwner`, but as a `ref struct` type. This is particularly useful for short-lived buffers that are only used in synchronous code (that don't require `Memory` instances), as well as code running in a tight loop, as creating `SpanOwner` values will not require memory allocations at all. + +## Syntax + +The same core features of `MemoryOwner` apply to this type as well, with the exception of it being a stack-only `struct`, and the fact that it lacks the `IMemoryOwner` `interface` implementation, as well as the `Memory` property. The syntax is virtually identical to that used with `MemoryOwner` as well, except for the differences mentioned above. + +As an example, suppose we have a method where we need to allocate a temporary buffer of a specified size (let's call this value `length`), and then use it to perform some work. A first, inefficient version might look like this: + +```csharp +byte[] buffer = new byte[length]; + +// Use buffer here +``` + +This is not ideal, as we're allocating a new buffer every time this code is used, and then throwing it away immediately (as mentioned in the `MemoryOwner` docs as well), which puts more pressure on the garbage collector. We can optimize the code above by using [`ArrayPool`](https://docs.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1): + +```csharp +// Using directive to access the ArrayPool type +using System.Buffers; + +int[] buffer = ArrayPool.Shared.Rent(length); + +try +{ + // Slice the span, as it might be larger than the requested size + Span span = buffer.AsSpan(0, length); + + // Use the span here +} +finally +{ + ArrayPool.Shared.Return(buffer); +} +``` + +The code above rents a buffer from an array pool, but it's more verbose and error-prone: we need to be careful with the `try/finally` block to make sure to always return the rented buffer to the pool. We can rewrite it by using the `SpanOwner` type, like so: + +```csharp +// Be sure to include this using at the top of the file: +using Microsoft.Toolkit.HighPerformance.Buffers; + +using SpanOwner buffer = SpanOwner.Allocate(length); + +Span span = buffer.Span; + +// Use the span here, no slicing necessary +``` + +The `SpanOwner` instance will internally rent an array, and will take care of returning it to the pool when it goes out of scope. We no longer need to use a `try/finally` block either, as the C# compiler will add that automatically when expanding that `using` statement. As such, the `SpanOwner` type can be seen as a lightweight wrapper around the `ArrayPool` APIs, which makes them both more compact and easier to use, reducing the amount of code that needs to be written to properly rent and dispose short lived buffers. You can see how using `SpanOwner` makes the code much shorter and more straightforward. + +> [!NOTE] +> As this is a stack-only type, it relies on the duck-typed `IDisposable` pattern introduced with C# 8. That is shown in the sample above: the `SpanOwner` type is being used within a `using` block despite the fact that the type doesn't implement the `IDisposable` interface at all, and it's also never boxed. The functionality is just the same: as soon as the buffer goes out of scope, it is automatically disposed. The APIs in `SpanOwner{T}` rely on this pattern for extra performance: they assume that the underlying buffer will never be disposed as long as the `SpanOwner` type is in scope, and they don't perform the additional checks that are done in `MemoryOwner` to ensure that the buffer is in fact still available before returning a `Memory` or `Span` 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. Technically the same can also be achieved by manually calling `Dispose` on the `SpanOwner` type (which doesn't require C# 8), but that is error prone and hence not recommended. + +## Properties + +| Property | Return Type | Description | +| -- | -- | -- | +| Length | int | Gets the number of items in the current instance | +| Span | System.Span<T> | Gets a span wrapping the memory belonging to the current instance | +| Empty | SpanOwner<T> | Gets an empty `SpanOwner` instance | + +## Methods + +| Method | Return Type | Description | +| -- | -- | -- | +| Allocate(int) | Memory<T> | Creates a new `SpanOwner` instance with the specified parameters | +| Allocate(int, AllocationMode) | Memory<T> | Creates a new `SpanOwner` instance with the specified parameters | +| DangerousGetReference() | ref T | Returns a reference to the first element within the current instance, with no bounds check | + +## Sample Code + +You can find more examples in our [unit tests](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/UnitTests/UnitTests.HighPerformance.Shared/Buffers) + +## Requirements + +| Device family | Universal, 10.0.16299.0 or higher | +| --- | --- | +| Namespace | Microsoft.Toolkit.HighPerformance | +| NuGet package | [Microsoft.Toolkit.HighPerformance](https://www.nuget.org/packages/Microsoft.Toolkit.HighPerformance/) | + +## API + +* [SpanOwner<T> source code](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/Microsoft.Toolkit.HighPerformance/Buffers) diff --git a/docs/toc.md b/docs/toc.md index 1c9c0cf54..eaea76ca2 100644 --- a/docs/toc.md +++ b/docs/toc.md @@ -164,6 +164,14 @@ ## [MarkdownParser](parsers/MarkdownParser.md) ## [RSSParser](parsers/RSSParser.md) +# High performance +## [Introduction](high-performance/Introduction.md) +## Buffers +### [MemoryOwner](high-performance/MemoryOwner.md) +### [SpanOwner](high-performance/SpanOwner.md) +## [ParallelHelper](high-performance/ParallelHelper.md) +## [Ref and ReadOnlyRef](high-performance/Ref.md) + # Developer tools ## [AlignmentGrid](developer-tools/AlignmentGrid.md) ## [FocusTracker](developer-tools/FocusTracker.md)