[Haskell-beginners] Global variables

Michael Snoyman michael at snoyman.com
Thu Jun 30 07:40:53 CEST 2011


On Thu, Jun 30, 2011 at 8:35 AM, Ertugrul Soeylemez <es at ertes.de> wrote:
> Michael Snoyman <michael at snoyman.com> wrote:
>
>> Though in this case, you can go with the cheaper IORef, since
>> (presumably) the only operation is going to be incrementing the
>> counter. Just be sure to use atomicModifyIORef[1].
>
> I never recommend (or mention for that matter) IORefs in the beginner
> list.  Also I think than an atomically modified IORef is not necessarily
> faster.  TVars are optimized for that kind of operation.

I understand why you avoid mentioning IORef, but I personally find it
a simpler solution to the problem, and avoids the need for a beginner
to jump into STM. I'd be very interested in seeing if TVars can be as
fast as IORefs for this kind of stuff; I know switching the timeout
code in Warp to use atomic IORef operations produced a *huge* speed
gain, but I may be conflating multiple code changes together.

Michael



More information about the Beginners mailing list