lpsmith / postgresql-simple

Mid-level client library for accessing PostgreSQL from Haskell
Other
206 stars 71 forks source link

Reduce overhead of fold #163

Closed tvh closed 9 years ago

tvh commented 9 years ago

This addresses issue 3 of 6457e41491a256ffbbfefd470039e2fd3310a931.

This is not thoroughly tested yet.

lpsmith commented 9 years ago

Looks promising... although one thing I'm not sure I agree with here is the use of the state monad in go, I think that putting an explicit accumulator on loop and using forM instead of forM_ would be clearer and possibly a touch faster.

Also speaking of forM_, you might be creating a superfluous list there. As in, I'm pretty sure this list is created with older versions of GHC, like 7.6, though this might be fixed in 7.8 or 7.10, whichever implemented the foldl fusion stuff. This turned foldl into a good consumer, and might have also made forM_ and forM good consumers. IIRC, there's a special-cased variant of forM_ for simple numerical ranges in postgresql-simple somewhere, I'm not sure if this function would be applicable here.

lpsmith commented 9 years ago

Ok, yeah the function I am talking about is forM' and it's not applicable to this situation. We'll probably need to create a similar function, possibly giving forM' a better name too.

tvh commented 9 years ago

As far as I know the state monad should not add any overhead on runtime. Not using it may make things more clear though. I adjusted the code to use explicit recursion.

lpsmith commented 9 years ago

I'll clean this up a little, but it looks good. Thank you!

lpsmith commented 9 years ago

(Also, I meant foldM at a couple of spots in my comment above when I said forM)