monadic performance

Hello, I've written some code that does a foldl (or scanl, depending on my mood) kind of thing, and builds a huge tree structure as it goes along. I've been careful to make "insert"s as strict (and eager) as possible, since I know all the pieces will be evaluated eventually anyway. Now I'd like to be able to write various output as I traverse the structure, and I was thinking about using the WriterT monad transformer. For this, I could just make the base monad Identity, or I think it could be nice (from a code elegance perspective) to use State and rewrite the insert code to be State-ful. Sometimes when I have used State before, I have been bitten by laziness. Should I expect a monadic version to take a performance hit? What if I use some SPECIALIZE pragmas or somesuch? Is it more efficient to write one from scratch, or do specific type annotations give me the same thing anyway? Thanks, Chad

On Fri, Oct 27, 2006 at 06:28:58AM -0700, Chad Scherrer wrote:
Should I expect a monadic version to take a performance hit? What if I use some SPECIALIZE pragmas or somesuch? Is it more efficient to write one from scratch, or do specific type annotations give me the same thing anyway?
I was recently making experiments with rewriting a decoder for some data format in Haskell. At some point I had a hand-written State/Reader monad using unboxed tuples and I tried rewriting it to monad transformers. I was surprised that there was no difference in performance. This is a change from monadic version to monadic version, but the speed was at about 66% of the C++ version, so it seems monads didn't add much cost. BTW, the state and enviroment were Ptrs. Best regards Tomasz

On 10/28/06, Tomasz Zielonka
On Fri, Oct 27, 2006 at 06:28:58AM -0700, Chad Scherrer wrote:
Should I expect a monadic version to take a performance hit? What if I use some SPECIALIZE pragmas or somesuch? Is it more efficient to write one from scratch, or do specific type annotations give me the same thing anyway?
That's good to hear, thanks! At this point my biggest concern is whether the strict parts and lazy parts will play nice. Everything about my data structure is strict, and thinking about how a Writer monad would interact with that just makes me confused, so far. -Chad
participants (2)
-
Chad Scherrer
-
Tomasz Zielonka