Skip to content

Compile-time regression between GHC-9.2 and GHC-9.4 on generically deriving TextShow instances

Summary

When porting a client project from ghc-9.2 (first to 9.6, then additionally to 9.4 to get more data), I noticed a pretty drastic compile time regression where the overall compile time of the project doubled (from roughly 8 to roughly 16 minutes). After a bit of investigation, I noticed that the slowdown is limited to a handful of modules. One of the worst affected modules had a number of derivations for TextShow instances using DerivingVia. So I'm singling out this part for now and reporting it as a bug.

After talking to @mpickering, I also did some additional experiments in how far the text version being used affects the overall outcome.

Steps to reproduce

Here is a program that can be used to reproduce: it depends on base and text-show. I think even a smaller program with just a few of the datatypes already demonstrates the problem, but the time differences are less clearly observable, so I've opted for the larger sample here:

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
module Slow2 where

import GHC.Int
import qualified GHC.Generics as GHC

import TextShow
import TextShow.Generic

data SM =
    CS !Int32 !Int32
  | NS !Int32
  deriving stock GHC.Generic
  deriving TextShow via FromGeneric SM

data EM =
    CE (Int, Int) (Int, Int)
  | NE (Int, Int)
  deriving stock GHC.Generic
  deriving TextShow via FromGeneric EM

data AA = K | D | M
  deriving stock GHC.Generic
  deriving TextShow via FromGeneric AA

data AWA =
    SI Int AA
  | SP Int [AWA]
  deriving stock GHC.Generic
  deriving TextShow via FromGeneric AWA

data EEP =
  EEP
    { eepP :: Int
    , eepO :: Int
    , eepB :: EM
    , eepAWA :: [[AWA]]
    }
  deriving stock GHC.Generic
  deriving TextShow via FromGeneric EEP

Expected behavior

I would hope to see no drastic regression in compile times. As it is, here is a table of compile times I have observed on my machine, split across various GHC versions and whether text-1 or text-2 is being used:

GHC version Text version Compile time
9.2.7 text-1.2.5.0 17.2s
9.4.5 text-1.2.5.0 35.1s
9.6.2 text-1.2.5.0 38.7s
9.2.7 text-2.0.2 17.6s
9.4.5 text-2.0.2 45.7s
9.6.2 text-2.0.2 50.7s

As can be seen, there is already a significant performance hit by just switching GHC versions, but it seems to be an even bigger hit if text-2 is used (which originally I upgraded at the same time as the GHC version, making the perceived hit even bigger).

Environment

All on Linux, GHC versions and text versions as specified above.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information