6 people like it.
Like the snippet!
Concurrent Memoization
This is more generic variant of http://www.fssnip.net/c4
if you want to memoize over different functions / code paths that calls the same to-be-cached-code.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
|
module Memoize
//More generic variant of http://www.fssnip.net/c4
open System.Collections.Concurrent
let cache = ConcurrentDictionary<(string * obj),Lazy<obj>>()
let memoizeConcurrent (caller:string) (f: ('a -> 'b)) =
fun x -> (cache.GetOrAdd((caller, x|>box), lazy ((f x)|>box)).Force() |> unbox) : 'b
// and this works also with F# async functions.
let internal memoizeAsync f =
let cache = System.Collections.Concurrent.ConcurrentDictionary<'a, System.Threading.Tasks.Task<'b>>()
fun (x: 'a) ->
async {
return! cache.GetOrAdd(x, fun x ->
let threadfix = lazy( f(x) |> Async.StartAsTask )
threadfix.Force()) |> Async.AwaitTask
}
(*
[<Test>]
let ``async cache should work``() =
let mutable x = 0
let someSlowFunc mykey = async {
Console.WriteLine "Simulated downloading..."
do! Async.Sleep 400
Console.WriteLine "Simulated downloading Done."
x <- x + 1 // Side effect!
return "" }
let memFunc = memoizeAsync <| someSlowFunc
async {
do! memFunc "a" |> Async.Ignore
do! memFunc "a" |> Async.Ignore
do! memFunc "a" |> Async.Ignore
for i = 1 to 30 do
Async.Start( memFunc "a" |> Async.Ignore )
Async.Start( memFunc "a" |> Async.Ignore )
do! Async.Sleep 500
do! memFunc "a" |> Async.Ignore
do! memFunc "a" |> Async.Ignore
for i = 1 to 30 do
Async.Start( memFunc "a" |> Async.Ignore )
} |> Async.RunSynchronously
x |> shouldEqual 1
*)
|
module Memoize
namespace System
namespace System.Collections
namespace System.Collections.Concurrent
val cache : ConcurrentDictionary<(string * obj),Lazy<obj>>
Full name: Memoize.cache
Multiple items
type ConcurrentDictionary<'TKey,'TValue> =
new : unit -> ConcurrentDictionary<'TKey, 'TValue> + 6 overloads
member AddOrUpdate : key:'TKey * addValueFactory:Func<'TKey, 'TValue> * updateValueFactory:Func<'TKey, 'TValue, 'TValue> -> 'TValue + 1 overload
member Clear : unit -> unit
member ContainsKey : key:'TKey -> bool
member Count : int
member GetEnumerator : unit -> IEnumerator<KeyValuePair<'TKey, 'TValue>>
member GetOrAdd : key:'TKey * valueFactory:Func<'TKey, 'TValue> -> 'TValue + 1 overload
member IsEmpty : bool
member Item : 'TKey -> 'TValue with get, set
member Keys : ICollection<'TKey>
...
Full name: System.Collections.Concurrent.ConcurrentDictionary<_,_>
--------------------
ConcurrentDictionary() : unit
ConcurrentDictionary(collection: System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<'TKey,'TValue>>) : unit
ConcurrentDictionary(comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : unit
ConcurrentDictionary(concurrencyLevel: int, capacity: int) : unit
ConcurrentDictionary(collection: System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<'TKey,'TValue>>, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : unit
ConcurrentDictionary(concurrencyLevel: int, collection: System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<'TKey,'TValue>>, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : unit
ConcurrentDictionary(concurrencyLevel: int, capacity: int, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : unit
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
type obj = System.Object
Full name: Microsoft.FSharp.Core.obj
Multiple items
active recognizer Lazy: Lazy<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.( |Lazy| )
--------------------
type Lazy<'T> = System.Lazy<'T>
Full name: Microsoft.FSharp.Control.Lazy<_>
val memoizeConcurrent : caller:string -> f:('a -> 'b) -> x:'a -> 'b
Full name: Memoize.memoizeConcurrent
val caller : string
val f : ('a -> 'b)
val x : 'a
ConcurrentDictionary.GetOrAdd(key: string * obj, value: Lazy<obj>) : Lazy<obj>
ConcurrentDictionary.GetOrAdd(key: string * obj, valueFactory: System.Func<(string * obj),Lazy<obj>>) : Lazy<obj>
val box : value:'T -> obj
Full name: Microsoft.FSharp.Core.Operators.box
val unbox : value:obj -> 'T
Full name: Microsoft.FSharp.Core.Operators.unbox
val internal memoizeAsync : f:('a -> Async<'b>) -> ('a -> Async<'b>)
Full name: Memoize.memoizeAsync
val f : ('a -> Async<'b>)
val cache : ConcurrentDictionary<'a,System.Threading.Tasks.Task<'b>>
namespace System.Threading
namespace System.Threading.Tasks
Multiple items
type Task<'TResult> =
inherit Task
new : function:Func<'TResult> -> Task<'TResult> + 7 overloads
member ContinueWith : continuationAction:Action<Task<'TResult>> -> Task + 9 overloads
member Result : 'TResult with get, set
static member Factory : TaskFactory<'TResult>
Full name: System.Threading.Tasks.Task<_>
--------------------
type Task =
new : action:Action -> Task + 7 overloads
member AsyncState : obj
member ContinueWith : continuationAction:Action<Task> -> Task + 9 overloads
member CreationOptions : TaskCreationOptions
member Dispose : unit -> unit
member Exception : AggregateException
member Id : int
member IsCanceled : bool
member IsCompleted : bool
member IsFaulted : bool
...
Full name: System.Threading.Tasks.Task
--------------------
System.Threading.Tasks.Task(function: System.Func<'TResult>) : unit
System.Threading.Tasks.Task(function: System.Func<'TResult>, cancellationToken: System.Threading.CancellationToken) : unit
System.Threading.Tasks.Task(function: System.Func<'TResult>, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(function: System.Func<obj,'TResult>, state: obj) : unit
System.Threading.Tasks.Task(function: System.Func<'TResult>, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(function: System.Func<obj,'TResult>, state: obj, cancellationToken: System.Threading.CancellationToken) : unit
System.Threading.Tasks.Task(function: System.Func<obj,'TResult>, state: obj, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(function: System.Func<obj,'TResult>, state: obj, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
--------------------
System.Threading.Tasks.Task(action: System.Action) : unit
System.Threading.Tasks.Task(action: System.Action, cancellationToken: System.Threading.CancellationToken) : unit
System.Threading.Tasks.Task(action: System.Action, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj) : unit
System.Threading.Tasks.Task(action: System.Action, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj, cancellationToken: System.Threading.CancellationToken) : unit
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
System.Threading.Tasks.Task(action: System.Action<obj>, state: obj, cancellationToken: System.Threading.CancellationToken, creationOptions: System.Threading.Tasks.TaskCreationOptions) : unit
val async : AsyncBuilder
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
ConcurrentDictionary.GetOrAdd(key: 'a, value: System.Threading.Tasks.Task<'b>) : System.Threading.Tasks.Task<'b>
ConcurrentDictionary.GetOrAdd(key: 'a, valueFactory: System.Func<'a,System.Threading.Tasks.Task<'b>>) : System.Threading.Tasks.Task<'b>
val threadfix : Lazy<System.Threading.Tasks.Task<'b>>
Multiple items
type Async
static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member AwaitTask : task:Task -> Async<unit>
static member AwaitTask : task:Task<'T> -> Async<'T>
static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
static member CancelDefaultToken : unit -> unit
static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken
Full name: Microsoft.FSharp.Control.Async
--------------------
type Async<'T>
Full name: Microsoft.FSharp.Control.Async<_>
static member Async.StartAsTask : computation:Async<'T> * ?taskCreationOptions:System.Threading.Tasks.TaskCreationOptions * ?cancellationToken:System.Threading.CancellationToken -> System.Threading.Tasks.Task<'T>
member System.Lazy.Force : unit -> 'T
static member Async.AwaitTask : task:System.Threading.Tasks.Task -> Async<unit>
static member Async.AwaitTask : task:System.Threading.Tasks.Task<'T> -> Async<'T>
More information