Is there an object-identity-based, thread-safe memoization library somewhere?

前端 未结 3 1459
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 15:11

I know that memoization seems to be a perennial topic here on the haskell tag on stack overflow, but I think this question has not been asked before.

I\'m aware

相关标签:
3条回答
  • 2021-02-07 15:16

    If you only want to memoize based on object identity, and not equality, you can just use the existing laziness mechanisms built into the language.

    For example, if you have a data structure like this

    data Foo = Foo { ... }
    expensive :: Foo -> Bar
    

    then you can just add the value to be memoized as an extra field and let the laziness take care of the rest for you.

    data Foo = Foo { ..., memo :: Bar }
    

    To make it easier to use, add a smart constructor to tie the knot.

    makeFoo ... = let foo = Foo { ..., memo = expensive foo } in foo
    

    Though this is somewhat less elegant than using a library, and requires modification of the data type to really be useful, it's a very simple technique and all thread-safety issues are already taken care of for you.

    0 讨论(0)
  • 2021-02-07 15:32

    Ekmett just uploaded a library that handles this and more (produced at HacPhi): http://hackage.haskell.org/package/intern. He assures me that it is thread safe.

    Edit: Actually, strictly speaking I realize this does something rather different. But I think you can use it for your purposes. It's really more of a stringtable-atom type interning library that works over arbitrary data structures (including recursive ones). It uses WeakPtrs internally to maintain the table. However, it uses Ints to index the values to avoid structural equality checks, which means packing them into the data type, when what you want are apparently actually StableNames. So I realize this answers a related question, but requires modifying your data type, which you want to avoid...

    0 讨论(0)
  • 2021-02-07 15:36

    It seems that stable-memo would be just what you needed (although I'm not sure if it can handle multiple threads):

    Whereas most memo combinators memoize based on equality, stable-memo does it based on whether the exact same argument has been passed to the function before (that is, is the same argument in memory).

    • stable-memo only evaluates keys to WHNF.

    • This can be more suitable for recursive functions over graphs with cycles.

    • stable-memo doesn't retain the keys it has seen so far, which allows them to be garbage collected if they will no longer be used. Finalizers are put in place to remove the corresponding entries from the memo table if this happens.

    • Data.StableMemo.Weak provides an alternative set of combinators that also avoid retaining the results of the function, only reusing results if they have not yet been garbage collected.

    • There is no type class constraint on the function's argument.

    stable-memo will not work for arguments which happen to have the same value but are not the same heap object. This rules out many candidates for memoization, such as the most common example, the naive Fibonacci implementation whose domain is machine Ints; it can still be made to work for some domains, though, such as the lazy naturals.

    0 讨论(0)
提交回复
热议问题