Skip to content

Binary instances are too large

See also

Consider this example, taken from here in #21839 (closed), and the perf test in !8734 (closed).

{-# LANGUAGE DeriveGeneric #-}

{-# OPTIONS_GHC #-}
module Example
  ( -- PathComponent(..)
   PathTemplateVariable(..)
  ) where

import GHC.Generics
import Data.Typeable
import Data.Binary

data PathTemplateVariable =
       VarA
     | VarB
     | VarC
     | VarD
     | Var1
     | Var2
     | Var3
     | Var4
     | Var5
     | Var6
     | Var7
     | Var8  
     -- etc
  deriving (Generic)

instance Binary PathTemplateVariable

This compiles significantly slower with HEAD than with ghc-9.4. See #21839 (closed).

I have established that there is zero difference from the deriving( Generic ). It's all the Binary instance.

This is odd. Really, the binary instance for PathTemplateVariable should boil down to

put VarA = write 1#
put VarB = write 2#
...
put Var8 = write 77#

It's a bit bonkers that it's so voluminous, even in ghc-9.4, let alone HEAD.

Understanding what's going on would require digging into

  • The binary library
  • The bytestring library
  • The Generics implementation so it's not the work of a moment. But it could be rewarding. Lots of people would be thrilled if these binary instances weren't so crushingly huge.
Edited by Simon Peyton Jones
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information