MemoizR.StructuredConcurrency 0.1.1-rc.3

This is a prerelease version of MemoizR.StructuredConcurrency.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package MemoizR.StructuredConcurrency --version 0.1.1-rc.3
NuGet\Install-Package MemoizR.StructuredConcurrency -Version 0.1.1-rc.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MemoizR.StructuredConcurrency" Version="0.1.1-rc.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MemoizR.StructuredConcurrency --version 0.1.1-rc.3
#r "nuget: MemoizR.StructuredConcurrency, 0.1.1-rc.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MemoizR.StructuredConcurrency as a Cake Addin
#addin nuget:?package=MemoizR.StructuredConcurrency&version=0.1.1-rc.3&prerelease

// Install MemoizR.StructuredConcurrency as a Cake Tool
#tool nuget:?package=MemoizR.StructuredConcurrency&version=0.1.1-rc.3&prerelease

MemoizR: Simplifying Structured Concurrency in .NET

"The world is still short on languages that deal super elegantly and inherently and intuitively with concurrency" Mads Torgersen Lead Designer of C# (https://www.youtube.com/watch?v=Nuw3afaXLUc&t=4402s)

MemoizR is a powerful Structured Concurrency State implementation designed to simplify and enhance state synchronization across multiple threads in .NET applications. It offers a performant and thread-safe approach to managing concurrency challenges, making it an excellent choice for various scenarios.

Key Advantages

  • Simplicity: MemoizR aims to provide a straightforward and intuitive way to handle concurrency, avoiding the complexities often associated with async/await patterns (e.g. async void, .Wait, not awaiting everything and even .ConfigureAwait) in C#. It offers a more natural approach to managing asynchronous tasks and multithreading.

  • Declarative Structured Concurrency: This represents the most innovative aspect of this library, allowing for straightforward configuration, effortless maintenance, robust error handling, and seamless cancellation of intricate concurrency scenarios. Moreover, there's potential for even greater benefits on the horizon, such as concurrent resource management. All of this within the framework of an intuitive mental model for tackling complex concurrent systems.

  • Scalability: This concurrency model has the potential for expansion into distributed setups, similar to the actor model. It can help you build scalable and distributed systems with ease.

  • Maintainable Code: MemoizR is designed to ensure the maintainability of your code, especially when dealing with challenging concurrent state synchronization problems in multi-threaded environments.

  • Performance Optimization: Even for simple use cases, MemoizR can optimize performance in scenarios where there are more reads than writes (thanks to memoization) or more writes than reads (using lazy evaluation).

Dynamic Lazy Memoization

With MemoizR, you can create a dependency graph that performs dynamic lazy memoization. This means that values are calculated only when needed and only if they haven't been calculated before (memoization). It also ensures efficient resource utilization and reduces unnecessary calculations (lazy). This implementation draws inspiration from the concepts found in reactively (https://github.com/modderme123/reactively)

Inspiration

MemoizR draws inspiration from various sources:

Benefits

Here are some key benefits of using MemoizR:

  • Dependency Tracking: MemoizR automatically tracks dependencies between functions and methods, eliminating the need for manual source listing. This ensures that calculations are triggered only when necessary.

  • Optimization: MemoizR includes intelligent optimization algorithms. Functions are executed only if required and only once, even in complex dependency networks. This reduces unnecessary computations and enhances efficiency.

Execution Model: Push-Pull Hybrid

MemoizR utilizes a hybrid push-pull execution model. It pushes notifications of changes down the graph and executes MemoizR elements lazily on demand as they are pulled from the leaves. This approach combines the simplicity of pull systems with the execution efficiency of push systems.

Example

Here's a simple example of using MemoizR:

var f = new MemoFactory();
var v1 = f.CreateSignal(1);
var m1 = f.CreateMemoizR(async() => await v1.Get());
var m2 = f.CreateMemoizR(async() => await v1.Get() * 2);
var m3 = f.CreateMemoizR(async() => await m1.Get() + await m2.Get());

// Get Values
await m3.Get(); // Calculates m1 + 2 * m1 => (1 + 2 * 1) = 3

// Change
await v1.Set(2); // Setting v1 does not trigger the evaluation of the graph
await m3.Get(); // Calculates m1 + 2 * m1 => (1 + 2 * 2) = 6
await m3.Get(); // No operation, result remains 6

await v1.Set(3); // Setting v1 does not trigger the evaluation of the graph
await v1.Set(2); // Setting v1 does not trigger the evaluation of the graph
await m3.Get(); // No operation, result remains 6 (because the last time the graph was evaluated, v1 was already 2)

MemoizR can also handle scenarios where the graph is not stable at runtime, making it adaptable to changing dependencies.

var m3 = f.CreateMemoizR(async() => await v1.Get() ? await m1.Get() : await m2.Get());

Declarative Structured Concurrency

MemoizR's declarative structured concurrency model enhances maintainability, error handling, and cancellation of complex concurrency use cases. It allows you to set up and manage concurrency in a clear and structured way, making your code easier to understand and maintain.

In summary, MemoizR offers a powerful and intuitive approach to managing concurrency and reactive data flows (Dataflow (Task Parallel Library), Channels), with features like implicit Join and LinkTo that simplify your code and improve maintainability. It also draws inspiration from ReactiveX, making it a versatile choice for reactive programming scenarios but without having to handle subscriptions.

var f = new MemoFactory("DSC");

var child1 = f.CreateConcurrentMapReduce(
    async c =>
    {
        await Task.Delay(3000, c.Token);
        return 3;
    });

// all tasks get canceled if one fails
var c1 = f.CreateConcurrentMapReduce(
    async c =>
    {
        await child1.Get(c);

        // Any group work can kick off other group work.
        await Task.WhenAll(Enumerable.Range(1, 10)
            .Select(x => f.CreateConcurrentMapReduce(
                async c =>
                {
                    await Task.Delay(3000, c.Token);
                    return x;
                }).Get(c)));
        
        return 4;
    });

var x = await c1.Get();

Get Started with MemoizR

Start using MemoizR to simplify and optimize concurrency management in your .NET applications. Enjoy a cleaner, and more efficient approach to handling concurrency challenges.

Test it

https://dotnetfiddle.net/Widget/Vrbwam

Example From: Khalid Abuhakmeh

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.1-rc.10 43 4/23/2024
0.1.1-rc.9 37 4/18/2024
0.1.1-rc.8 48 4/13/2024
0.1.1-rc.7 47 4/11/2024
0.1.1-rc.6 41 4/10/2024
0.1.1-rc.5 42 4/4/2024
0.1.1-rc.4 45 4/1/2024
0.1.1-rc.3 45 3/24/2024
0.1.1-rc.2 49 2/17/2024
0.1.1-rc.1 97 1/4/2024
0.1.0-rc9 121 11/6/2023
0.1.0-rc8 75 10/26/2023
0.1.0-rc7 68 10/24/2023
0.1.0-rc6 67 10/21/2023
0.1.0-rc5 55 10/19/2023
0.1.0-rc4 67 10/14/2023
0.1.0-rc3 61 10/13/2023
0.1.0-rc2 63 10/11/2023
0.1.0-rc10 50 11/12/2023
0.1.0-rc1 58 10/10/2023
0.1.0-rc.11 63 1/4/2024
0.1.0-alpha2 71 10/6/2023
0.1.0-alpha1 61 10/6/2023