on master help sign in
Remove peekWith and pokeWith helper functions in Unbox
eee2654Adithya Kumar
core/src/Streamly/Internal/Data/Array.hs+4−8
@@ -114,11 +114,7 @@ import Data.Word (Word8)
114import Foreign.C.String (CString)
114import Foreign.C.String (CString)
115import Foreign.Ptr (castPtr)
115import Foreign.Ptr (castPtr)
116import Foreign.Storable (Storable)
116import Foreign.Storable (Storable)
117−import Streamly.Internal.Data.Unbox
117+import Streamly.Internal.Data.Unbox (Unbox(..))
118− ( Unbox
119− , peekWith
120− , sizeOf
121− )
122import Prelude hiding (length, null, last, map, (!!), read, concat)
118import Prelude hiding (length, null, last, map, (!!), read, concat)
123
119
124import Streamly.Internal.Data.Array.Mut.Type (ArrayUnsafe(..))
120import Streamly.Internal.Data.Array.Mut.Type (ArrayUnsafe(..))
@@ -239,7 +235,7 @@ readerUnsafe = Unfold step inject
239 --
235 --
240 -- This should be safe as the array contents are guaranteed to be
236 -- This should be safe as the array contents are guaranteed to be
241 -- evaluated/written to before we peek at them.
237 -- evaluated/written to before we peek at them.
242− let !x = unsafeInlineIO $ peekWith contents p
238+ let !x = unsafeInlineIO $ peekByteIndex p contents
243 let !p1 = INDEX_NEXT(p,a)
239 let !p1 = INDEX_NEXT(p,a)
244 return $ D.Yield x (ArrayUnsafe contents end p1)
240 return $ D.Yield x (ArrayUnsafe contents end p1)
245
241
@@ -263,7 +259,7 @@ getIndexRev i arr =
263 $ do
259 $ do
264 let elemPtr = RINDEX_OF(arrEnd arr, i, a)
260 let elemPtr = RINDEX_OF(arrEnd arr, i, a)
265 if i >= 0 && elemPtr >= arrStart arr
261 if i >= 0 && elemPtr >= arrStart arr
266− then Just <$> peekWith (arrContents arr) elemPtr
262+ then Just <$> peekByteIndex elemPtr (arrContents arr)
267 else return Nothing
263 else return Nothing
268
264
269-- |
265-- |
@@ -425,7 +421,7 @@ getIndex i arr =
425 $ do
421 $ do
426 let elemPtr = INDEX_OF(arrStart arr, i, a)
422 let elemPtr = INDEX_OF(arrStart arr, i, a)
427 if i >= 0 && INDEX_VALID(elemPtr, arrEnd arr, a)
423 if i >= 0 && INDEX_VALID(elemPtr, arrEnd arr, a)
428− then Just <$> peekWith (arrContents arr) elemPtr
424+ then Just <$> peekByteIndex elemPtr (arrContents arr)
429 else return Nothing
425 else return Nothing
430
426
431-- | Given a stream of array indices, read the elements on those indices from
427-- | Given a stream of array indices, read the elements on those indices from
core/src/Streamly/Internal/Data/Array/Mut/Type.hs+38−41
@@ -235,11 +235,8 @@ import Foreign.C.Types (CSize(..), CInt(..))
235import Foreign.Ptr (plusPtr, minusPtr, nullPtr)
235import Foreign.Ptr (plusPtr, minusPtr, nullPtr)
236import Streamly.Internal.Data.Unbox
236import Streamly.Internal.Data.Unbox
237 ( MutableByteArray(..)
237 ( MutableByteArray(..)
238− , Unbox
238+ , Unbox(..)
239 , getMutableByteArray#
239 , getMutableByteArray#
240− , peekWith
241− , pokeWith
242− , sizeOf
243 , touch
240 , touch
244 )
241 )
245import GHC.Base
242import GHC.Base
@@ -497,7 +494,7 @@ putIndexUnsafe :: forall m a. (MonadIO m, Unbox a)
497putIndexUnsafe i MutArray{..} x = do
494putIndexUnsafe i MutArray{..} x = do
498 let index = INDEX_OF(arrStart, i, a)
495 let index = INDEX_OF(arrStart, i, a)
499 assert (i >= 0 && INDEX_VALID(index, arrEnd, a)) (return ())
496 assert (i >= 0 && INDEX_VALID(index, arrEnd, a)) (return ())
500− liftIO $ pokeWith arrContents index x
497+ liftIO $ pokeByteIndex index arrContents x
501
498
502invalidIndex :: String -> Int -> a
499invalidIndex :: String -> Int -> a
503invalidIndex label i =
500invalidIndex label i =
@@ -515,7 +512,7 @@ putIndex :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> a -> m ()
515putIndex i MutArray{..} x = do
512putIndex i MutArray{..} x = do
516 let index = INDEX_OF(arrStart,i,a)
513 let index = INDEX_OF(arrStart,i,a)
517 if i >= 0 && INDEX_VALID(index,arrEnd,a)
514 if i >= 0 && INDEX_VALID(index,arrEnd,a)
518− then liftIO $ pokeWith arrContents index x
515+ then liftIO $ pokeByteIndex index arrContents x
519 else invalidIndex "putIndex" i
516 else invalidIndex "putIndex" i
520
517
521-- | Write an input stream of (index, value) pairs to an array. Throws an
518-- | Write an input stream of (index, value) pairs to an array. Throws an
@@ -541,9 +538,9 @@ modifyIndexUnsafe :: forall m a b. (MonadIO m, Unbox a) =>
541modifyIndexUnsafe i MutArray{..} f = liftIO $ do
538modifyIndexUnsafe i MutArray{..} f = liftIO $ do
542 let index = INDEX_OF(arrStart,i,a)
539 let index = INDEX_OF(arrStart,i,a)
543 assert (i >= 0 && INDEX_NEXT(index,a) <= arrEnd) (return ())
540 assert (i >= 0 && INDEX_NEXT(index,a) <= arrEnd) (return ())
544− r <- peekWith arrContents index
541+ r <- peekByteIndex index arrContents
545 let (x, res) = f r
542 let (x, res) = f r
546− pokeWith arrContents index x
543+ pokeByteIndex index arrContents x
547 return res
544 return res
548
545
549-- | Modify a given index of an array using a modifier function.
546-- | Modify a given index of an array using a modifier function.
@@ -555,9 +552,9 @@ modifyIndex i MutArray{..} f = do
555 let index = INDEX_OF(arrStart,i,a)
552 let index = INDEX_OF(arrStart,i,a)
556 if i >= 0 && INDEX_VALID(index,arrEnd,a)
553 if i >= 0 && INDEX_VALID(index,arrEnd,a)
557 then liftIO $ do
554 then liftIO $ do
558− r <- peekWith arrContents index
555+ r <- peekByteIndex index arrContents
559 let (x, res) = f r
556 let (x, res) = f r
560− pokeWith arrContents index x
557+ pokeByteIndex index arrContents x
561 return res
558 return res
562 else invalidIndex "modifyIndex" i
559 else invalidIndex "modifyIndex" i
563
560
@@ -591,8 +588,8 @@ modify MutArray{..} f = liftIO $
591
588
592 go i =
589 go i =
593 when (INDEX_VALID(i,arrEnd,a)) $ do
590 when (INDEX_VALID(i,arrEnd,a)) $ do
594− r <- peekWith arrContents i
591+ r <- peekByteIndex i arrContents
595− pokeWith arrContents i (f r)
592+ pokeByteIndex i arrContents (f r)
596 go (INDEX_NEXT(i,a))
593 go (INDEX_NEXT(i,a))
597
594
598-- XXX We could specify the number of bytes to swap instead of Proxy. Need
595-- XXX We could specify the number of bytes to swap instead of Proxy. Need
@@ -606,10 +603,10 @@ swapArrayByteIndices ::
606 -> Int
603 -> Int
607 -> IO ()
604 -> IO ()
608swapArrayByteIndices _ arrContents i1 i2 = do
605swapArrayByteIndices _ arrContents i1 i2 = do
609− r1 <- peekWith arrContents i1
606+ r1 <- peekByteIndex i1 arrContents
610− r2 <- peekWith arrContents i2
607+ r2 <- peekByteIndex i2 arrContents
611− pokeWith arrContents i1 (r2 :: a)
608+ pokeByteIndex i1 arrContents (r2 :: a)
612− pokeWith arrContents i2 (r1 :: a)
609+ pokeByteIndex i2 arrContents (r1 :: a)
613
610
614-- | Swap the elements at two indices without validating the indices.
611-- | Swap the elements at two indices without validating the indices.
615--
612--
@@ -882,7 +879,7 @@ rightSize arr@MutArray{..} = do
882snocNewEnd :: (MonadIO m, Unbox a) => Int -> MutArray a -> a -> m (MutArray a)
879snocNewEnd :: (MonadIO m, Unbox a) => Int -> MutArray a -> a -> m (MutArray a)
883snocNewEnd newEnd arr@MutArray{..} x = liftIO $ do
880snocNewEnd newEnd arr@MutArray{..} x = liftIO $ do
884 assert (newEnd <= arrBound) (return ())
881 assert (newEnd <= arrBound) (return ())
885− pokeWith arrContents arrEnd x
882+ pokeByteIndex arrEnd arrContents x
886 return $ arr {arrEnd = newEnd}
883 return $ arr {arrEnd = newEnd}
887
884
888-- | Really really unsafe, appends the element into the first array, may
885-- | Really really unsafe, appends the element into the first array, may
@@ -996,7 +993,7 @@ getIndexUnsafe :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m a
996getIndexUnsafe i MutArray{..} = do
993getIndexUnsafe i MutArray{..} = do
997 let index = INDEX_OF(arrStart,i,a)
994 let index = INDEX_OF(arrStart,i,a)
998 assert (i >= 0 && INDEX_VALID(index,arrEnd,a)) (return ())
995 assert (i >= 0 && INDEX_VALID(index,arrEnd,a)) (return ())
999− liftIO $ peekWith arrContents index
996+ liftIO $ peekByteIndex index arrContents
1000
997
1001-- | /O(1)/ Lookup the element at the given index. Index starts from 0.
998-- | /O(1)/ Lookup the element at the given index. Index starts from 0.
1002--
999--
@@ -1005,7 +1002,7 @@ getIndex :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m (Maybe a)
1005getIndex i MutArray{..} = do
1002getIndex i MutArray{..} = do
1006 let index = INDEX_OF(arrStart,i,a)
1003 let index = INDEX_OF(arrStart,i,a)
1007 if i >= 0 && INDEX_VALID(index,arrEnd,a)
1004 if i >= 0 && INDEX_VALID(index,arrEnd,a)
1008− then liftIO $ Just <$> peekWith arrContents index
1005+ then liftIO $ Just <$> peekByteIndex index arrContents
1009 else return Nothing
1006 else return Nothing
1010
1007
1011-- | /O(1)/ Lookup the element at the given index from the end of the array.
1008-- | /O(1)/ Lookup the element at the given index from the end of the array.
@@ -1018,7 +1015,7 @@ getIndexRev :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m a
1018getIndexRev i MutArray{..} = do
1015getIndexRev i MutArray{..} = do
1019 let index = RINDEX_OF(arrEnd,i,a)
1016 let index = RINDEX_OF(arrEnd,i,a)
1020 if i >= 0 && index >= arrStart
1017 if i >= 0 && index >= arrStart
1021− then liftIO $ peekWith arrContents index
1018+ then liftIO $ peekByteIndex index arrContents
1022 else invalidIndex "getIndexRev" i
1019 else invalidIndex "getIndexRev" i
1023
1020
1024data GetIndicesState contents start end st =
1021data GetIndicesState contents start end st =
@@ -1158,7 +1155,7 @@ partitionBy f arr@MutArray{..} = liftIO $ do
1158
1155
1159 -- Invariant low < high on entry, and on return as well
1156 -- Invariant low < high on entry, and on return as well
1160 moveHigh low high = do
1157 moveHigh low high = do
1161− h <- peekWith arrContents high
1158+ h <- peekByteIndex high arrContents
1162 if f h
1159 if f h
1163 then
1160 then
1164 -- Correctly classified, continue the loop
1161 -- Correctly classified, continue the loop
@@ -1178,7 +1175,7 @@ partitionBy f arr@MutArray{..} = liftIO $ do
1178 -- low <= high
1175 -- low <= high
1179 -- Both low and high are valid locations within the array
1176 -- Both low and high are valid locations within the array
1180 go low high = do
1177 go low high = do
1181− l <- peekWith arrContents low
1178+ l <- peekByteIndex low arrContents
1182 if f l
1179 if f l
1183 then
1180 then
1184 -- low is wrongly classified
1181 -- low is wrongly classified
@@ -1189,8 +1186,8 @@ partitionBy f arr@MutArray{..} = liftIO $ do
1189 case r of
1186 case r of
1190 Nothing -> return low
1187 Nothing -> return low
1191 Just (high1, h) -> do -- low < high1
1188 Just (high1, h) -> do -- low < high1
1192− pokeWith arrContents low h
1189+ pokeByteIndex low arrContents h
1193− pokeWith arrContents high1 l
1190+ pokeByteIndex high1 arrContents l
1194 let low1 = INDEX_NEXT(low,a)
1191 let low1 = INDEX_NEXT(low,a)
1195 high2 = INDEX_PREV(high1,a)
1192 high2 = INDEX_PREV(high1,a)
1196 if low1 <= high2
1193 if low1 <= high2
@@ -1334,7 +1331,7 @@ chunksOf n (D.Stream step state) =
1334 r <- step (adaptState gst) st
1331 r <- step (adaptState gst) st
1335 case r of
1332 case r of
1336 D.Yield x s -> do
1333 D.Yield x s -> do
1337− liftIO $ pokeWith contents end x
1334+ liftIO $ pokeByteIndex end contents x
1338 let end1 = INDEX_NEXT(end,a)
1335 let end1 = INDEX_NEXT(end,a)
1339 return $
1336 return $
1340 if end1 >= bound
1337 if end1 >= bound
@@ -1396,7 +1393,7 @@ flattenArrays (D.Stream step state) = D.Stream step' (OuterLoop state)
1396 return $ D.Skip $ OuterLoop st
1393 return $ D.Skip $ OuterLoop st
1397
1394
1398 step' _ (InnerLoop st contents p end) = do
1395 step' _ (InnerLoop st contents p end) = do
1399− x <- liftIO $ peekWith contents p
1396+ x <- liftIO $ peekByteIndex p contents
1400 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
1397 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
1401
1398
1402-- | Use the "readerRev" unfold instead.
1399-- | Use the "readerRev" unfold instead.
@@ -1426,7 +1423,7 @@ flattenArraysRev (D.Stream step state) = D.Stream step' (OuterLoop state)
1426 return $ D.Skip $ OuterLoop st
1423 return $ D.Skip $ OuterLoop st
1427
1424
1428 step' _ (InnerLoop st contents p start) = do
1425 step' _ (InnerLoop st contents p start) = do
1429− x <- liftIO $ peekWith contents p
1426+ x <- liftIO $ peekByteIndex p contents
1430 let cur = INDEX_PREV(p,a)
1427 let cur = INDEX_PREV(p,a)
1431 return $ D.Yield x (InnerLoop st contents cur start)
1428 return $ D.Yield x (InnerLoop st contents cur start)
1432
1429
@@ -1465,7 +1462,7 @@ producerWith liftio = Producer step (return . toArrayUnsafe) extract
1465 -- few actions for correctness and execution order sanity. We want
1462 -- few actions for correctness and execution order sanity. We want
1466 -- the peek to occur right here and not lazily at some later point
1463 -- the peek to occur right here and not lazily at some later point
1467 -- because we want the peek to be ordered with respect to the touch.
1464 -- because we want the peek to be ordered with respect to the touch.
1468− !x <- liftio $ peekWith contents cur
1465+ !x <- liftio $ peekByteIndex cur contents
1469 return $ D.Yield x (ArrayUnsafe contents (INDEX_NEXT(cur,a)) end)
1466 return $ D.Yield x (ArrayUnsafe contents (INDEX_NEXT(cur,a)) end)
1470
1467
1471 extract = return . fromArrayUnsafe
1468 extract = return . fromArrayUnsafe
@@ -1496,7 +1493,7 @@ readerRevWith liftio = Unfold step inject
1496 {-# INLINE_LATE step #-}
1493 {-# INLINE_LATE step #-}
1497 step (ArrayUnsafe _ start p) | p < start = return D.Stop
1494 step (ArrayUnsafe _ start p) | p < start = return D.Stop
1498 step (ArrayUnsafe contents start p) = do
1495 step (ArrayUnsafe contents start p) = do
1499− !x <- liftio $ peekWith contents p
1496+ !x <- liftio $ peekByteIndex p contents
1500 return $ D.Yield x (ArrayUnsafe contents start (INDEX_PREV(p,a)))
1497 return $ D.Yield x (ArrayUnsafe contents start (INDEX_PREV(p,a)))
1501
1498
1502-- | Unfold an array into a stream in reverse order.
1499-- | Unfold an array into a stream in reverse order.
@@ -1527,7 +1524,7 @@ toListFB c n MutArray{..} = go arrStart
1527 -- evaluated/written to before we peek at them.
1524 -- evaluated/written to before we peek at them.
1528 -- XXX
1525 -- XXX
1529 let !x = unsafeInlineIO $ do
1526 let !x = unsafeInlineIO $ do
1530− r <- peekWith arrContents p
1527+ r <- peekByteIndex arrContents p
1531 return r
1528 return r
1532 in c x (go (PTR_NEXT(p,a)))
1529 in c x (go (PTR_NEXT(p,a)))
1533-}
1530-}
@@ -1544,7 +1541,7 @@ toList MutArray{..} = liftIO $ go arrStart
1544
1541
1545 go p | assert (p <= arrEnd) (p == arrEnd) = return []
1542 go p | assert (p <= arrEnd) (p == arrEnd) = return []
1546 go p = do
1543 go p = do
1547− x <- peekWith arrContents p
1544+ x <- peekByteIndex p arrContents
1548 (:) x <$> go (INDEX_NEXT(p,a))
1545 (:) x <$> go (INDEX_NEXT(p,a))
1549
1546
1550{-# INLINE_NORMAL toStreamDWith #-}
1547{-# INLINE_NORMAL toStreamDWith #-}
@@ -1558,7 +1555,7 @@ toStreamDWith liftio MutArray{..} = D.Stream step arrStart
1558 {-# INLINE_LATE step #-}
1555 {-# INLINE_LATE step #-}
1559 step _ p | assert (p <= arrEnd) (p == arrEnd) = return D.Stop
1556 step _ p | assert (p <= arrEnd) (p == arrEnd) = return D.Stop
1560 step _ p = liftio $ do
1557 step _ p = liftio $ do
1561− r <- peekWith arrContents p
1558+ r <- peekByteIndex p arrContents
1562 return $ D.Yield r (INDEX_NEXT(p,a))
1559 return $ D.Yield r (INDEX_NEXT(p,a))
1563
1560
1564-- | Convert a 'MutArray' into a stream.
1561-- | Convert a 'MutArray' into a stream.
@@ -1579,7 +1576,7 @@ toStreamKWith liftio MutArray{..} = go arrStart
1579
1576
1580 go p | assert (p <= arrEnd) (p == arrEnd) = K.nil
1577 go p | assert (p <= arrEnd) (p == arrEnd) = K.nil
1581 | otherwise =
1578 | otherwise =
1582− let elemM = peekWith arrContents p
1579+ let elemM = peekByteIndex p arrContents
1583 in liftio elemM `K.consM` go (INDEX_NEXT(p,a))
1580 in liftio elemM `K.consM` go (INDEX_NEXT(p,a))
1584
1581
1585{-# INLINE toStreamK #-}
1582{-# INLINE toStreamK #-}
@@ -1599,7 +1596,7 @@ toStreamDRevWith liftio MutArray{..} =
1599 {-# INLINE_LATE step #-}
1596 {-# INLINE_LATE step #-}
1600 step _ p | p < arrStart = return D.Stop
1597 step _ p | p < arrStart = return D.Stop
1601 step _ p = liftio $ do
1598 step _ p = liftio $ do
1602− r <- peekWith arrContents p
1599+ r <- peekByteIndex p arrContents
1603 return $ D.Yield r (INDEX_PREV(p,a))
1600 return $ D.Yield r (INDEX_PREV(p,a))
1604
1601
1605-- | Convert a 'MutArray' into a stream in reverse order.
1602-- | Convert a 'MutArray' into a stream in reverse order.
@@ -1622,7 +1619,7 @@ toStreamKRevWith liftio MutArray {..} =
1622
1619
1623 go p | p < arrStart = K.nil
1620 go p | p < arrStart = K.nil
1624 | otherwise =
1621 | otherwise =
1625− let elemM = peekWith arrContents p
1622+ let elemM = peekByteIndex p arrContents
1626 in liftio elemM `K.consM` go (INDEX_PREV(p,a))
1623 in liftio elemM `K.consM` go (INDEX_PREV(p,a))
1627
1624
1628{-# INLINE toStreamKRev #-}
1625{-# INLINE toStreamKRev #-}
@@ -1690,7 +1687,7 @@ writeAppendNUnsafe n action =
1690 return $ toArrayUnsafe arr1
1687 return $ toArrayUnsafe arr1
1691
1688
1692 step (ArrayUnsafe contents start end) x = do
1689 step (ArrayUnsafe contents start end) x = do
1693− liftIO $ pokeWith contents end x
1690+ liftIO $ pokeByteIndex end contents x
1694 return $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
1691 return $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
1695
1692
1696-- | Append @n@ elements to an existing array. Any free space left in the array
1693-- | Append @n@ elements to an existing array. Any free space left in the array
@@ -1750,7 +1747,7 @@ writeNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
1750 initial = toArrayUnsafe <$> alloc (max n 0)
1747 initial = toArrayUnsafe <$> alloc (max n 0)
1751
1748
1752 step (ArrayUnsafe contents start end) x = do
1749 step (ArrayUnsafe contents start end) x = do
1753− liftIO $ pokeWith contents end x
1750+ liftIO $ pokeByteIndex end contents x
1754 return
1751 return
1755 $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
1752 $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
1756
1753
@@ -1806,7 +1803,7 @@ writeRevNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
1806
1803
1807 step (ArrayUnsafe contents start end) x = do
1804 step (ArrayUnsafe contents start end) x = do
1808 let ptr = INDEX_PREV(start,a)
1805 let ptr = INDEX_PREV(start,a)
1809− liftIO $ pokeWith contents ptr x
1806+ liftIO $ pokeByteIndex ptr contents x
1810 return
1807 return
1811 $ ArrayUnsafe contents ptr end
1808 $ ArrayUnsafe contents ptr end
1812
1809
@@ -1938,7 +1935,7 @@ fromStreamDN limit str = do
1938 where
1935 where
1939
1936
1940 fwrite arrContents ptr x = do
1937 fwrite arrContents ptr x = do
1941− liftIO $ pokeWith arrContents ptr x
1938+ liftIO $ pokeByteIndex ptr arrContents x
1942 return $ INDEX_NEXT(ptr,a)
1939 return $ INDEX_NEXT(ptr,a)
1943
1940
1944-- | Create a 'MutArray' from the first N elements of a list. The array is
1941-- | Create a 'MutArray' from the first N elements of a list. The array is
@@ -2328,7 +2325,7 @@ strip eq arr@MutArray{..} = liftIO $ do
2328 getStart cur = do
2325 getStart cur = do
2329 if cur < arrEnd
2326 if cur < arrEnd
2330 then do
2327 then do
2331− r <- peekWith arrContents cur
2328+ r <- peekByteIndex cur arrContents
2332 if eq r
2329 if eq r
2333 then getStart (INDEX_NEXT(cur,a))
2330 then getStart (INDEX_NEXT(cur,a))
2334 else return cur
2331 else return cur
@@ -2338,7 +2335,7 @@ strip eq arr@MutArray{..} = liftIO $ do
2338 if cur > low
2335 if cur > low
2339 then do
2336 then do
2340 let prev = INDEX_PREV(cur,a)
2337 let prev = INDEX_PREV(cur,a)
2341− r <- peekWith arrContents prev
2338+ r <- peekByteIndex prev arrContents
2342 if eq r
2339 if eq r
2343 then getLast prev low
2340 then getLast prev low
2344 else return cur
2341 else return cur
core/src/Streamly/Internal/Data/Array/Type.hs+4−4
@@ -95,7 +95,7 @@ import GHC.Ptr (Ptr(..))
95import Streamly.Internal.Data.Array.Mut.Type (MutArray(..), MutableByteArray)
95import Streamly.Internal.Data.Array.Mut.Type (MutArray(..), MutableByteArray)
96import Streamly.Internal.Data.Fold.Type (Fold(..))
96import Streamly.Internal.Data.Fold.Type (Fold(..))
97import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
97import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
98−import Streamly.Internal.Data.Unbox (Unbox, peekWith, sizeOf)
98+import Streamly.Internal.Data.Unbox (Unbox(..))
99import Streamly.Internal.Data.Unfold.Type (Unfold(..))
99import Streamly.Internal.Data.Unfold.Type (Unfold(..))
100import Text.Read (readPrec)
100import Text.Read (readPrec)
101
101
@@ -447,8 +447,8 @@ toListFB c n Array{..} = go arrStart
447 -- accumulating the list and fusing better with the pure consumers.
447 -- accumulating the list and fusing better with the pure consumers.
448 --
448 --
449 -- This should be safe as the array contents are guaranteed to be
449 -- This should be safe as the array contents are guaranteed to be
450− -- evaluated/written to before we peekWith at them.
450+ -- evaluated/written to before we peekByteIndex at them.
451− let !x = unsafeInlineIO $ peekWith arrContents p
451+ let !x = unsafeInlineIO $ peekByteIndex p arrContents
452 in c x (go (INDEX_NEXT(p,a)))
452 in c x (go (INDEX_NEXT(p,a)))
453
453
454-- | Convert an 'Array' into a list.
454-- | Convert an 'Array' into a list.
@@ -636,7 +636,7 @@ _toStreamD_ size Array{..} = D.Stream step arrStart
636 {-# INLINE_LATE step #-}
636 {-# INLINE_LATE step #-}
637 step _ p | p == arrEnd = return D.Stop
637 step _ p | p == arrEnd = return D.Stop
638 step _ p = liftIO $ do
638 step _ p = liftIO $ do
639− x <- peekWith arrContents p
639+ x <- peekByteIndex p arrContents
640 return $ D.Yield x (p + size)
640 return $ D.Yield x (p + size)
641
641
642{-
642{-
core/src/Streamly/Internal/Data/Fold/Chunked.hs+3−3
@@ -63,7 +63,7 @@ import Control.Exception (assert)
63import Control.Monad.IO.Class (MonadIO(..))
63import Control.Monad.IO.Class (MonadIO(..))
64import Data.Bifunctor (first)
64import Data.Bifunctor (first)
65import Data.Proxy (Proxy(..))
65import Data.Proxy (Proxy(..))
66−import Streamly.Internal.Data.Unbox (peekWith, sizeOf, Unbox)
66+import Streamly.Internal.Data.Unbox (Unbox(..))
67import GHC.Types (SPEC(..))
67import GHC.Types (SPEC(..))
68import Streamly.Internal.Data.Array.Mut.Type (touch)
68import Streamly.Internal.Data.Array.Mut.Type (touch)
69import Streamly.Internal.Data.Array.Type (Array(..))
69import Streamly.Internal.Data.Array.Type (Array(..))
@@ -123,7 +123,7 @@ fromFold (Fold.Fold fstep finitial fextract) =
123 assert (cur == end) (return ())
123 assert (cur == end) (return ())
124 return $ Partial 0 fs
124 return $ Partial 0 fs
125 goArray !_ !cur !fs = do
125 goArray !_ !cur !fs = do
126− x <- liftIO $ peekWith contents cur
126+ x <- liftIO $ peekByteIndex cur contents
127 res <- fstep fs x
127 res <- fstep fs x
128 let elemSize = SIZE_OF(a)
128 let elemSize = SIZE_OF(a)
129 next = INDEX_NEXT(cur,a)
129 next = INDEX_NEXT(cur,a)
@@ -160,7 +160,7 @@ fromParserD (ParserD.Parser step1 initial1 extract1) =
160 else return $ st (arrRem + n) fs1
160 else return $ st (arrRem + n) fs1
161
161
162 goArray !_ !cur !fs = do
162 goArray !_ !cur !fs = do
163− x <- liftIO $ peekWith contents cur
163+ x <- liftIO $ peekByteIndex cur contents
164 liftIO $ touch contents
164 liftIO $ touch contents
165 res <- step1 fs x
165 res <- step1 fs x
166 let elemSize = SIZE_OF(a)
166 let elemSize = SIZE_OF(a)
core/src/Streamly/Internal/Data/IORef/Unboxed.hs+5−7
@@ -42,11 +42,9 @@ import Data.Proxy (Proxy(..))
42import Control.Monad.IO.Class (MonadIO(..))
42import Control.Monad.IO.Class (MonadIO(..))
43import Streamly.Internal.Data.Unbox
43import Streamly.Internal.Data.Unbox
44 ( MutableByteArray(..)
44 ( MutableByteArray(..)
45− , Unbox
45+ , Unbox(..)
46− , sizeOf
47− , peekWith
48− , pokeWith
49 , newUnpinnedBytes
46 , newUnpinnedBytes
47+ , sizeOf
50 )
48 )
51
49
52import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
50import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
@@ -61,7 +59,7 @@ newtype IORef a = IORef MutableByteArray
61newIORef :: forall a. Unbox a => a -> IO (IORef a)
59newIORef :: forall a. Unbox a => a -> IO (IORef a)
62newIORef x = do
60newIORef x = do
63 var <- newUnpinnedBytes (sizeOf (Proxy :: Proxy a))
61 var <- newUnpinnedBytes (sizeOf (Proxy :: Proxy a))
64− pokeWith var 0 x
62+ pokeByteIndex 0 var x
65 return $ IORef var
63 return $ IORef var
66
64
67-- | Write a value to an 'IORef'.
65-- | Write a value to an 'IORef'.
@@ -69,14 +67,14 @@ newIORef x = do
69-- /Pre-release/
67-- /Pre-release/
70{-# INLINE writeIORef #-}
68{-# INLINE writeIORef #-}
71writeIORef :: Unbox a => IORef a -> a -> IO ()
69writeIORef :: Unbox a => IORef a -> a -> IO ()
72−writeIORef (IORef var) = pokeWith var 0
70+writeIORef (IORef var) = pokeByteIndex 0 var
73
71
74-- | Read a value from an 'IORef'.
72-- | Read a value from an 'IORef'.
75--
73--
76-- /Pre-release/
74-- /Pre-release/
77{-# INLINE readIORef #-}
75{-# INLINE readIORef #-}
78readIORef :: Unbox a => IORef a -> IO a
76readIORef :: Unbox a => IORef a -> IO a
79−readIORef (IORef var) = peekWith var 0
77+readIORef (IORef var) = peekByteIndex 0 var
80
78
81-- | Modify the value of an 'IORef' using a function with strict application.
79-- | Modify the value of an 'IORef' using a function with strict application.
82--
80--
core/src/Streamly/Internal/Data/Parser/ParserK/Type.hs+2−2
@@ -46,7 +46,7 @@ import Control.Monad.IO.Class (MonadIO, liftIO)
46import Data.Proxy (Proxy(..))
46import Data.Proxy (Proxy(..))
47import GHC.Types (SPEC(..))
47import GHC.Types (SPEC(..))
48import Streamly.Internal.Data.Array.Type (Array(..))
48import Streamly.Internal.Data.Array.Type (Array(..))
49−import Streamly.Internal.Data.Unbox (peekWith, sizeOf, Unbox)
49+import Streamly.Internal.Data.Unbox (Unbox(..))
50import Streamly.Internal.System.IO (unsafeInlineIO)
50import Streamly.Internal.System.IO (unsafeInlineIO)
51
51
52import qualified Control.Monad.Fail as Fail
52import qualified Control.Monad.Fail as Fail
@@ -430,7 +430,7 @@ parseDToK pstep initial extract cont !offset0 !usedCount !input = do
430 go !_ !cur !pst | cur >= end =
430 go !_ !cur !pst | cur >= end =
431 onContinue ((end - start) `div` SIZE_OF(a)) pst
431 onContinue ((end - start) `div` SIZE_OF(a)) pst
432 go !_ !cur !pst = do
432 go !_ !cur !pst = do
433− let !x = unsafeInlineIO $ peekWith contents cur
433+ let !x = unsafeInlineIO $ peekByteIndex cur contents
434 pRes <- pstep pst x
434 pRes <- pstep pst x
435 let elemSize = SIZE_OF(a)
435 let elemSize = SIZE_OF(a)
436 next = INDEX_NEXT(cur,a)
436 next = INDEX_NEXT(cur,a)
core/src/Streamly/Internal/Data/Ring/Unboxed.hs+3−3
@@ -79,7 +79,7 @@ import Foreign.Storable
79import Foreign.ForeignPtr (ForeignPtr, withForeignPtr, touchForeignPtr)
79import Foreign.ForeignPtr (ForeignPtr, withForeignPtr, touchForeignPtr)
80import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
80import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
81import Foreign.Ptr (plusPtr, minusPtr, castPtr)
81import Foreign.Ptr (plusPtr, minusPtr, castPtr)
82−import Streamly.Internal.Data.Unbox as Unboxed (Unbox, peekWith)
82+import Streamly.Internal.Data.Unbox as Unboxed (Unbox(peekByteIndex))
83import GHC.ForeignPtr (mallocPlainForeignPtrAlignedBytes)
83import GHC.ForeignPtr (mallocPlainForeignPtrAlignedBytes)
84import GHC.Ptr (Ptr(..))
84import GHC.Ptr (Ptr(..))
85import Streamly.Internal.Data.Array.Mut.Type (MutArray)
85import Streamly.Internal.Data.Array.Mut.Type (MutArray)
@@ -411,7 +411,7 @@ unsafeEqArrayN Ring{..} rh A.Array{..} nBytes
411
411
412 check p i = do
412 check p i = do
413 (relem :: Word8) <- peek p
413 (relem :: Word8) <- peek p
414− aelem <- peekWith w8Contents i
414+ aelem <- peekByteIndex i w8Contents
415 if relem == aelem
415 if relem == aelem
416 then go (p `plusPtr` 1) (i + 1)
416 then go (p `plusPtr` 1) (i + 1)
417 else return False
417 else return False
@@ -445,7 +445,7 @@ unsafeEqArray Ring{..} rh A.Array{..} =
445
445
446 check p i = do
446 check p i = do
447 (relem :: Word8) <- peek p
447 (relem :: Word8) <- peek p
448− aelem <- peekWith w8Contents i
448+ aelem <- peekByteIndex i w8Contents
449 if relem == aelem
449 if relem == aelem
450 then go (p `plusPtr` 1) (i + 1)
450 then go (p `plusPtr` 1) (i + 1)
451 else return False
451 else return False
core/src/Streamly/Internal/Data/Stream/Chunked.hs+7−7
@@ -69,7 +69,7 @@ import Control.Exception (assert)
69import Control.Monad.IO.Class (MonadIO(..))
69import Control.Monad.IO.Class (MonadIO(..))
70import Data.Proxy (Proxy(..))
70import Data.Proxy (Proxy(..))
71import Data.Word (Word8)
71import Data.Word (Word8)
72−import Streamly.Internal.Data.Unbox (Unbox, peekWith, sizeOf)
72+import Streamly.Internal.Data.Unbox (Unbox(..))
73import Fusion.Plugin.Types (Fuse(..))
73import Fusion.Plugin.Types (Fuse(..))
74import GHC.Exts (SpecConstrAnnotation(..))
74import GHC.Exts (SpecConstrAnnotation(..))
75import GHC.Types (SPEC(..))
75import GHC.Types (SPEC(..))
@@ -201,7 +201,7 @@ unlines sep (D.Stream step state) = D.Stream step' (OuterLoop state)
201 return $ D.Yield sep $ OuterLoop st
201 return $ D.Yield sep $ OuterLoop st
202
202
203 step' _ (InnerLoop st contents p end) = do
203 step' _ (InnerLoop st contents p end) = do
204− x <- liftIO $ peekWith contents p
204+ x <- liftIO $ peekByteIndex p contents
205 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
205 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
206
206
207-------------------------------------------------------------------------------
207-------------------------------------------------------------------------------
@@ -355,7 +355,7 @@ foldBreakD (FL.Fold fstep initial extract) stream@(D.Stream step state) = do
355 | cur == end = do
355 | cur == end = do
356 go SPEC s fs
356 go SPEC s fs
357 goArray !_ st fp@(Tuple' end contents) !cur !fs = do
357 goArray !_ st fp@(Tuple' end contents) !cur !fs = do
358− x <- liftIO $ peekWith contents cur
358+ x <- liftIO $ peekByteIndex cur contents
359 res <- fstep fs x
359 res <- fstep fs x
360 let next = INDEX_NEXT(cur,a)
360 let next = INDEX_NEXT(cur,a)
361 case res of
361 case res of
@@ -388,7 +388,7 @@ foldBreakK (FL.Fold fstep initial extract) stream = do
388 | cur == end = do
388 | cur == end = do
389 go fs st
389 go fs st
390 goArray !fs st fp@(Tuple' end contents) !cur = do
390 goArray !fs st fp@(Tuple' end contents) !cur = do
391− x <- liftIO $ peekWith contents cur
391+ x <- liftIO $ peekByteIndex cur contents
392 res <- fstep fs x
392 res <- fstep fs x
393 let next = INDEX_NEXT(cur,a)
393 let next = INDEX_NEXT(cur,a)
394 case res of
394 case res of
@@ -597,7 +597,7 @@ parseBreakD
597 | cur == end = do
597 | cur == end = do
598 go SPEC s backBuf pst
598 go SPEC s backBuf pst
599 gobuf !_ s backBuf fp@(Tuple' end contents) !cur !pst = do
599 gobuf !_ s backBuf fp@(Tuple' end contents) !cur !pst = do
600− x <- liftIO $ peekWith contents cur
600+ x <- liftIO $ peekByteIndex contents cur
601 pRes <- pstep pst x
601 pRes <- pstep pst x
602 let next = INDEX_NEXT(cur,a)
602 let next = INDEX_NEXT(cur,a)
603 case pRes of
603 case pRes of
@@ -669,7 +669,7 @@ parseBreakK (PRD.Parser pstep initial extract) stream = do
669 -- Use strictness on "cur" to keep it unboxed
669 -- Use strictness on "cur" to keep it unboxed
670 goArray !pst backBuf st (Array _ cur end) | cur == end = go pst st backBuf
670 goArray !pst backBuf st (Array _ cur end) | cur == end = go pst st backBuf
671 goArray !pst backBuf st (Array contents cur end) = do
671 goArray !pst backBuf st (Array contents cur end) = do
672− x <- liftIO $ peekWith contents cur
672+ x <- liftIO $ peekByteIndex cur contents
673 pRes <- pstep pst x
673 pRes <- pstep pst x
674 let next = INDEX_NEXT(cur,a)
674 let next = INDEX_NEXT(cur,a)
675 case pRes of
675 case pRes of
@@ -714,7 +714,7 @@ parseBreakK (PRD.Parser pstep initial extract) stream = do
714 goExtract !pst backBuf (Array _ cur end)
714 goExtract !pst backBuf (Array _ cur end)
715 | cur == end = goStop pst backBuf
715 | cur == end = goStop pst backBuf
716 goExtract !pst backBuf (Array contents cur end) = do
716 goExtract !pst backBuf (Array contents cur end) = do
717− x <- liftIO $ peekWith contents cur
717+ x <- liftIO $ peekByteIndex cur contents
718 pRes <- pstep pst x
718 pRes <- pstep pst x
719 let next = INDEX_NEXT(cur,a)
719 let next = INDEX_NEXT(cur,a)
720 case pRes of
720 case pRes of
core/src/Streamly/Internal/Data/Unbox.hs−14
@@ -7,8 +7,6 @@
7
7
8module Streamly.Internal.Data.Unbox
8module Streamly.Internal.Data.Unbox
9 ( Unbox(..)
9 ( Unbox(..)
10− , peekWith
11− , pokeWith
12 , MutableByteArray(..)
10 , MutableByteArray(..)
13 , touch
11 , touch
14 , getMutableByteArray#
12 , getMutableByteArray#
@@ -482,18 +480,6 @@ instance Unbox Bool where
482 {-# INLINE sizeOf #-}
480 {-# INLINE sizeOf #-}
483 sizeOf _ = 1
481 sizeOf _ = 1
484
482
485−--------------------------------------------------------------------------------
486−-- Functions
487−--------------------------------------------------------------------------------
488−
489−{-# INLINE peekWith #-}
490−peekWith :: Unbox a => MutableByteArray -> Int -> IO a
491−peekWith arr i = peekByteIndex i arr
492−
493−{-# INLINE pokeWith #-}
494−pokeWith :: Unbox a => MutableByteArray -> Int -> a -> IO ()
495−pokeWith arr i = pokeByteIndex i arr
496−
497--------------------------------------------------------------------------------
483--------------------------------------------------------------------------------
498-- Generic deriving
484-- Generic deriving
499--------------------------------------------------------------------------------
485--------------------------------------------------------------------------------
core/src/Streamly/Internal/Unicode/Stream.hs+3−3
@@ -108,7 +108,7 @@ import Streamly.Internal.Data.Stream (Stream)
108import Streamly.Internal.Data.Stream (Step (..))
108import Streamly.Internal.Data.Stream (Step (..))
109import Streamly.Internal.Data.SVar.Type (adaptState)
109import Streamly.Internal.Data.SVar.Type (adaptState)
110import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
110import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
111−import Streamly.Internal.Data.Unbox (peekWith)
111+import Streamly.Internal.Data.Unbox (Unbox(peekByteIndex))
112import Streamly.Internal.Data.Unfold.Type (Unfold(..))
112import Streamly.Internal.Data.Unfold.Type (Unfold(..))
113import Streamly.Internal.System.IO (unsafeInlineIO)
113import Streamly.Internal.System.IO (unsafeInlineIO)
114
114
@@ -745,7 +745,7 @@ decodeUtf8ArraysWithD cfm (D.Stream step state) =
745 | p == end = do
745 | p == end = do
746 return $ Skip $ OuterLoop st Nothing
746 return $ Skip $ OuterLoop st Nothing
747 step' _ _ (InnerLoopDecodeInit st contents p end) = do
747 step' _ _ (InnerLoopDecodeInit st contents p end) = do
748− x <- liftIO $ peekWith contents p
748+ x <- liftIO $ peekByteIndex p contents
749 -- Note: It is important to use a ">" instead of a "<=" test here for
749 -- Note: It is important to use a ">" instead of a "<=" test here for
750 -- GHC to generate code layout for default branch prediction for the
750 -- GHC to generate code layout for default branch prediction for the
751 -- common case. This is fragile and might change with the compiler
751 -- common case. This is fragile and might change with the compiler
@@ -779,7 +779,7 @@ decodeUtf8ArraysWithD cfm (D.Stream step state) =
779 step' _ _ (InnerLoopDecoding st _ p end sv cp)
779 step' _ _ (InnerLoopDecoding st _ p end sv cp)
780 | p == end = return $ Skip $ OuterLoop st (Just (sv, cp))
780 | p == end = return $ Skip $ OuterLoop st (Just (sv, cp))
781 step' table _ (InnerLoopDecoding st contents p end statePtr codepointPtr) = do
781 step' table _ (InnerLoopDecoding st contents p end statePtr codepointPtr) = do
782− x <- liftIO $ peekWith contents p
782+ x <- liftIO $ peekByteIndex p contents
783 let (Tuple' sv cp) = decode1 table statePtr codepointPtr x
783 let (Tuple' sv cp) = decode1 table statePtr codepointPtr x
784 return $
784 return $
785 case sv of
785 case sv of