@@ -7,6 +7,7 @@
7{-# LANGUAGE PolyKinds #-}
7{-# LANGUAGE PolyKinds #-}
8{-# LANGUAGE ScopedTypeVariables #-}
8{-# LANGUAGE ScopedTypeVariables #-}
9{-# LANGUAGE TypeFamilies #-}
9{-# LANGUAGE TypeFamilies #-}
10+{-# LANGUAGE MagicHash, BangPatterns #-}
12module Data.TypeRep.OptimalVector
13module Data.TypeRep.OptimalVector
@@ -26,10 +27,11 @@ import Data.Foldable (for_)
26import Data.Proxy (Proxy (..))
27import Data.Proxy (Proxy (..))
27import Data.Typeable (Typeable, typeRep, typeRepFingerprint)
28import Data.Typeable (Typeable, typeRep, typeRepFingerprint)
28import Data.Word (Word64)
29import Data.Word (Word64)
29−import GHC.Base (Any, liftM)
30+import GHC.Base hiding (empty)
30import GHC.Exts (sortWith)
31import GHC.Exts (sortWith)
31import GHC.Fingerprint (Fingerprint (..))
32import GHC.Fingerprint (Fingerprint (..))
32import Unsafe.Coerce (unsafeCoerce)
33import Unsafe.Coerce (unsafeCoerce)
34import qualified Data.Vector as V
36import qualified Data.Vector as V
35import qualified Data.Vector.Generic as G
37import qualified Data.Vector.Generic as G
@@ -160,18 +162,20 @@ fromList tfs = TypeRepVect (Unboxed.fromList fps) (V.fromList ans)
160 an :: TF f -> Any
162 an :: TF f -> Any
161 an (TF x) = unsafeCoerce x
163 an (TF x) = unsafeCoerce x
164-- | Returns the index is found.
165-- | Returns the index is found.
165binarySearch :: Fingerprint -> Unboxed.Vector Fingerprint -> Maybe Int
166binarySearch :: Fingerprint -> Unboxed.Vector Fingerprint -> Maybe Int
166binarySearch fp fpVect =
167binarySearch fp fpVect =
169+ !(I# len) = Unboxed.length fpVect
167− let ind = binSearchHelp (-1) (Unboxed.length fpVect) in
170+ ind = I# (binSearchHelp (-1#) len)
168− if fp == (fpVect Unboxed.! ind) then Just ind else Nothing
172+ if fp == (fpVect Unboxed.! ind) then Just ind else Nothing
170− binSearchHelp :: Int -> Int -> Int
174+ binSearchHelp :: Int# -> Int# -> Int#
171− binSearchHelp l r = if l < r - 1
175+ binSearchHelp l r = case l <# (r -# 1#) of
173− let m = (l + r) `div` 2 in
178+ let m = uncheckedIShiftRA# (l +# r) 1# in
174− if fpVect Unboxed.! m < fp
179+ if Unboxed.unsafeIndex fpVect (I# m) < fp
175 then binSearchHelp m r
180 then binSearchHelp m r
176− else binSearchHelp l m )
181+ else binSearchHelp l m