Skip to content

Commit

Permalink
Internal.Array: Don't return MArrays from loops (#377)
Browse files Browse the repository at this point in the history
This prevents unnecessary MArray boxing.
  • Loading branch information
sjakobi authored Mar 14, 2022
1 parent 7237826 commit 5ea4197
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions Data/HashMap/Internal/Array.hs
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,10 @@ map f = \ ary ->
in run $ do
mary <- new_ n
go ary mary 0 n
return mary
where
go ary mary i n
| i >= n = return mary
| i >= n = return ()
| otherwise = do
x <- indexM ary i
write mary i $ f x
Expand All @@ -458,9 +459,10 @@ map' f = \ ary ->
in run $ do
mary <- new_ n
go ary mary 0 n
return mary
where
go ary mary i n
| i >= n = return mary
| i >= n = return ()
| otherwise = do
x <- indexM ary i
write mary i $! f x
Expand All @@ -473,21 +475,23 @@ fromList n xs0 =
run $ do
mary <- new_ n
go xs0 mary 0
return mary
where
go [] !mary !_ = return mary
go (x:xs) mary i = do write mary i x
go xs mary (i+1)
go [] !_ !_ = return ()
go (x:xs) mary i = do write mary i x
go xs mary (i+1)

fromList' :: Int -> [a] -> Array a
fromList' n xs0 =
CHECK_EQ("fromList'", n, Prelude.length xs0)
run $ do
mary <- new_ n
go xs0 mary 0
return mary
where
go [] !mary !_ = return mary
go (!x:xs) mary i = do write mary i x
go xs mary (i+1)
go [] !_ !_ = return ()
go (!x:xs) mary i = do write mary i x
go xs mary (i+1)

-- | @since 0.2.17.0
instance TH.Lift a => TH.Lift (Array a) where
Expand Down

0 comments on commit 5ea4197

Please sign in to comment.