This one earned some infamy because the patch sped up cabal update (cabal is the Haskell package manager) incredibly: The patch claims improvements from 80s to 0.01s.

Despite the patch being initially-inscrutable Haskell, this is actually a instance of an incredibly-common accidentally-quadratic bug.

The key part of the old code was

foldr (flip (buf_append bufOps)) (buf_empty bufOps) strs

There’s a bunch of noise here, but basically this is trying to take an array of byte buffers ( strs ), and flatten them into a single buffer. It does so via a foldr of buf_append , starting with buf_empty – the empty buffer.

Since buf_append copies both underlying buffers into a new buffer, this will repeatedly copy data as it foldr’s. If the total data is length n, and the pieces are roughly constant-sized (say, one TCP packet a piece, or one socket buffer apiece), this will do O(n*n/k) = O(n²) work. It’s the exact same problem as building up long strings in Java using += instead of StringBuilder .

The fix switches to buf_concat , which takes the whole list and directly produces a new output buffer:

buf_concat bufOps $ reverse strs

Since buf_concat can walk the list once to determine the allocation size, and then copy each piece of data once, this restores linear performance.

I really like this one because even though Haskell performance and syntax can be totally inscrutable, once you dig into it, it’s actually the same super-common bug class that you find in a lot of imperative code.