RepositoryC#CHSwitzerland
TI

timonkrebs/MemoizR

Declarative Structured Concurrency for C#

121stars
2forks
19issues
1views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: reactiv

#1 Repository of the month

1
TINIX TRENDING·Topic: signal

#1 Repository of the month

1
TINIX TRENDING·Topic: signals

#1 Repository of the month

MemoizR

MemoizR-logo
Streamlined Concurrency

.NET NuGet NuGet

"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 library for .NET that brings the power of Dynamic Lazy Memoization and Declarative Structured Concurrency to your fingertips. It streamlines complex multi-threaded scenarios, making them easier to write, maintain, and reason about.

Inspired From Stephen Cleary — Asynchronous streams https://www.youtube.com/watch?v=-Tq4wLyen7Q&t=706s

compared towhich isMemoizR/Signals
IEnumerablesynchronousasynchronous
Tasksingle valuemulti value
Observablepush basedpush-pull
IAsyncEnumerablepull basedpush-pull

Key Features

  • Dynamic Lazy Memoization: Calculate values only when needed, avoiding unnecessary computations and optimizing performance.
  • Declarative Structured Concurrency: Easily manage complex concurrency scenarios with straightforward configuration, effortless maintenance, robust error handling, and seamless cancellation.
  • Dependency Graph:Automatically track dependencies between your data, ensuring that only necessary computations are performed.
  • Automatic Synchronization: Work with shared state without the hassle of manual synchronization.
  • Performance Optimization: Benefit from memoization for read-heavy scenarios and lazy evaluation for write-heavy scenarios. Inspiration

MemoizR draws inspiration from various sources

  • Reactively and Solid: Dynamic lazy memoization concepts.
  • VHDL: Synchronization mechanisms.
  • ReactiveX: Reactive programming paradigms.
  • Structured Concurrency: Principles for well-structured concurrent code. Special thanks to @mfp22 for the idea of signal operators!

Advantages over ReactiveX and Dataflow

MemoizR offers several advantages over traditional concurrency libraries:

Implicit Subscription Handling

No need to manage subscriptions manually; MemoizR automatically tracks and synchronizes dependencies. Implicit LinkTo: Dependencies are automatically linked based on your code's structure, simplifying data flow setup. Simplified Error Handling: Structured concurrency makes error handling more robust and easier to reason about.

Usage

Basic Memoization

// Setup
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 Value manually
await m3.Get(); // Calculates m1 + 2 * m1 => (1 + 2 * 1) = 3

// Change
await Task.Run(async () => await v1.Set(2));
// Synchronization is handled by MemoizR
await m3.Get(); // Calculates m1 + 2 * m1 => (1 + 2 * 2) = 6
await m3.Get(); // No operation, result is still 6

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

Dynamic Graphs

MemoizR can handle dynamic changes in the dependency graph:

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

Declarative Structured Concurrency

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();
        return 4;
    });

var x = await c1.Get();

Resources

A concurrent job can own resources. These resources will be disposed by the job after all its work is done.

var groupTask = f.CreateConcurrentMapReduce(async group =>
{
    group.AddResource(myDisposableResource);

    return await myDisposableResource.DoWorkAsync(group.Token);
});
await groupTask.Get(); // First, waits for all tasks to complete; then, disposes myDisposableResource.

All exceptions raised by disposal of any resource are ignored.

Reactivity

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 r1 = f.CreateReaction(m1, m2, (val1, val2) => val1 + val2);

WPF / UI threads

With the MemoizR.Wpf package the whole dependency graph keeps evaluating on the thread pool; only each reaction's action is dispatched to the WPF UI thread:

var f = new MemoFactory().AddWpfDispatcher(); // uses Application.Current.Dispatcher

var v1 = f.CreateSignal(1);
var m1 = f.CreateMemoizR(async () => await v1.Get() * 2); // computed on worker threads

// Dependencies are passed as separate parameters so they are evaluated in parallel on the
// thread pool; only the action below runs on the UI thread, with the already-computed values.
var r1 = f.CreateReaction(m1, v => viewModel.Value = v);

await v1.Set(5); // safe from any thread

A specific Dispatcher can be supplied with f.AddWpfDispatcher(dispatcher). On other UI stacks, register the UI SynchronizationContext directly from the UI thread with f.AddSynchronizationContext(SynchronizationContext.Current!) (MemoizR.Reactive); reactions built from the factory then follow the same contract: dependencies on the thread pool, action on the registered context.

Try it out!https: Experiment with MemoizR online: https://dotnetfiddle.net/Widget/EWtptc

Example From: Khalid Abuhakmeh

Testing

Run the test suite with dotnet test. Thread interleavings of the locking code are explored systematically with Microsoft Coyote; see docs/Coyote.md for how to rewrite the assemblies and run the Coyote test.

Similar Projects

Discover other similar projects and AI models.

RepositoryTypeScript
ngrx/platform

Reactive State for Angular

8.3k
Nov 25, 2026
RepositoryTypeScript
preactjs/signals

Manage state with style in every framework

4.5k
Nov 18, 2026
RepositoryHTML
sdras/signals-demo

⚡️ A small demo site to explain some of the cool things Signals-based reactivity does in Angular

143
Mar 19, 2023
RepositoryClojure
lilactown/flex

flex is a reactive signal library for Clojure(Script)

94
Jul 18, 2025
RepositoryTypeScript
damiansire/angular-signals-playground

An interactive playground and introduction to learning Angular Signals (computed, effect, linkedSignal, resource) hands-on.

92
Nov 15, 2026
RepositoryShell
ohmyzsh/ohmyzsh

🙃 A delightful, community-driven framework for managing your zsh configuration. Built by 2,600+ contributors, with hundreds of plugins (git, rails, laravel, node, python, docker, kubectl, aws, and more), themes to spice up your terminal, and automatic updates to keep your command line feeling like home.

191.0k
Nov 29, 2026
RepositoryC#
akkadotnet/akka.net

Canonical actor model implementation for .NET with local + distributed actors in C# and F#.

5.1k
Nov 25, 2026
RepositoryC#
AvillaDaniel/AvillaForensics

Avilla Forensics FREE

1.2k
Apr 4, 2026
RepositoryC#
signal-csharp/Signal-Windows

Unofficial Signal Private Messenger for Windows

112
May 24, 2021