This is not really a bug ... but.

I'm benchmarking MVar's and other shared memory abstractions, e.g. by
accessing a variable a number of times:


main :: IO ()
main = do {
        v <- newMVar 0;
        access v 10000000
} where access v 0 = print "done"
-- 1)   access v n = do {swapMVar v n; access v (n-1)}
-- 2)   access v n = do {takeMVar v; putMVar v n; access v (n-1)}
-- 3)   access v n = do {setVar v n; access v (n-1)}

I'm accessing the MVar's according to 3 different schemes: one using swapMVar
(1), the other using takeMVar and putMVar (2) and the third by calling a method of 
class 

        Variable var where
                setVar :: Var a -> a -> IO ()
                
        instance Variable MVar where
                setVar mvar val = do {takeMVar mvar; putMVar mvar val}

These are the results that I get when running the programs:

        1) 42 secs
        2) 16 secs
        3) 32 secs

It's quite surprising to see how much time swapMVar costs: I actually
expected it to be faster than a take followed by a put. I have now
spend some time getting rid of swapMVar wherever performance matters.

One thing bugs me though: what is causing the overhead when I call the
class operation, and - how can I get around it! This is quite
important to the efficency of some of my basic abstractions such as
reentrant monitors and the like.

Einar

Reply via email to