@@ -7,6 +7,7 @@
77{-# LANGUAGE PolyKinds #-}
88{-# LANGUAGE ScopedTypeVariables #-}
99{-# LANGUAGE TypeFamilies #-}
10+{-# LANGUAGE MagicHash, BangPatterns #-}
1011
1112
1213module Data.TypeRep.OptimalVector
@@ -26,10 +27,11 @@ import Data.Foldable (for_)
2627import Data.Proxy (Proxy (..))
2728import Data.Typeable (Typeable, typeRep, typeRepFingerprint)
2829import Data.Word (Word64)
29−import GHC.Base (Any, liftM)
30+import GHC.Base hiding (empty)
3031import GHC.Exts (sortWith)
3132import GHC.Fingerprint (Fingerprint (..))
3233import Unsafe.Coerce (unsafeCoerce)
34+import GHC.Prim
3335
3436import qualified Data.Vector as V
3537import qualified Data.Vector.Generic as G
@@ -160,18 +162,20 @@ fromList tfs = TypeRepVect (Unboxed.fromList fps) (V.fromList ans)
160162 an :: TF f -> Any
161163 an (TF x) = unsafeCoerce x
162164
163−
164165-- | Returns the index is found.
165166binarySearch :: Fingerprint -> Unboxed.Vector Fingerprint -> Maybe Int
166167binarySearch fp fpVect =
167− let ind = binSearchHelp (-1) (Unboxed.length fpVect) in
168− if fp == (fpVect Unboxed.! ind) then Just ind else Nothing
168+ let
169+ !(I# len) = Unboxed.length fpVect
170+ ind = I# (binSearchHelp (-1#) len)
171+ in
172+ if fp == (fpVect Unboxed.! ind) then Just ind else Nothing
169173 where
170− binSearchHelp :: Int -> Int -> Int
171− binSearchHelp l r = if l < r - 1
172− then (
173− let m = (l + r) `div` 2 in
174− if fpVect Unboxed.! m < fp
174+ binSearchHelp :: Int# -> Int# -> Int#
175+ binSearchHelp l r = case l <# (r -# 1#) of
176+ 0# -> r
177+ _ ->
178+ let m = uncheckedIShiftRA# (l +# r) 1# in
179+ if Unboxed.unsafeIndex fpVect (I# m) < fp
175180 then binSearchHelp m r
176− else binSearchHelp l m )
177− else r
181+ else binSearchHelp l m