Skip to content

Commit

Permalink
Fix #642 Bad performance/memory leak for long list literals
Browse files Browse the repository at this point in the history
A one-character fix that speeds up the formatting of long literals
and drastically reduces memory usage.

A very simple example is

```elm
module Small exposing (x)

x =
    [ 1, 2, 3
     , 4]
```

But with a list literal of many thousands of elements. The newline
forces all elements to get their own line. This is implemented as a
fold, but in practice this would often perform

```
longList ++ shortList
```

By reversing the fold (from `foldl` to `foldr`) that turns into

```
shortList ++ longList
```

Which requires less traversals and memory.
  • Loading branch information
folkertdev committed Nov 8, 2019
1 parent 1fa616f commit e7c1e74
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/Box.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ stack1 children =
[first] ->
first
boxes ->
foldl1 stack' boxes
foldr1 stack' boxes


mapLines :: (Line -> Line) -> Box -> Box
Expand Down

0 comments on commit e7c1e74

Please sign in to comment.