
On Tuesday 17 November 2009 9:56:21 pm Antoine Latter wrote:
Every time this comes up I grow paranoid that there is some sort of general overhead to evaluating an expression of type "StateT s Identity a" vs "State s a".
So I finally benchmarked it using the example code listed in Control.Monad.State, and found no difference whatsoever.
Here's the source: http://hpaste.org/fastcgi/hpaste.fcgi/view?id=12259#a12259
I guess parsec is the odd one out, where there is a difference. Or I was measuring something else back when I had parsec benchmarks set up.
At least in the State case, everything involved is a newtype, so: StateT s Identity a should be represented identically to: s -> Identity (a, s) = s -> (a, s) Which is exactly what State s a is. From there it's just up to the compiler to eliminate no-ops like: return :: a -> a -- return :: a -> Identity a return a = a which shouldn't be too arduous. Building up stacks of novel monads can introduce additional indirection over flattening them into a direct implementation (like StateT s (WriterT w ...) versus RWST r s w ...), but thankfully, Identity shouldn't introduce such overhead. -- Dan