Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alanmcgovern/reusabletasks
An allocation free Task-like object for async methods
https://github.com/alanmcgovern/reusabletasks
async await csharp dotnet performance task threading
Last synced: 3 months ago
JSON representation
An allocation free Task-like object for async methods
- Host: GitHub
- URL: https://github.com/alanmcgovern/reusabletasks
- Owner: alanmcgovern
- License: other
- Created: 2019-10-28T17:19:25.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-30T07:41:28.000Z (7 months ago)
- Last Synced: 2024-10-14T09:16:57.195Z (3 months ago)
- Topics: async, await, csharp, dotnet, performance, task, threading
- Language: C#
- Homepage: https://github.com/alanmcgovern/ReusableTasks/wiki
- Size: 146 KB
- Stars: 38
- Watchers: 6
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ReusableTask
[![NuGet version](https://badge.fury.io/nu/reusabletasks.svg)](https://badge.fury.io/nu/reusabletasks)
[![Build status](https://dev.azure.com/alanmcgovern0144/ReusableTasks/_apis/build/status/ReusableTasks-CI)](https://dev.azure.com/alanmcgovern0144/ReusableTasks/_build/latest?definitionId=3)A .NET Standard 2.0 compatible library which can be used to implement zero allocation async/await. This is conceptually similar to `ValueTask`, except it's compatible with .NET 2.0 and has zero ongoing allocations once the internal cache initializes.
Sample usage:
```
public async ReusableTask InitializeAsync ()
{
if (!Initialized) {
await LongInitializationAsync ();
Initialized = true;
}
}async ReusableTask LongInitializationAsync ()
{
// Contact a database, load from a file, etc
}public async ReusableTask CreateMyDataAsync ()
{
if (!Initialized) {
mydata = await LongMyDataCreationAsync ();
Initialized = true;
}
return mydata;
}async ReusableTask LongMyDataCreationAsync ()
{
// Contact a database, load from a file, etc..
return new MyData (....);
}
```
The compiler generated async state machine for these methods is allocation-free, and results in no garbage collectable objects being created no matter how many times the methods are invoked.The four things you cannot do with `ValueTask` you also cannot do with `ReusableTask`. The documentation can be read here in the remarks section, https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.valuetask-1?view=netcore-3.0#remarks.
Unlike the documentation states for `ValueTask`, I would recommend that the default return value for any async method should be `ReusableTask` or `ReusableTask`, unless benchmarking shows otherwise.