Skip to content

Revamp derived Eq instance code generation (#17240)

Alex D requested to merge nineonine/ghc:wip/T17240 into master

#17240 (closed)

This patch changes code generation for derived Eq instances by comparing constructor tags. For this data type

data T = A | B Int | C Char

We now generate this code:

    (==) a b =
      case dataToTag# a /= dataToTag# b of
        True -> False
        False -> case a of       -- Here we already know that tags match
            B a1 -> case b of
                B b1 -> a1 == b1 -- Only one branch
            C a1 -> case b of
                C b1 -> a1 == b1 -- Only one branch
            _ -> True            -- catch-all to match all nullary ctors

This produces less code - once we know that the tags match, we only need to emit 1 branch per constructor. It is also reasonable to think this should execute faster.

  • update Note
  • Add test
  • Squash / Update commit message
Edited by Alex D

Merge request reports