Skip to content

Commit 617441a

Browse files
Prevent (small?) space leak in mapAccum{L,R}
1 parent 685da60 commit 617441a

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
FIXED: The `unzip` family no longer retains a reference to the original input for every (unevaluated) part of the output tuple. This can help to prevent space leaks. See [#3038](https://github.com/clash-lang/clash-compiler/issues/3038).
1+
FIXED: The `unzip` family no longer retains a reference to the original input for every (unevaluated) part of the output tuple. Similarly, `mapAccumL` and `mapAccumR` are now also more eager to drop references. This can help to prevent space leaks. See [#3038](https://github.com/clash-lang/clash-compiler/issues/3038).

clash-prelude/src/Clash/Sized/Vector.hs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,11 +1335,10 @@ postscanr f z xs = init (scanr f z xs)
13351335
mapAccumL :: (acc -> x -> (acc,y)) -> acc -> Vec n x -> (acc,Vec n y)
13361336
mapAccumL f acc xs = (acc',ys)
13371337
where
1338-
accs = acc `Cons` accs'
1339-
ws = zipWith (flip f) xs (init accs)
1340-
accs' = map fst ws
1341-
ys = map snd ws
1342-
acc' = last accs
1338+
accs = acc `Cons` accs'
1339+
ws = zipWith (flip f) xs (init accs)
1340+
(accs', ys) = unzip ws
1341+
acc' = last accs
13431342
{-# INLINE mapAccumL #-}
13441343

13451344
-- | The 'mapAccumR' function behaves like a combination of 'map' and 'foldr';
@@ -1356,11 +1355,10 @@ mapAccumL f acc xs = (acc',ys)
13561355
mapAccumR :: (acc -> x -> (acc,y)) -> acc -> Vec n x -> (acc, Vec n y)
13571356
mapAccumR f acc xs = (acc',ys)
13581357
where
1359-
accs = accs' :< acc
1360-
ws = zipWith (flip f) xs (tail accs)
1361-
accs' = map fst ws
1362-
ys = map snd ws
1363-
acc' = head accs
1358+
accs = accs' :< acc
1359+
ws = zipWith (flip f) xs (tail accs)
1360+
(accs', ys) = unzip ws
1361+
acc' = head accs
13641362
{-# INLINE mapAccumR #-}
13651363

13661364
-- | 'zip' takes two vectors and returns a vector of corresponding pairs.

0 commit comments

Comments
 (0)