Ticket #1038 (closed bug: fixed)

Opened 6 years ago

Last modified 5 years ago

selector thunks not working? space leak in standard example

Reported by: tullsen@… Owned by: simonmar
Priority: normal Milestone: 6.8.1
Component: Runtime System Version: 6.6
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: x86
Type of failure: Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

I thought that GHC evaluates "THUNK_SELECTORS" and thus eliminates the space leak in this infamous example [Hughes83][Wadler87][Sparud93]:

module Main where

surprise xs = b1 ++ [0,0] ++ b2
              where
              (b1,b2) = break (==0) xs

strictFromTo :: Int -> Int -> [Int]
strictFromTo i m = takeWhile (<= m) $ strictFrom i

strictFrom :: Int -> [Int]
strictFrom x = let x' = x + 1 in seq x' $ x' : strictFrom x'
        
main = print (length $ surprise $ strictFromTo (-1000000) 1)
       -- space leak, w/ or w/o optimization
main2 = print (length $ strictFromTo (-1000000) 1)
       -- runs in constant space, w/ or w/o optimization

However, this program does not run in constant space, even with -O2. I get the same results on

GHC6.6 Mac OS X Intel GHC6.2.1 Linux Intel GHC6.4.1 Linux Intel

Attachments

1038.patch Download (23.0 KB) - added by simonmar 6 years ago.

Change History

Changed 6 years ago by igloo

  • milestone set to 6.8

I thought we already had a bug report about this, but I can't find it now.

As I recall, GHC does implement an optimisation designed to fix this leak, but it only looks down the thunk/selector chain to a bounded depth before bailing out, and thunks/selectors are created too fast. We have an idea of how it could be fixed, but haven't implemented it yet.

Changed 6 years ago by simonmar

  • owner set to simonmar

Patch attached. I'm not going to push this until after 6.8.1, as it needs plenty of testing. For 6.8.1 I'm going to simply bump the depth limit for selector thunks, which also makes the example run in constant space, though more slowly.

Changed 6 years ago by simonmar

Changed 6 years ago by guest

Changed 6 years ago by simonmar

  • status changed from new to closed
  • resolution set to fixed

Now pushed, don't merge to the branch.

Mon Sep 17 16:18:34 BST 2007  Simon Marlow <simonmar@microsoft.com>
  * FIX #1038: failure of selector-thunk machinery to do its job

and a couple of subsequent fixes.

Changed 6 years ago by igloo

  • milestone changed from 6.8 branch to 6.8.1

Changed 5 years ago by simonmar

  • os changed from Multiple to Unknown/Multiple
Note: See TracTickets for help on using tickets.