dkubb/memoizable — pull request #11 by sferik (2013-12-19 14:28:21+00:00)
Title: Use Hash for Memoizable::Memory

This patch replaces `ThreadSafe::Cache` with a `Hash` as storage for `Memoizable::Memory`. As with most decisions about data structures, this one has tradeoffs.

On the plus side, this patch:
* Uses a core Ruby data structure (removes an external gem dependency)
* Doesn’t require [using a `Mutex` to avoid deadlocks on MRI](https://github.com/dkubb/memoizable/pull/9)
* Allows memoized objects to be [serialized with `Marshal`](https://github.com/dkubb/memoizable/issues/10) (and adds an integration test for this)

On the minus side, we would no longer be able to guarantee that memoized methods won’t execute more than once on Ruby implementations without a GIL (such as JRuby and Rubinius).

In my opinion, the plusses outweigh the minus. Since only idempotent methods should be memoized, it shouldn’t effect a program’s semantics if a memoized method is executed more than once, however a race could lead to the same work being done twice.

In theory, we could release a platform-specific gem for JRuby that uses `ThreadSafe::Cache`, while the MRI implementation uses `Hash`. This would allow us to get rid of the `Mutex`, since the `thread_safe` deadlock only occurs on MRI, however this would not solve the serialization issue on JRuby.

I suspect this will be a somewhat controversial patch and welcome a lengthy discussion.