Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time to live memoization in F#

Tags:

f#

Not sure if I got this right or whether there's a better way or an existing library solving this problem already.

In particular I'm not sure if the CAS would need a memory fence... I think not but better ask.

I also tried with an agent and mutable dictionary but my intuition that it would be slower was confirmed and the implementation was more involved.

module CAS =
    open System.Threading

    let create (value: 'T) =
        let cell = ref value

        let get () = !cell

        let rec swap f =
            let before = get()
            let newValue = f before
            match Interlocked.CompareExchange<'T>(cell, newValue, before) with
            | result when obj.ReferenceEquals(before, result) ->
                newValue
            | _ ->
                swap f

        get, swap

module Memoization =
    let timeToLive milis f =
        let get, swap = CAS.create Map.empty

        let evict key =
            async {
                do! Async.Sleep milis
                swap (Map.remove key) |> ignore
            } |> Async.Start

        fun key ->
            let data = get()
            match data.TryFind key with
            | Some v -> v
            | None ->
                let v = f key
                swap (Map.add key v) |> ignore
                evict key
                v
like image 774
David Grenier Avatar asked Aug 13 '13 17:08

David Grenier


1 Answers

If you are willing to limit what to memoize to functions that take a string input, you can reuse the functionality from System.Runtime.Caching.

This should be reasonably robust as part of the core library (you would hope...) but the string limitation is a pretty heavy one and you'd have to benchmark against your current implementation if you want to do a comparison on performance.

open System
open System.Runtime.Caching

type Cached<'a>(func : string -> 'a, cache : IDisposable) =
    member x.Func : string -> 'a = func

    interface IDisposable with
        member x.Dispose () =
            cache.Dispose ()

let cache timespan (func : string -> 'a) =
    let cache = new MemoryCache(typeof<'a>.FullName)
    let newFunc parameter =
        match cache.Get(parameter) with
        | null ->
            let result = func parameter
            let ci = CacheItem(parameter, result :> obj)
            let cip = CacheItemPolicy()
            cip.AbsoluteExpiration <- DateTimeOffset(DateTime.UtcNow + timespan)
            cip.SlidingExpiration <- TimeSpan.Zero
            cache.Add(ci, cip) |> ignore
            result
        | result ->
            (result :?> 'a)
    new Cached<'a>(newFunc, cache)

let cacheAsync timespan (func : string -> Async<'a>) =
    let cache = new MemoryCache(typeof<'a>.FullName)
    let newFunc parameter =
        match cache.Get(parameter) with
        | null ->
            async {
                let! result = func parameter
                let ci = CacheItem(parameter, result :> obj)
                let cip = CacheItemPolicy()
                cip.AbsoluteExpiration <- DateTimeOffset(DateTime.UtcNow + timespan)
                cip.SlidingExpiration <- TimeSpan.Zero
                cache.Add(ci, cip) |> ignore
                return result
            }
        | result ->
            async { return (result :?> 'a) }
    new Cached<Async<'a>>(newFunc, cache)

Usage:

let getStuff = 
    let cached = cacheAsync (TimeSpan(0, 0, 5)) uncachedGetStuff
    // deal with the fact that the cache is IDisposable here
    // however is appropriate...
    cached.Func

If you're never interested in accessing the underlying cache directly you can obviously just return a new function with the same signature of the old - but given the cache is IDisposable, that seemed unwise.

I think in many ways I prefer your solution, but when I faced a similar problem I had a perverse thought that I should really use the built in stuff if I could.

like image 158
mavnn Avatar answered Jan 03 '23 02:01

mavnn