[Haskell-cafe] Caching the Result of a Transaction?
Jake Mcarthur
jake.mcarthur at gmail.com
Sat Apr 26 12:54:38 EDT 2008
I have a problem I've been trying to work around using the existing
STM API, and so far it seems that I may be unable to do it. For more
background, see my blog post at <http://geekrant.wordpress.com/2008/04/25/stm-caching-need/
>. Here, for brevity, I will only describe exactly what I think I
need, not what it's for.
Say I have a function f :: STM a. The transaction reads from one or
more TMVars, performs some computation, and returns the result in the
STM monad. Also, in this scenario, it is known that once the TMVars
have values, those values will never be changed again (write once,
read many, somewhat like IVars before they were removed). Now say I
try to use this function as so.
liftM2 (,) f f
So the desired result is a pair in the STM monad where both components
are the result from f. The problem I have is that, in the above
example, the TMVars are all read twice and the computations are all
performed twice, once for each of the components of the resulting
pair. In many cases, this may be the correct thing to do because the
values of the TMVars may have changed, but what about this case where
I _know_ that the values have not been modified?
What I need is a way to cache the result of f so that future uses of f
don't have to reread from the TMVars, even across multiple
transactions, maybe even leading to the eventual garbage collection of
the TMVars if they are not used elsewhere.
Right now I think the only way to do this would be to change the STM
implementation slightly and create a new primitive function. If there
is a way to do something like this with the current STM API, I would
love to hear suggestions. Any ideas?
- Jake McArthur
More information about the Haskell-Cafe
mailing list