Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up the Eq instance for TArray #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Control/Concurrent/STM/TArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Control.Concurrent.STM.TArray (
import Data.Array (Array, bounds)
import Data.Array.Base (listArray, arrEleBottom, unsafeAt, MArray(..),
IArray(numElements))
import Data.Ix (rangeSize)
import Data.Ix (Ix (rangeSize))
import Data.Typeable (Typeable)
import Control.Concurrent.STM.TVar (TVar, newTVar, readTVar, writeTVar)
#ifdef __GLASGOW_HASKELL__
Expand All @@ -41,7 +41,18 @@ import Control.Sequential.STM (STM)
-- but it may be replaced by a more efficient implementation in the future
-- (the interface will remain the same, however).
--
newtype TArray i e = TArray (Array i (TVar e)) deriving (Eq, Typeable)
newtype TArray i e = TArray (Array i (TVar e)) deriving (Typeable)

-- There are no provisions for moving/copying TVars between TArrays.
-- Therefore, two TArrays are equal if and only if they are both empty or are
-- actually the same array in memory. We have no safe operations for checking
-- that directly (though in practice we could use `unsafeCoerce#` with
-- `sameMutableArray#`). So instead we take a quick look at the array sizes and
-- then decide based on the first TVar of each.
instance Ix i => Eq (TArray i e) where
TArray t1 == TArray t2
= numElements t1 == numElements t2
&& (numElements t1 == 0 || unsafeAt t1 0 == unsafeAt t2 0)

instance MArray TArray e STM where
getBounds (TArray a) = return (bounds a)
Expand Down