FP.Memoization 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package FP.Memoization --version 1.0.0
NuGet\Install-Package FP.Memoization -Version 1.0.0
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="FP.Memoization" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FP.Memoization --version 1.0.0
#r "nuget: FP.Memoization, 1.0.0"
#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 FP.Memoization as a Cake Addin
#addin nuget:?package=FP.Memoization&version=1.0.0

// Install FP.Memoization as a Cake Tool
#tool nuget:?package=FP.Memoization&version=1.0.0

MIT License Travis branch

Memoizer

Memoize C# functions with ease.
No dependencies, targets .NET Standard 1.1.
All 16 Func delegates are supported and tested using source code generation.

Why ?

From Wikipedia:
In computing, memoization [...] is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

Essentially we want to avoid executing a function again if its inputs do not change.
This repository provides helper methods to memoize a function that can take up to 16 parameters.

Also most of NuGet packages out there are outdated and do not target .NET Standard so they cannot be used with .NET Core.

How do I use it ?

Add an using for Memoization namespace.
Call Memoizer.Memoize(fn) and pass it the function that you want to memoize.
You will receive back and instance of Memoizer which you can use to control memoization for your function.
Invoke the method Call from the Memoizer instance to execute the memoized function.

Code example:


using Memoization;

...

public string SomeFunctionThatYouWantToMemoize(string someArgs) {
  ...
}

//Memoize the function
var memoizedFn = Memoizer.Memoize(SomeFunctionThatYouWantToMemoize);

//Call the memoized function
var result = memoizedFn.Call("Something");

A Memoizer instance can also be implicitly converted to a Func so you can use it more transparently.
What you get is essentially a reference to Memoizer.Call that you can still control from the Memoizer instance.


//Memoize the function
var memoizedFn = Memoizer.Memoize(SomeFunctionThatYouWantToMemoize);

//Convert the Memoizer instance to a `Func` implictily
Func<string, string> memoizedFnAsFunc = memoizedFn;

Another thing to take into consideration is that memoization might generate memory leaks.

In case you want to clear any memoized parameter you can call Reset on a Memoizer instance.
This is useful if you wish to force executing the function again or clear any retained memory.

Tips on function memoization

Memoization works best if the parameters are immutable.
Having immutable parameters allows us to compare them with a simple reference equal, improving performance.

A memoized function should not have side effects.
Memoization will prevent the function from executing if the inputs are the same therefore if you depend on some side effects resulting from the function execution you will lose them.

A memoized function should be pure.
That means that the function result is the same if the inputs are the same, if the function was to depend on other context it would be impossible to memoize correctly as only parameters can be cached.

Limitations

The memoization mechanism does not event try to be thread safe, like, at all. Keep that in mind.

Only the last parameters used to invoke the function are recorded, if they change the function is evaluated again.
This is to make sure that the memoization does not use too much memory and that there is a reliable cache invalidation.
Therefore it is better if you memoize only functions that you know are called many times with the same parameters.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.1 is compatible.  netstandard1.2 was computed.  netstandard1.3 was computed.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
1.1.0 37,284 8/27/2018
1.0.0 789 7/23/2018