What is the leak of space?

I found a haskell wiki page on space leaks that claims to list examples of real leaks that aren't there. In fact, he does not say what a leak of space is; it simply links to a page for memory leaks.

What is the leak of space?

+4
source share
2 answers

As @Rasko noted in the answer, a space leak refers to a situation where a program or a particular calculation uses more (usually much more) memory than is necessary for the calculation and / or expected by the programmer.

Haskell, , , - ( , IO ) , , .

. Haskell:

main = print $ sum [1..1000000000]

. -O2, ( , , ).

, , , , , Haskell . , , , , ( , Haskell).

, , , - , , , , , . , "" - , . (, , : , .)

Soooo... Haskell , ( ) " " , .

, , , :

main = let vals = [1..1000000000]
       in print (sum vals, length vals)

, (, , 13Gigs, , ).

. - , , , , " " , , . , , vals, , "" . sum vals, , , length vals .

:

main = do txt <- getContents
          print (length txt, length (words txt))

, 10- , 100 , , . , , - txt - txt , Haskell String ( ), , , length txt , , length (words txt).

, :

main = do txt <- getContents
          print $ length txt

main = do txt <- getContents
          print $ length (words txt)

.

, , , , , , . :

{-# LANGUAGE BangPatterns #-}

import Data.List
import Data.Char

charsWords :: String -> (Int, Int)
charsWords str = let (_, chrs, wrds) = foldl' step (False, 0, 0) str
                 in (chrs, wrds)
  where step (inWord, cs, ws) c =
          let !cs' = succ cs
              !ws' = if not inWord && inWord' then succ ws else ws
              !inWord' = not (isSpace c)
          in (inWord', cs', ws')

main = do txt <- getContents
          print $ charsWords txt

( bang (!) length words) , , Haskell. , foldl' foldl ( foldr foldr' !), cs' ws' , inWord' ( ) ..

+8

, , . , , , , , , . *

+6

Source: https://habr.com/ru/post/1684922/


All Articles