RSCG – RSCG_Utils_Memo
 
 
| name | RSCG_Utils_Memo | 
| nuget | https://www.nuget.org/packages/rscgutils | 
| link | https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation | 
| author | Ignat Andrei | 
Memo the function result
This is how you can use RSCG_Utils_Memo .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="rscgutils" Version="2023.914.2016" OutputItemType="Analyzer" ReferenceOutputAssembly="false"  />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>
The code that you will use is
using DemoRSCG_UtilsMemo; fibTest f = new(); Console.Write("start calculating,see output"); Console.WriteLine("first time result:" + f.Test()); Console.WriteLine("memo,no output"); Console.WriteLine("second time result:" + f.Test()); var dt = DateTime.Now; Console.WriteLine("no memo :" + await f.fib(5) ); Console.WriteLine(" in " + DateTime.Now.Subtract(dt).TotalSeconds.ToString("0#")); dt = DateTime.Now; Console.WriteLine("memo :" + await f.fibonacci(5)); Console.WriteLine(" in " + DateTime.Now.Subtract(dt).TotalSeconds.ToString("0#")); dt = DateTime.Now; Console.WriteLine("FAST memo :" + await f.fibonacci(5)); Console.WriteLine(" in " + DateTime.Now.Subtract(dt).TotalSeconds.ToString("0#"));
namespace DemoRSCG_UtilsMemo;
internal partial class fibTest
{
    public long Test_MemoPure()
    {
        Console.WriteLine("calculating type");
        return this.GetType().ToString().GetHashCode();
    }
    public async Task<long> fib(long nr)
    {
        await Task.Delay(1000);
        //Console.WriteLine("calculated value for " + nr);
        if (nr <= 1) return 1;
        if (nr == 2) return 2;
        return await fib(nr - 1) + await fib(nr - 1);
    }
    public async Task<long> fibonacci_MemoPure(long nr)
    {
        if (nr <= 1) return 1;
        if (nr == 2) return 2;
        await Task.Delay(1000);
        return await fibonacci(nr - 1) + await fibonacci(nr - 1);
    }
}
The code that is generated is
using System.Collections.Concurrent;
//this is auto-generated by a tool
namespace DemoRSCG_UtilsMemo;
partial class fibTest 
{
    
    System.Collections.Concurrent.ConcurrentDictionary<Tuple<long >,long> __cache_DemoRSCG_UtilsMemo_fibTest_fibonacci_MemoPure =new System.Collections.Concurrent.ConcurrentDictionary<Tuple<long >,long>();
    //True 
    public async Task<long>  fibonacci (long nr ){
        var key= Tuple.Create(nr);
        if (__cache_DemoRSCG_UtilsMemo_fibTest_fibonacci_MemoPure.TryGetValue(key,out var result)) return result;
        //Console.WriteLine($"not in cache,calculating {key}");
        var data= await __wrap_fibonacci(key);
        return __cache_DemoRSCG_UtilsMemo_fibTest_fibonacci_MemoPure.GetOrAdd(key,data);
    }
    public async Task<long>  __wrap_fibonacci (Tuple<long > args){
        return await fibonacci_MemoPure (args.Item1);
    }
    
}
using System.Collections.Concurrent;
//this is auto-generated by a tool
namespace DemoRSCG_UtilsMemo;
partial class fibTest 
{
    
    System.Collections.Concurrent.ConcurrentDictionary<string,long > __cache_DemoRSCG_UtilsMemo_fibTest_Test_MemoPure =new System.Collections.Concurrent.ConcurrentDictionary<string,long >();
    //False 
    public  long  Test ( ){
        var key= string.Empty;
        if (__cache_DemoRSCG_UtilsMemo_fibTest_Test_MemoPure.TryGetValue(key,out var result)) return result;
        //Console.WriteLine($"not in cache,calculating {key}");
        var data=  __wrap_Test(key);
        return __cache_DemoRSCG_UtilsMemo_fibTest_Test_MemoPure.GetOrAdd(key,data);
    }
    public  long  __wrap_Test (string args){
        return  Test_MemoPure ();
    }
    
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/RSCG_Utils_Memo
Leave a Reply