diff options
author | Don Stewart <dons@galois.com> | 2008-06-09 01:21:18 +0000 |
---|---|---|
committer | Don Stewart <dons@galois.com> | 2008-06-09 01:21:18 +0000 |
commit | 695f40564544130d594090f7224f468c366d3eb0 (patch) | |
tree | e3f3bf5e54a144dcde145d1c0e57d98f868e4802 /lib/Data/Packed/Internal | |
parent | a407c0e101f8f6db44fcf731ebb8460d0f691196 (diff) |
Use unpacking and inlining to ensure Vector can be stored unlifted at runtime
Diffstat (limited to 'lib/Data/Packed/Internal')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index 772aafd..7d6e3d5 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -21,9 +21,12 @@ import Foreign | |||
21 | import Complex | 21 | import Complex |
22 | import Control.Monad(when) | 22 | import Control.Monad(when) |
23 | 23 | ||
24 | import GHC.Base | ||
25 | import GHC.IOBase | ||
26 | |||
24 | -- | A one-dimensional array of objects stored in a contiguous memory block. | 27 | -- | A one-dimensional array of objects stored in a contiguous memory block. |
25 | data Vector t = V { dim :: Int -- ^ number of elements | 28 | data Vector t = V { dim :: {-# UNPACK #-} !Int -- ^ number of elements |
26 | , fptr :: ForeignPtr t -- ^ foreign pointer to the memory block | 29 | , fptr :: {-# UNPACK #-}!(ForeignPtr t) -- ^ foreign pointer to the memory block |
27 | } | 30 | } |
28 | 31 | ||
29 | vec = withVector | 32 | vec = withVector |
@@ -53,7 +56,12 @@ fromList l = unsafePerformIO $ do | |||
53 | app1 f vec v "fromList" | 56 | app1 f vec v "fromList" |
54 | return v | 57 | return v |
55 | 58 | ||
56 | safeRead v = unsafePerformIO . withForeignPtr (fptr v) | 59 | safeRead v = inlinePerformIO . withForeignPtr (fptr v) |
60 | {-# INLINE safeRead #-} | ||
61 | |||
62 | inlinePerformIO :: IO a -> a | ||
63 | inlinePerformIO (IO m) = case m realWorld# of (# _, r #) -> r | ||
64 | {-# INLINE inlinePerformIO #-} | ||
57 | 65 | ||
58 | {- | extracts the Vector elements to a list | 66 | {- | extracts the Vector elements to a list |
59 | 67 | ||
@@ -77,6 +85,7 @@ at' v n = safeRead v $ flip peekElemOff n | |||
77 | at :: Storable a => Vector a -> Int -> a | 85 | at :: Storable a => Vector a -> Int -> a |
78 | at v n | n >= 0 && n < dim v = at' v n | 86 | at v n | n >= 0 && n < dim v = at' v n |
79 | | otherwise = error "vector index out of range" | 87 | | otherwise = error "vector index out of range" |
88 | {-# INLINE at #-} | ||
80 | 89 | ||
81 | {- | takes a number of consecutive elements from a Vector | 90 | {- | takes a number of consecutive elements from a Vector |
82 | 91 | ||