Opened 5 years ago

Last modified 2 years ago

#9522 new feature request

SPECIALISE pragmas for derived instances

Reported by: simonpj Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.8.2
Keywords: deriving Cc: nfrisby
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Runtime performance bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by simonpj)

In package ghc-prim, in GHC.Classes we have

instance (Eq a) => Eq [a] where
    {-# SPECIALISE instance Eq [[Char]] #-}
    {-# SPECIALISE instance Eq [Char] #-}
    {-# SPECIALISE instance Eq [Int] #-}
    []     == []     = True
    (x:xs) == (y:ys) = x == y && xs == ys
    _xs    == _ys    = False

The SPECIALISE instance pragmas instantiate the code for these commonly-used types.

But for tuples we use deriving:

deriving instance (Eq  a, Eq  b) => Eq  (a, b)

and many more similar. There is no way to add a SPECIALISE instance pragma for a derived instance. This is bad, because they are heavily used.

You can see the lossage from messages lie

WARNING: file compiler/specialise/Specialise.lhs, line 673
    specImport discarding:
      GHC.Classes.$w$c== :: forall a b. (Eq a, Eq b) => a -> b -> a -> b -> Bool
        @ Module.Module @ Module.Module Module.$fEqModule Module.$fEqModule

which says that we will end up calling $w$c== for pairs of modules, passing dictionaries to compare the modules for equality. These messages show up when compiling the libraries if you build your stage1 compiler with -DDEBUG.

It should probably be possible to have a top-level

{-# SPECIALISE instance Eq (Int, Bool) #-}

even in another module (as we can now do for functions. To do this right, we'd need to make the code for derived methods INLINEALBE.

Change History (4)

comment:1 Changed 5 years ago by simonpj

Description: modified (diff)

comment:2 Changed 4 years ago by thomie

Type: bugfeature request
Type of failure: None/UnknownRuntime performance bug

comment:3 Changed 3 years ago by nfrisby

Cc: nfrisby added

comment:4 Changed 2 years ago by RyanGlScott

Keywords: deriving added
Note: See TracTickets for help on using tickets.