Opened 5 years ago

Closed 4 years ago

Last modified 2 years ago

#10535 closed bug (duplicate)

Float out causes major space leak

Reported by: AlexET Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.10.1
Keywords: FloatOut Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #7367 #7206 Differential Rev(s):
Wiki Page:

Description (last modified by AlexET)

This issue originally was on haskell-cafe

The following code has a space leak

numbers = [1..200]

replicateM' :: Monad m => Int -> m a -> m [a]
replicateM' 0 xs = return []
replicateM' n xs = do
  a <- xs
  b <- replicateM' (n-1) xs
  return (a:b)

test :: [[Int]]
test = replicateM' 4 numbers

main = print test

The recursive call in replicateM' gets floated out. This then causes a very large (2003 elements) list to be kept in memory instead of being lazily produced and consumed each time.

A similar thing happens to the replicateM in Control.Monad but it is harder to spot as a lot of specialisation and fusion goes on before the float-out happens

This is similar to #7367 but in this case we have a huge space-leak instead of just adding allocations.

Change History (7)

comment:1 Changed 5 years ago by AlexET

Description: modified (diff)

comment:2 Changed 5 years ago by nomeata

Can you tell whether this is related to the state hack? I.e., does -fno-state-hack make a difference?

comment:3 Changed 5 years ago by AlexET

-fno-state-hack makes no difference

-fno-full-laziness does fix the problem.

In many ways the float out pass is doing what it is supposed to (floating out a shared value though a lambda) as it now does less work. However the list which is floated out is produced and consumed lazily and hence the floating out causes a major space leak (space leak may be the wrong term as the extra is constant but very large (1GB)).

comment:4 Changed 5 years ago by nomeata

Ah, right. Sounds like a case for :-]. Anyways, this is a tricky problem; how could the compiler detect things that are cheaper to recalculate than to share?

#7206 is also related.

comment:5 Changed 4 years ago by rwbarton

This is a frequently reported issue. The original is #917, and other tickets (#1945, #3273, #4276, #5729) have been closed as duplicates of it. #8457 has a recapitulation of the subject. Should we close this ticket as a duplicate too?

comment:6 Changed 4 years ago by simonpj

Resolution: duplicate
Status: newclosed

Yes, that makes sense. I wish I knew what to do about the full-laziness space leak problem!

comment:7 Changed 2 years ago by simonpj

Keywords: FloatOut added
Note: See TracTickets for help on using tickets.