Skip to content

Commit d180512

Browse files
Merge pull request #301 from Sergio0694/feature/high-performance
Microsoft.Toolkit.HighPerformance docs page(s)
2 parents cfc5fcc + 34abbeb commit d180512

File tree

6 files changed

+521
-0
lines changed

6 files changed

+521
-0
lines changed

docs/high-performance/Introduction.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
title: Introduction to the High Performance package
3+
author: Sergio0694
4+
description: An overview of how to get started with High Performance package and to the APIs it contains
5+
keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, get started, visual studio, high performance, net core, net standard
6+
---
7+
8+
# Introduction to the High Performance package
9+
10+
This package can be installed through NuGet, and it has the following multi-targets:
11+
- .NET Standard 1.4
12+
- .NET Standard 2.0
13+
- .NET Standard 2.1
14+
- .NET Core 2.1
15+
- .NET Core 3.1
16+
17+
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.
18+
19+
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.
20+
21+
Follow these steps to install the High Performance package:
22+
23+
1. Open an existing project in Visual studio, targeting any of the following:
24+
- UWP (>= 10.0)
25+
- .NET Standard (>= 1.4)
26+
- .NET Core (>= 1.0)
27+
- Any other framework supporting .NET Standard 1.4 and up
28+
29+
2. In Solution Explorer panel, right click on your project name and select **Manage NuGet Packages**. Search for **Microsoft.Toolkit.HighPerformance** and install it.
30+
31+
![NuGet Packages](../resources/images/ManageNugetPackages.png "Manage NuGet Packages Image")
32+
33+
3. Add a using directive in your C# files to use the new APIs:
34+
35+
```c#
36+
using Microsoft.Toolkit.HighPerformance;
37+
```
38+
39+
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.
40+
41+
## When should I use this package?
42+
43+
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.
44+
45+
This package makes heavy use of APIs such as:
46+
- [`System.Buffers.ArrayPool<T>`](https://docs.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1)
47+
- [`System.Runtime.CompilerServices.Unsafe`](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.unsafe)
48+
- [`System.Runtime.InteropServices.MemoryMarshal`](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.memorymarshal)
49+
- [`System.Threading.Tasks.Parallel`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel)
50+
51+
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!
52+
53+
## Where to start?
54+
55+
Here are some APIs you could look at first, if you were already using one of those types mentioned above:
56+
- [MemoryOwner<T>](MemoryOwner.md) and [SpanOwner<T>](SpanOwner.md), if you were using `System.Buffers.ArrayPool<T>`.
57+
- [ParallelHelper](ParallelHelper.md), if you were using `System.Threading.Tasks.Parallel`.

docs/high-performance/MemoryOwner.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: MemoryOwner&lt;T>
3+
author: Sergio0694
4+
description: A buffer type implementing `IMemoryOwner<T>` that rents memory from a shared pool
5+
keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, parallel, high performance, net core, net standard
6+
dev_langs:
7+
- csharp
8+
---
9+
10+
# MemoryOwner&lt;T>
11+
12+
The [MemoryOwner&lt;T>](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.highperformance.buffers.memoryowner-1) is a buffer type implementing [`IMemoryOwner<T>`](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<T>`](https://docs.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1) type, with some additional helper utilities.
13+
14+
## How it works
15+
16+
`MemoryOwner<T>` has the following main features:
17+
18+
- One of the main issues of arrays returned by the `ArrayPool<T>` APIs and of the `IMemoryOwner<T>` instances returned by the `MemoryPool<T>` 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<T>` solves this by also storing the original requested size, so that [`Memory<T>`](https://docs.microsoft.com/en-us/dotnet/api/system.memory-1) and [`Span<T>`](https://docs.microsoft.com/en-us/dotnet/api/system.span-1) instances retrieved from it will never need to be manually sliced.
19+
- When using `IMemoryOwner<T>`, getting a `Span<T>` for the underlying buffer requires first to get a `Memory<T>` instance, and then a `Span<T>`. This is fairly expensive, and often unnecessary, as the intermediate `Memory<T>` might actually not be needed at all. `MemoryOwner<T>` instead has an additional `Span` property which is extremely lightweight, as it directly wraps the internal `T[]` array being rented from the pool.
20+
- 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<T>` 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<T>`, or one that automatically clears the rented buffer.
21+
- 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<T>` 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.
22+
23+
## Syntax
24+
25+
Here is an example of how to rent a buffer and retrieve a `Memory<T>` instance:
26+
27+
```csharp
28+
// Be sure to include this using at the top of the file:
29+
using Microsoft.Toolkit.HighPerformance.Buffers;
30+
31+
using (MemoryOwner<int> buffer = MemoryOwner<int>.Allocate(42))
32+
{
33+
// Both memory and span have exactly 42 items
34+
Memory<int> memory = buffer.Memory;
35+
Span<int> span = buffer.Span;
36+
37+
// Writing to the span modifies the underlying buffer
38+
span[0] = 42;
39+
}
40+
```
41+
42+
In this example, we used a `using` block to declare the `MemoryOwner<T>` 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<T>` 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.
43+
44+
## When should this be used?
45+
46+
`MemoryOwner<T>` 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.
47+
48+
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:
49+
50+
```csharp
51+
public static byte[] GetBytesFromFile(string path)
52+
{
53+
using Stream stream = File.OpenRead(path);
54+
55+
byte[] buffer = new byte[(int)stream.Length];
56+
57+
stream.Read(buffer, 0, buffer.Length);
58+
59+
return buffer;
60+
}
61+
```
62+
63+
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:
64+
65+
```csharp
66+
public static (byte[] Buffer, int Length) GetBytesFromFile(string path)
67+
{
68+
using Stream stream = File.OpenRead(path);
69+
70+
byte[] buffer = ArrayPool<T>.Shared.Rent((int)stream.Length);
71+
72+
stream.Read(buffer, 0, (int)stream.Length);
73+
74+
return (buffer, (int)stream.Length);
75+
}
76+
```
77+
78+
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.
79+
80+
There are two main issues with the code above:
81+
- `ArrayPool<T>` 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.
82+
- 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<T>`](https://docs.microsoft.com/en-us/dotnet/api/system.buffers.memorypool-1) instead and by returning an `IMemoryOwner<T>` instance, but we still have the problem of rented buffers having a greater size than what we need. Additionally, `IMemoryOwner<T>` has some overhead when retrieving a `Span<T>` to work on, due to it being an interface, and the fact that we always need to get a `Memory<T>` instance first, and then a `Span<T>`.
83+
84+
To solve both these issues, we can refactor this code again by using `MemoryOwner<T>`:
85+
86+
```csharp
87+
public static MemoryOwner<byte> GetBytesFromFile(string path)
88+
{
89+
using Stream stream = File.OpenRead(path);
90+
91+
MemoryOwner<byte> buffer = MemoryOwner<byte>.Allocate((int)stream.Length);
92+
93+
stream.Read(buffer.Span);
94+
95+
return buffer;
96+
}
97+
```
98+
99+
The returned `IMemoryOwner<byte>` 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<T>` or `Span<T>` instance to interact with the loaded data, and then dispose the instance when we no longer need it. Additionally, all the `MemoryOwner<T>` properties (like `MemoryOwner<T>.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.
100+
101+
## Properties
102+
103+
| Property | Return Type | Description |
104+
| -- | -- | -- |
105+
| Length | int | Gets the number of items in the current instance |
106+
| Memory | System.Memory&lt;T> | Gets the memory belonging to this owner |
107+
| Span | System.Span&lt;T> | Gets a span wrapping the memory belonging to the current instance |
108+
| Empty | MemoryOwner&lt;T> | Gets an empty `MemoryOwner<T>` instance |
109+
110+
## Methods
111+
112+
| Method | Return Type | Description |
113+
| -- | -- | -- |
114+
| Allocate(int) | Memory&lt;T> | Creates a new `MemoryOwner<T>` instance with the specified parameters |
115+
| Allocate(int, AllocationMode) | Memory&lt;T> | Creates a new `MemoryOwner<T>` instance with the specified parameters |
116+
| DangerousGetReference() | ref T | Returns a reference to the first element within the current instance, with no bounds check |
117+
| Slice(int, int) | MemoryOwner&lt;T> | Slices the buffer currently in use and returns a new `MemoryOwner<T>` instance |
118+
119+
## Sample Code
120+
121+
You can find more examples in our [unit tests](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/UnitTests/UnitTests.HighPerformance.Shared/Buffers)
122+
123+
## Requirements
124+
125+
| Device family | Universal, 10.0.16299.0 or higher |
126+
| --- | --- |
127+
| Namespace | Microsoft.Toolkit.HighPerformance |
128+
| NuGet package | [Microsoft.Toolkit.HighPerformance](https://www.nuget.org/packages/Microsoft.Toolkit.HighPerformance/) |
129+
130+
## API
131+
132+
* [MemoryOwner&lt;T> source code](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/Microsoft.Toolkit.HighPerformance/Buffers)

0 commit comments

Comments
 (0)