Skip to content

Commit

Permalink
Make fromListN functions good consumers
Browse files Browse the repository at this point in the history
...in terms of list fusion.
  • Loading branch information
meooow25 committed Nov 17, 2024
1 parent 2e95fd0 commit 390d468
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
9 changes: 5 additions & 4 deletions Data/Primitive/Array.hs
Original file line number Diff line number Diff line change
Expand Up @@ -587,17 +587,18 @@ mapArray' f a =
-- | Create an array from a list of a known length. If the length
-- of the list does not match the given length, this throws an exception.
arrayFromListN :: Int -> [a] -> Array a
{-# INLINE arrayFromListN #-} -- Inline for list fusion
arrayFromListN n l =
createArray n (die "fromListN" "uninitialized element") $ \sma ->
let go !ix [] = if ix == n
let z !ix = if ix == n
then return ()
else die "fromListN" "list length less than specified size"
go !ix (x : xs) = if ix < n
f x k = GHC.Exts.oneShot $ \ !ix -> if ix < n

Check failure on line 596 in Data/Primitive/Array.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

Not in scope: ‘GHC.Exts.oneShot’
then do
writeArray sma ix x
go (ix+1) xs
k (ix+1)
else die "fromListN" "list length greater than specified size"
in go 0 l
in foldr f z l 0

-- | Create an array from a list.
arrayFromList :: [a] -> Array a
Expand Down
9 changes: 5 additions & 4 deletions Data/Primitive/ByteArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,17 @@ byteArrayFromList xs = byteArrayFromListN (length xs) xs
-- | Create a 'ByteArray' from a list of a known length. If the length
-- of the list does not match the given length, this throws an exception.
byteArrayFromListN :: forall a. Prim a => Int -> [a] -> ByteArray
{-# INLINE byteArrayFromListN #-} -- Inline for list fusion
byteArrayFromListN n ys = createByteArray (n * sizeOfType @a) $ \marr ->
let go !ix [] = if ix == n
let z !ix = if ix == n
then return ()
else die "byteArrayFromListN" "list length less than specified size"
go !ix (x : xs) = if ix < n
f x k = GHC.Exts.oneShot $ \ !ix -> if ix < n
then do
writeByteArray marr ix x
go (ix + 1) xs
k (ix + 1)
else die "byteArrayFromListN" "list length greater than specified size"
in go 0 ys
in foldr f z ys 0

unI# :: Int -> Int#
unI# (I# n#) = n#
Expand Down
9 changes: 5 additions & 4 deletions Data/Primitive/PrimArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,17 @@ primArrayFromList vs = primArrayFromListN (L.length vs) vs
-- | Create a 'PrimArray' from a list of a known length. If the length
-- of the list does not match the given length, this throws an exception.
primArrayFromListN :: forall a. Prim a => Int -> [a] -> PrimArray a
{-# INLINE primArrayFromListN #-} -- Inline for list fusion
primArrayFromListN len vs = createPrimArray len $ \arr ->
let go [] !ix = if ix == len
let z !ix = if ix == len
then return ()
else die "fromListN" "list length less than specified size"
go (a : as) !ix = if ix < len
f a k = GHC.Exts.oneShot $ \ !ix -> if ix < len
then do
writePrimArray arr ix a
go as (ix + 1)
k (ix + 1)
else die "fromListN" "list length greater than specified size"
in go vs 0
in foldr f z vs 0

-- | Convert a 'PrimArray' to a list.
{-# INLINE primArrayToList #-}
Expand Down
9 changes: 5 additions & 4 deletions Data/Primitive/SmallArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -924,18 +924,19 @@ instance (Typeable s, Typeable a) => Data (SmallMutableArray s a) where
-- | Create a 'SmallArray' from a list of a known length. If the length
-- of the list does not match the given length, this throws an exception.
smallArrayFromListN :: Int -> [a] -> SmallArray a
{-# INLINE smallArrayFromListN #-} -- Inline for list fusion
smallArrayFromListN n l =
createSmallArray n
(die "smallArrayFromListN" "uninitialized element") $ \sma ->
let go !ix [] = if ix == n
let z !ix = if ix == n
then return ()
else die "smallArrayFromListN" "list length less than specified size"
go !ix (x : xs) = if ix < n
f x k = GHC.Exts.oneShot $ \ !ix -> if ix < n
then do
writeSmallArray sma ix x
go (ix + 1) xs
k (ix + 1)
else die "smallArrayFromListN" "list length greater than specified size"
in go 0 l
in foldr f z l 0

-- | Create a 'SmallArray' from a list.
smallArrayFromList :: [a] -> SmallArray a
Expand Down

0 comments on commit 390d468

Please sign in to comment.