diff --git a/vector/internal/GenUnboxTuple.hs b/vector/internal/GenUnboxTuple.hs index 64daafa5..eed092c4 100644 --- a/vector/internal/GenUnboxTuple.hs +++ b/vector/internal/GenUnboxTuple.hs @@ -1,7 +1,12 @@ +#!/usr/bin/env cabal +{- cabal: +build-depends: base, pretty +-} {-# LANGUAGE ParallelListComp #-} module Main where +import Prelude hiding ((<>)) import Text.PrettyPrint import System.Environment ( getArgs ) @@ -235,6 +240,6 @@ generate n = ,("basicUnsafeThaw", gen_unsafeThaw) ,("basicLength", gen_length "V") ,("basicUnsafeSlice", gen_unsafeSlice "G" "V") - ,("basicUnsafeIndexM", gen_basicUnsafeIndexM) + ,("basicUnsafeIndexM#", gen_basicUnsafeIndexM) ,("basicUnsafeCopy", gen_unsafeCopy "V" qG) ,("elemseq", gen_elemseq)] diff --git a/vector/src/Data/Vector/Fusion/Bundle/Monadic.hs b/vector/src/Data/Vector/Fusion/Bundle/Monadic.hs index 6e51532f..2fdc3a89 100644 --- a/vector/src/Data/Vector/Fusion/Bundle/Monadic.hs +++ b/vector/src/Data/Vector/Fusion/Bundle/Monadic.hs @@ -4,6 +4,7 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} +{-# LANGUAGE MagicHash #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -98,10 +99,10 @@ import Control.Monad.Primitive import qualified Data.List as List import Data.Char ( ord ) -import GHC.Base ( unsafeChr ) +import GHC.Base ( unsafeChr, Int(..) ) import Control.Monad ( liftM ) import Prelude - ( Eq, Ord, Num, Enum, Functor, Monad, Bool(..), Ordering, Char, Int, Word, Integer, Float, Double, Maybe(..), Either(..), Integral, RealFrac + ( Eq, Ord, Num, Enum, Functor, Monad, Bool(..), Ordering, Char, Word, Integer, Float, Double, Maybe(..), Either(..), Integral, RealFrac , return, fmap, otherwise, id, const, seq, max, maxBound, fromIntegral, truncate , (+), (-), (<), (<=), (>), (>=), (==), (/=), (&&), (.), ($), (<$), (/) ) @@ -1079,9 +1080,10 @@ fromVector v = v `seq` n `seq` Bundle (Stream step 0) n = basicLength v {-# INLINE step #-} - step i | i >= n = return Done - | otherwise = case basicUnsafeIndexM v i of - Box x -> return $ Yield x (i+1) + step (I# i) + | I# i >= n = return Done + | otherwise = case basicUnsafeIndexM# v i of + Box x -> return $ Yield x (I# i + 1) {-# INLINE vstep #-} @@ -1100,10 +1102,10 @@ fromVectors us = Bundle (Stream pstep (Left us)) pstep (Left []) = return Done pstep (Left (v:vs)) = basicLength v `seq` return (Skip (Right (v,0,vs))) - pstep (Right (v,i,vs)) - | i >= basicLength v = return $ Skip (Left vs) - | otherwise = case basicUnsafeIndexM v i of - Box x -> return $ Yield x (Right (v,i+1,vs)) + pstep (Right (v, I# i, vs)) + | I# i >= basicLength v = return $ Skip (Left vs) + | otherwise = case basicUnsafeIndexM# v i of + Box x -> return $ Yield x (Right (v, I# i + 1, vs)) -- FIXME: work around bug in GHC 7.6.1 vstep :: HasCallStack => [v a] -> m (Step [v a] (Chunk v a)) @@ -1131,10 +1133,10 @@ concatVectors Bundle{sElems = Stream step t} Skip s' -> return (Skip (Left s')) Done -> return Done - pstep (Right (v,i,s)) - | i >= basicLength v = return (Skip (Left s)) - | otherwise = case basicUnsafeIndexM v i of - Box x -> return (Yield x (Right (v,i+1,s))) + pstep (Right (v, I# i, s)) + | I# i >= basicLength v = return (Skip (Left s)) + | otherwise = case basicUnsafeIndexM# v i of + Box x -> return (Yield x (Right (v, I# i + 1,s))) vstep s = do diff --git a/vector/src/Data/Vector/Generic.hs b/vector/src/Data/Vector/Generic.hs index 85e2f6db..f3d26aae 100644 --- a/vector/src/Data/Vector/Generic.hs +++ b/vector/src/Data/Vector/Generic.hs @@ -1,6 +1,7 @@ {-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE MagicHash #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -218,6 +219,7 @@ import Data.Typeable ( Typeable, gcast1 ) import Data.Data ( Data, DataType, Constr, Fixity(Prefix), mkDataType, mkConstr, constrIndex, mkNoRepType ) import qualified Data.Traversable as T (Traversable(mapM)) +import GHC.Exts (Int(..)) -- Length information -- ------------------ @@ -253,16 +255,16 @@ infixl 9 ! (!) :: (HasCallStack, Vector v a) => v a -> Int -> a {-# INLINE_FUSED (!) #-} -- See NOTE: [Strict indexing] -(!) v !i = checkIndex Bounds i (length v) $ unBox (basicUnsafeIndexM v i) +(!) v (I# i) = checkIndex Bounds (I# i) (length v) $ unBox (basicUnsafeIndexM# v i) infixl 9 !? -- | O(1) Safe indexing. (!?) :: Vector v a => v a -> Int -> Maybe a {-# INLINE_FUSED (!?) #-} -- See NOTE: [Strict indexing] --- Use basicUnsafeIndexM @Box to perform the indexing eagerly. -v !? (!i) - | i `inRange` length v = case basicUnsafeIndexM v i of Box a -> Just a +-- Use basicUnsafeIndexM# @Box to perform the indexing eagerly. +v !? (I# i) + | I# i `inRange` length v = case basicUnsafeIndexM# v i of Box a -> Just a | otherwise = Nothing @@ -280,7 +282,7 @@ last v = v ! (length v - 1) unsafeIndex :: Vector v a => v a -> Int -> a {-# INLINE_FUSED unsafeIndex #-} -- See NOTE: [Strict indexing] -unsafeIndex v !i = checkIndex Unsafe i (length v) $ unBox (basicUnsafeIndexM v i) +unsafeIndex v (I# i) = checkIndex Unsafe (I# i) (length v) $ unBox (basicUnsafeIndexM# v i) -- | /O(1)/ First element, without checking if the vector is empty. unsafeHead :: Vector v a => v a -> a @@ -340,7 +342,7 @@ unsafeLast v = unsafeIndex v (length v - 1) -- element) is evaluated eagerly. indexM :: (HasCallStack, Vector v a, Monad m) => v a -> Int -> m a {-# INLINE_FUSED indexM #-} -indexM v !i = checkIndex Bounds i (length v) $ liftBox $ basicUnsafeIndexM v i +indexM v (I# i) = checkIndex Bounds (I# i) (length v) $ liftBox $ basicUnsafeIndexM# v i -- | /O(1)/ First element of a vector in a monad. See 'indexM' for an -- explanation of why this is useful. @@ -358,9 +360,9 @@ lastM v = indexM v (length v - 1) -- explanation of why this is useful. unsafeIndexM :: (Vector v a, Monad m) => v a -> Int -> m a {-# INLINE_FUSED unsafeIndexM #-} -unsafeIndexM v !i = checkIndex Unsafe i (length v) +unsafeIndexM v (I# i) = checkIndex Unsafe (I# i) (length v) $ liftBox - $ basicUnsafeIndexM v i + $ basicUnsafeIndexM# v i -- | /O(1)/ First element in a monad, without checking for empty vectors. -- See 'indexM' for an explanation of why this is useful. @@ -1019,7 +1021,7 @@ backpermute v is = seq v -- NOTE: we do it this way to avoid triggering LiberateCase on n in -- polymorphic code index :: HasCallStack => Int -> Box a - index !i = checkIndex Bounds i n $ basicUnsafeIndexM v i + index (I# i) = checkIndex Bounds (I# i) n $ basicUnsafeIndexM# v i -- | Same as 'backpermute', but without bounds checking. unsafeBackpermute :: (Vector v a, Vector v Int) => v a -> v Int -> v a @@ -1036,7 +1038,7 @@ unsafeBackpermute v is = seq v {-# INLINE index #-} -- NOTE: we do it this way to avoid triggering LiberateCase on n in -- polymorphic code - index !i = checkIndex Unsafe i n $ basicUnsafeIndexM v i + index (I# i) = checkIndex Unsafe (I# i) n $ basicUnsafeIndexM# v i -- Safe destructive updates -- ------------------------ @@ -2581,9 +2583,9 @@ streamR v = v `seq` n `seq` (Bundle.unfoldr get n `Bundle.sized` Exact n) {-# INLINE get #-} get 0 = Nothing - get i = let !i' = i-1 + get i = let !(I# i') = i-1 in - case basicUnsafeIndexM v i' of Box x -> Just (x, i') + case basicUnsafeIndexM# v i' of Box x -> Just (x, I# i') -- | /O(n)/ Construct a vector from a 'Bundle', proceeding from right to left. unstreamR :: Vector v a => Bundle v a -> v a diff --git a/vector/src/Data/Vector/Generic/Base.hs b/vector/src/Data/Vector/Generic/Base.hs index 8625284b..dc8c7be0 100644 --- a/vector/src/Data/Vector/Generic/Base.hs +++ b/vector/src/Data/Vector/Generic/Base.hs @@ -1,5 +1,6 @@ {-# LANGUAGE BangPatterns #-} {-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE MagicHash #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -34,6 +35,7 @@ import qualified Data.Primitive.PrimArray as Prim import Control.Monad.ST import Data.Kind (Type) +import GHC.Exts (Int(..), Int#) -- | @Mutable v s a@ is the mutable version of the immutable vector type @v a@ with -- the state token @s@. It is injective on GHC 8 and newer. @@ -112,7 +114,13 @@ class MVector (Mutable v) a => Vector v a where -- -- which does not have this problem, because indexing (but not the returned -- element!) is evaluated immediately. - basicUnsafeIndexM :: v a -> Int -> Box a + basicUnsafeIndexM :: v a -> Int -> Box a + basicUnsafeIndexM xs (I# i) = basicUnsafeIndexM# xs i + {-# INLINE basicUnsafeIndexM #-} + + basicUnsafeIndexM# :: v a -> Int# -> Box a + basicUnsafeIndexM# xs i = basicUnsafeIndexM xs (I# i) + {-# INLINE basicUnsafeIndexM# #-} -- | /Assumed complexity: O(n)/ -- @@ -131,11 +139,12 @@ class MVector (Mutable v) a => Vector v a where where !n = basicLength src - do_copy i | i < n = do - x <- liftBox $ basicUnsafeIndexM src i - M.basicUnsafeWrite dst i x - do_copy (i+1) - | otherwise = return () + do_copy (I# i) + | I# i < n = do + x <- liftBox $ basicUnsafeIndexM# src i + M.basicUnsafeWrite dst (I# i) x + do_copy (I# i + 1) + | otherwise = return () -- | Evaluate @a@ as far as storing it in a vector would and yield @b@. -- The @v a@ argument only fixes the type and is not touched. This method is @@ -152,4 +161,4 @@ class MVector (Mutable v) a => Vector v a where elemseq _ = \_ x -> x {-# MINIMAL basicUnsafeFreeze, basicUnsafeThaw, basicLength, - basicUnsafeSlice, basicUnsafeIndexM #-} + basicUnsafeSlice, (basicUnsafeIndexM | basicUnsafeIndexM#) #-} diff --git a/vector/src/Data/Vector/Primitive.hs b/vector/src/Data/Vector/Primitive.hs index 4f8e25bf..51e50fdc 100644 --- a/vector/src/Data/Vector/Primitive.hs +++ b/vector/src/Data/Vector/Primitive.hs @@ -1,5 +1,6 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE MagicHash #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE RoleAnnotations #-} @@ -1721,12 +1722,12 @@ toList :: Prim a => Vector a -> [a] {-# INLINE toList #-} toList = G.toList --- | /O(n)/ Convert a list to a vector. During the operation, the --- vector’s capacity will be doubling until the list's contents are --- in the vector. Depending on the list’s size, up to half of the vector’s --- capacity might be empty. If you’d rather avoid this, you can use --- 'fromListN', which will provide the exact space the list requires but will --- prevent list fusion, or @'force' . 'fromList'@, which will create the +-- | /O(n)/ Convert a list to a vector. During the operation, the +-- vector’s capacity will be doubling until the list's contents are +-- in the vector. Depending on the list’s size, up to half of the vector’s +-- capacity might be empty. If you’d rather avoid this, you can use +-- 'fromListN', which will provide the exact space the list requires but will +-- prevent list fusion, or @'force' . 'fromList'@, which will create the -- vector and then copy it without the superfluous space. -- -- @since 0.4 diff --git a/vector/src/Data/Vector/Primitive/Unsafe.hs b/vector/src/Data/Vector/Primitive/Unsafe.hs index f13d0377..b44b4dd4 100644 --- a/vector/src/Data/Vector/Primitive/Unsafe.hs +++ b/vector/src/Data/Vector/Primitive/Unsafe.hs @@ -1,5 +1,6 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE MagicHash #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE RoleAnnotations #-} @@ -103,8 +104,8 @@ instance Prim a => G.Vector Vector a where {-# INLINE basicUnsafeSlice #-} basicUnsafeSlice j n (UnsafeVector i _ arr) = UnsafeVector (i+j) n arr - {-# INLINE basicUnsafeIndexM #-} - basicUnsafeIndexM (UnsafeVector i _ arr) j = return $! indexByteArray arr (i+j) + {-# INLINE basicUnsafeIndexM# #-} + basicUnsafeIndexM# (UnsafeVector i _ arr) j = return $! indexByteArray arr (i + Exts.I# j) {-# INLINE basicUnsafeCopy #-} basicUnsafeCopy (UnsafeMVector i n dst) (UnsafeVector j _ src) diff --git a/vector/src/Data/Vector/Storable.hs b/vector/src/Data/Vector/Storable.hs index 6ac73f86..9ab3da33 100644 --- a/vector/src/Data/Vector/Storable.hs +++ b/vector/src/Data/Vector/Storable.hs @@ -1,5 +1,6 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE MagicHash #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE RoleAnnotations #-} @@ -1760,12 +1761,12 @@ toList :: Storable a => Vector a -> [a] {-# INLINE toList #-} toList = G.toList --- | /O(n)/ Convert a list to a vector. During the operation, the --- vector’s capacity will be doubling until the list's contents are --- in the vector. Depending on the list’s size, up to half of the vector’s --- capacity might be empty. If you’d rather avoid this, you can use --- 'fromListN', which will provide the exact space the list requires but will --- prevent list fusion, or @'force' . 'fromList'@, which will create the +-- | /O(n)/ Convert a list to a vector. During the operation, the +-- vector’s capacity will be doubling until the list's contents are +-- in the vector. Depending on the list’s size, up to half of the vector’s +-- capacity might be empty. If you’d rather avoid this, you can use +-- 'fromListN', which will provide the exact space the list requires but will +-- prevent list fusion, or @'force' . 'fromList'@, which will create the -- vector and then copy it without the superfluous space. -- -- @since 0.4 diff --git a/vector/src/Data/Vector/Storable/Unsafe.hs b/vector/src/Data/Vector/Storable/Unsafe.hs index 2d6f9224..708dbaa0 100644 --- a/vector/src/Data/Vector/Storable/Unsafe.hs +++ b/vector/src/Data/Vector/Storable/Unsafe.hs @@ -1,5 +1,6 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE MagicHash #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE RoleAnnotations #-} @@ -120,12 +121,12 @@ instance Storable a => G.Vector Vector a where {-# INLINE basicUnsafeSlice #-} basicUnsafeSlice i n (UnsafeVector _ fp) = UnsafeVector n (updPtr (`advancePtr` i) fp) - {-# INLINE basicUnsafeIndexM #-} - basicUnsafeIndexM (UnsafeVector _ fp) i + {-# INLINE basicUnsafeIndexM# #-} + basicUnsafeIndexM# (UnsafeVector _ fp) i = return . unsafeInlineIO $ unsafeWithForeignPtr fp $ \p -> - peekElemOff p i + peekElemOff p (Exts.I# i) {-# INLINE basicUnsafeCopy #-} basicUnsafeCopy (UnsafeMVector n fp) (UnsafeVector _ fq) diff --git a/vector/src/Data/Vector/Strict/Unsafe.hs b/vector/src/Data/Vector/Strict/Unsafe.hs index 744fa134..dc5a1c22 100644 --- a/vector/src/Data/Vector/Strict/Unsafe.hs +++ b/vector/src/Data/Vector/Strict/Unsafe.hs @@ -1,6 +1,7 @@ {-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE MagicHash #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE TypeFamilies #-} @@ -119,8 +120,8 @@ instance G.Vector Vector a where basicLength = coerce (G.basicLength @V.Vector @a) {-# INLINE basicUnsafeSlice #-} basicUnsafeSlice = coerce (G.basicUnsafeSlice @V.Vector @a) - {-# INLINE basicUnsafeIndexM #-} - basicUnsafeIndexM = coerce (G.basicUnsafeIndexM @V.Vector @a) + {-# INLINE basicUnsafeIndexM# #-} + basicUnsafeIndexM# = coerce (G.basicUnsafeIndexM# @V.Vector @a) {-# INLINE basicUnsafeCopy #-} basicUnsafeCopy = coerce (G.basicUnsafeCopy @V.Vector @a) {-# INLINE elemseq #-} diff --git a/vector/src/Data/Vector/Unboxed.hs b/vector/src/Data/Vector/Unboxed.hs index f413c020..76624bf1 100644 --- a/vector/src/Data/Vector/Unboxed.hs +++ b/vector/src/Data/Vector/Unboxed.hs @@ -1,4 +1,5 @@ {-# LANGUAGE CPP #-} +{-# LANGUAGE MagicHash #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE TypeFamilies #-} -- | diff --git a/vector/src/Data/Vector/Unboxed/Unsafe.hs b/vector/src/Data/Vector/Unboxed/Unsafe.hs index 2a355139..c019eca5 100644 --- a/vector/src/Data/Vector/Unboxed/Unsafe.hs +++ b/vector/src/Data/Vector/Unboxed/Unsafe.hs @@ -4,6 +4,7 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE MagicHash #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE StandaloneDeriving #-} @@ -157,6 +158,9 @@ instance G.Vector Vector () where {-# INLINE basicUnsafeIndexM #-} basicUnsafeIndexM (V_Unit _) _ = return () + {-# INLINE basicUnsafeIndexM# #-} + basicUnsafeIndexM# (V_Unit _) _ = return () + {-# INLINE basicUnsafeCopy #-} basicUnsafeCopy (MV_Unit _) (V_Unit _) = return () @@ -249,13 +253,13 @@ instance P.Prim a => G.Vector Vector (UnboxViaPrim a) where {-# INLINE basicUnsafeThaw #-} {-# INLINE basicLength #-} {-# INLINE basicUnsafeSlice #-} - {-# INLINE basicUnsafeIndexM #-} + {-# INLINE basicUnsafeIndexM# #-} {-# INLINE elemseq #-} basicUnsafeFreeze = coerce $ G.basicUnsafeFreeze @P.Vector @a basicUnsafeThaw = coerce $ G.basicUnsafeThaw @P.Vector @a basicLength = coerce $ G.basicLength @P.Vector @a basicUnsafeSlice = coerce $ G.basicUnsafeSlice @P.Vector @a - basicUnsafeIndexM = coerce $ G.basicUnsafeIndexM @P.Vector @a + basicUnsafeIndexM# = coerce $ G.basicUnsafeIndexM# @P.Vector @a basicUnsafeCopy = coerce $ G.basicUnsafeCopy @P.Vector @a elemseq _ = seq @@ -389,8 +393,8 @@ instance (IsoUnbox a b, Unbox b) => G.Vector Vector (As a b) where basicUnsafeCopy = coerce $ G.basicUnsafeCopy @Vector @b elemseq _ = seq -- Conversion to/from underlying representation - {-# INLINE basicUnsafeIndexM #-} - basicUnsafeIndexM (V_UnboxAs v) i = As . fromURepr <$> G.basicUnsafeIndexM v i + {-# INLINE basicUnsafeIndexM# #-} + basicUnsafeIndexM# (V_UnboxAs v) i = As . fromURepr <$> G.basicUnsafeIndexM# v i newtype instance MVector s Int = MV_Int (P.MVector s Int) @@ -524,13 +528,13 @@ instance G.Vector Vector Bool where {-# INLINE basicUnsafeThaw #-} {-# INLINE basicLength #-} {-# INLINE basicUnsafeSlice #-} - {-# INLINE basicUnsafeIndexM #-} + {-# INLINE basicUnsafeIndexM# #-} {-# INLINE elemseq #-} basicUnsafeFreeze (MV_Bool v) = V_Bool `liftM` G.basicUnsafeFreeze v basicUnsafeThaw (V_Bool v) = MV_Bool `liftM` G.basicUnsafeThaw v basicLength (V_Bool v) = G.basicLength v basicUnsafeSlice i n (V_Bool v) = V_Bool $ G.basicUnsafeSlice i n v - basicUnsafeIndexM (V_Bool v) i = toBool `liftM` G.basicUnsafeIndexM v i + basicUnsafeIndexM# (V_Bool v) i = toBool `liftM` G.basicUnsafeIndexM# v i basicUnsafeCopy (MV_Bool mv) (V_Bool v) = G.basicUnsafeCopy mv v elemseq _ = seq @@ -582,10 +586,10 @@ instance (Unbox a) => G.Vector Vector (Complex a) where basicLength = coerce $ G.basicLength @Vector @(a,a) basicUnsafeSlice = coerce $ G.basicUnsafeSlice @Vector @(a,a) basicUnsafeCopy = coerce $ G.basicUnsafeCopy @Vector @(a,a) - {-# INLINE basicUnsafeIndexM #-} + {-# INLINE basicUnsafeIndexM# #-} {-# INLINE elemseq #-} - basicUnsafeIndexM (V_Complex v) i - = uncurry (:+) <$> G.basicUnsafeIndexM v i + basicUnsafeIndexM# (V_Complex v) i + = uncurry (:+) <$> G.basicUnsafeIndexM# v i elemseq _ (x :+ y) z = G.elemseq (undefined :: Vector a) x $ G.elemseq (undefined :: Vector a) y z @@ -707,9 +711,9 @@ instance (Unbox a, Unbox b) => G.Vector Vector (Arg a b) where basicLength = coerce $ G.basicLength @Vector @(a,b) basicUnsafeSlice = coerce $ G.basicUnsafeSlice @Vector @(a,b) basicUnsafeCopy = coerce $ G.basicUnsafeCopy @Vector @(a,b) - {-# INLINE basicUnsafeIndexM #-} + {-# INLINE basicUnsafeIndexM# #-} {-# INLINE elemseq #-} - basicUnsafeIndexM (V_Arg v) i = uncurry Arg `liftM` G.basicUnsafeIndexM v i + basicUnsafeIndexM# (V_Arg v) i = uncurry Arg `liftM` G.basicUnsafeIndexM# v i elemseq _ (Arg x y) z = G.elemseq (undefined :: Vector a) x $ G.elemseq (undefined :: Vector b) y z @@ -797,13 +801,13 @@ instance St.Storable a => G.Vector Vector (UnboxViaStorable a) where {-# INLINE basicUnsafeThaw #-} {-# INLINE basicLength #-} {-# INLINE basicUnsafeSlice #-} - {-# INLINE basicUnsafeIndexM #-} + {-# INLINE basicUnsafeIndexM# #-} {-# INLINE elemseq #-} basicUnsafeFreeze = coerce $ G.basicUnsafeFreeze @St.Vector @a basicUnsafeThaw = coerce $ G.basicUnsafeThaw @St.Vector @a basicLength = coerce $ G.basicLength @St.Vector @a basicUnsafeSlice = coerce $ G.basicUnsafeSlice @St.Vector @a - basicUnsafeIndexM = coerce $ G.basicUnsafeIndexM @St.Vector @a + basicUnsafeIndexM# = coerce $ G.basicUnsafeIndexM# @St.Vector @a basicUnsafeCopy = coerce $ G.basicUnsafeCopy @St.Vector @a elemseq _ = seq @@ -884,13 +888,13 @@ instance G.Vector Vector (DoNotUnboxLazy a) where {-# INLINE basicUnsafeThaw #-} {-# INLINE basicLength #-} {-# INLINE basicUnsafeSlice #-} - {-# INLINE basicUnsafeIndexM #-} + {-# INLINE basicUnsafeIndexM# #-} {-# INLINE elemseq #-} basicUnsafeFreeze = coerce $ G.basicUnsafeFreeze @B.Vector @a basicUnsafeThaw = coerce $ G.basicUnsafeThaw @B.Vector @a basicLength = coerce $ G.basicLength @B.Vector @a basicUnsafeSlice = coerce $ G.basicUnsafeSlice @B.Vector @a - basicUnsafeIndexM = coerce $ G.basicUnsafeIndexM @B.Vector @a + basicUnsafeIndexM# = coerce $ G.basicUnsafeIndexM# @B.Vector @a basicUnsafeCopy = coerce $ G.basicUnsafeCopy @B.Vector @a elemseq _ = seq @@ -968,13 +972,13 @@ instance G.Vector Vector (DoNotUnboxStrict a) where {-# INLINE basicUnsafeThaw #-} {-# INLINE basicLength #-} {-# INLINE basicUnsafeSlice #-} - {-# INLINE basicUnsafeIndexM #-} + {-# INLINE basicUnsafeIndexM# #-} {-# INLINE elemseq #-} basicUnsafeFreeze = coerce $ G.basicUnsafeFreeze @S.Vector @a basicUnsafeThaw = coerce $ G.basicUnsafeThaw @S.Vector @a basicLength = coerce $ G.basicLength @S.Vector @a basicUnsafeSlice = coerce $ G.basicUnsafeSlice @S.Vector @a - basicUnsafeIndexM = coerce $ G.basicUnsafeIndexM @S.Vector @a + basicUnsafeIndexM# = coerce $ G.basicUnsafeIndexM# @S.Vector @a basicUnsafeCopy = coerce $ G.basicUnsafeCopy @S.Vector @a elemseq _ = seq @@ -1053,13 +1057,13 @@ instance NFData a => G.Vector Vector (DoNotUnboxNormalForm a) where {-# INLINE basicUnsafeThaw #-} {-# INLINE basicLength #-} {-# INLINE basicUnsafeSlice #-} - {-# INLINE basicUnsafeIndexM #-} + {-# INLINE basicUnsafeIndexM# #-} {-# INLINE elemseq #-} basicUnsafeFreeze = coerce $ G.basicUnsafeFreeze @S.Vector @a basicUnsafeThaw = coerce $ G.basicUnsafeThaw @S.Vector @a basicLength = coerce $ G.basicLength @S.Vector @a basicUnsafeSlice = coerce $ G.basicUnsafeSlice @S.Vector @a - basicUnsafeIndexM = coerce $ G.basicUnsafeIndexM @S.Vector @a + basicUnsafeIndexM# = coerce $ G.basicUnsafeIndexM# @S.Vector @a basicUnsafeCopy = coerce $ G.basicUnsafeCopy @S.Vector @a elemseq _ x y = rnf (coerce x :: a) `seq` y @@ -1203,11 +1207,11 @@ instance (Unbox a, Unbox b) => G.Vector Vector (a, b) where basicUnsafeSlice i_ m_ (V_2 _ as bs) = V_2 m_ (G.basicUnsafeSlice i_ m_ as) (G.basicUnsafeSlice i_ m_ bs) - {-# INLINE basicUnsafeIndexM #-} - basicUnsafeIndexM (V_2 _ as bs) i_ + {-# INLINE basicUnsafeIndexM# #-} + basicUnsafeIndexM# (V_2 _ as bs) i_ = do - a <- G.basicUnsafeIndexM as i_ - b <- G.basicUnsafeIndexM bs i_ + a <- G.basicUnsafeIndexM# as i_ + b <- G.basicUnsafeIndexM# bs i_ return (a, b) {-# INLINE basicUnsafeCopy #-} basicUnsafeCopy (MV_2 _ as1 bs1) (V_2 _ as2 bs2) @@ -1331,12 +1335,12 @@ instance (Unbox a, = V_3 m_ (G.basicUnsafeSlice i_ m_ as) (G.basicUnsafeSlice i_ m_ bs) (G.basicUnsafeSlice i_ m_ cs) - {-# INLINE basicUnsafeIndexM #-} - basicUnsafeIndexM (V_3 _ as bs cs) i_ + {-# INLINE basicUnsafeIndexM# #-} + basicUnsafeIndexM# (V_3 _ as bs cs) i_ = do - a <- G.basicUnsafeIndexM as i_ - b <- G.basicUnsafeIndexM bs i_ - c <- G.basicUnsafeIndexM cs i_ + a <- G.basicUnsafeIndexM# as i_ + b <- G.basicUnsafeIndexM# bs i_ + c <- G.basicUnsafeIndexM# cs i_ return (a, b, c) {-# INLINE basicUnsafeCopy #-} basicUnsafeCopy (MV_3 _ as1 bs1 cs1) (V_3 _ as2 bs2 cs2) @@ -1487,13 +1491,13 @@ instance (Unbox a, (G.basicUnsafeSlice i_ m_ bs) (G.basicUnsafeSlice i_ m_ cs) (G.basicUnsafeSlice i_ m_ ds) - {-# INLINE basicUnsafeIndexM #-} - basicUnsafeIndexM (V_4 _ as bs cs ds) i_ + {-# INLINE basicUnsafeIndexM# #-} + basicUnsafeIndexM# (V_4 _ as bs cs ds) i_ = do - a <- G.basicUnsafeIndexM as i_ - b <- G.basicUnsafeIndexM bs i_ - c <- G.basicUnsafeIndexM cs i_ - d <- G.basicUnsafeIndexM ds i_ + a <- G.basicUnsafeIndexM# as i_ + b <- G.basicUnsafeIndexM# bs i_ + c <- G.basicUnsafeIndexM# cs i_ + d <- G.basicUnsafeIndexM# ds i_ return (a, b, c, d) {-# INLINE basicUnsafeCopy #-} basicUnsafeCopy (MV_4 _ as1 bs1 cs1 ds1) (V_4 _ as2 @@ -1678,14 +1682,14 @@ instance (Unbox a, (G.basicUnsafeSlice i_ m_ cs) (G.basicUnsafeSlice i_ m_ ds) (G.basicUnsafeSlice i_ m_ es) - {-# INLINE basicUnsafeIndexM #-} - basicUnsafeIndexM (V_5 _ as bs cs ds es) i_ - = do - a <- G.basicUnsafeIndexM as i_ - b <- G.basicUnsafeIndexM bs i_ - c <- G.basicUnsafeIndexM cs i_ - d <- G.basicUnsafeIndexM ds i_ - e <- G.basicUnsafeIndexM es i_ + {-# INLINE basicUnsafeIndexM# #-} + basicUnsafeIndexM# (V_5 _ as bs cs ds es) i_ + = do + a <- G.basicUnsafeIndexM# as i_ + b <- G.basicUnsafeIndexM# bs i_ + c <- G.basicUnsafeIndexM# cs i_ + d <- G.basicUnsafeIndexM# ds i_ + e <- G.basicUnsafeIndexM# es i_ return (a, b, c, d, e) {-# INLINE basicUnsafeCopy #-} basicUnsafeCopy (MV_5 _ as1 bs1 cs1 ds1 es1) (V_5 _ as2 @@ -1896,15 +1900,15 @@ instance (Unbox a, (G.basicUnsafeSlice i_ m_ ds) (G.basicUnsafeSlice i_ m_ es) (G.basicUnsafeSlice i_ m_ fs) - {-# INLINE basicUnsafeIndexM #-} - basicUnsafeIndexM (V_6 _ as bs cs ds es fs) i_ - = do - a <- G.basicUnsafeIndexM as i_ - b <- G.basicUnsafeIndexM bs i_ - c <- G.basicUnsafeIndexM cs i_ - d <- G.basicUnsafeIndexM ds i_ - e <- G.basicUnsafeIndexM es i_ - f <- G.basicUnsafeIndexM fs i_ + {-# INLINE basicUnsafeIndexM# #-} + basicUnsafeIndexM# (V_6 _ as bs cs ds es fs) i_ + = do + a <- G.basicUnsafeIndexM# as i_ + b <- G.basicUnsafeIndexM# bs i_ + c <- G.basicUnsafeIndexM# cs i_ + d <- G.basicUnsafeIndexM# ds i_ + e <- G.basicUnsafeIndexM# es i_ + f <- G.basicUnsafeIndexM# fs i_ return (a, b, c, d, e, f) {-# INLINE basicUnsafeCopy #-} basicUnsafeCopy (MV_6 _ as1 bs1 cs1 ds1 es1 fs1) (V_6 _ as2 diff --git a/vector/src/Data/Vector/Unsafe.hs b/vector/src/Data/Vector/Unsafe.hs index 13e73a0c..5e368ff8 100644 --- a/vector/src/Data/Vector/Unsafe.hs +++ b/vector/src/Data/Vector/Unsafe.hs @@ -1,6 +1,7 @@ {-# LANGUAGE BangPatterns #-} {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE MagicHash #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE TypeFamilies #-} @@ -46,7 +47,7 @@ import qualified Control.Applicative as Applicative import qualified Data.Foldable as Foldable import qualified Data.Traversable as Traversable -import qualified GHC.Exts as Exts (IsList(..)) +import qualified GHC.Exts as Exts (IsList(..), Int(..)) -- | Lazy boxed vectors, supporting efficient slicing. @@ -112,8 +113,8 @@ instance G.Vector Vector a where {-# INLINE basicUnsafeSlice #-} basicUnsafeSlice j n (UnsafeVector i _ arr) = UnsafeVector (i+j) n arr - {-# INLINE basicUnsafeIndexM #-} - basicUnsafeIndexM (UnsafeVector i _ arr) j = indexArrayM arr (i+j) + {-# INLINE basicUnsafeIndexM# #-} + basicUnsafeIndexM# (UnsafeVector i _ arr) j = indexArrayM arr (i + Exts.I# j) {-# INLINE basicUnsafeCopy #-} basicUnsafeCopy (UnsafeMVector i n dst) (UnsafeVector j _ src) @@ -287,10 +288,10 @@ instance Foldable.Foldable Vector where instance Traversable.Traversable Vector where {-# INLINE traverse #-} traverse = G.traverse - + {-# INLINE mapM #-} mapM = G.mapM - + {-# INLINE sequence #-} sequence = G.sequence