Skip to content

Generics deriving is quadratic

I'm compiling some code with many sum type alternatives and it's absurdly slow.

{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics (Generic)

data D = D1 | D2 | ... | D400 deriving (Generic)

main = return ()

I did a little benchmark and it's actually precisely O(n²) in the number of alternatives.

Easy repro: https://github.com/nh2/ghc-generics-deriving-is-slow/

I assume that this is a bug and it should be linear.

Note that it is also quadratic in memory usage.

Edited by Niklas Hambüchen
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information