Opened 3 years ago

Last modified 3 years ago

#12817 new bug

Degraded performance with constraint synonyms

Reported by: crockeea Owned by:
Priority: normal Milestone:
Component: Compiler Version: 8.0.1
Keywords: Cc: mpickering, RyanGlScott
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 crockeea)

The attached bug demonstrates how using constraint synonyms can negatively impact performance.

I have two functions which are identical, except that one uses a constraint synonym:

type CElt r = (Num r, Eq r)

fooSlow :: (CElt r) => Bar r -> Bar r
{-# INLINABLE fooSlow #-}
fooSlow (C1 u) = either (fromEitherBar . eitherFoo) (fromEitherBar . eitherFoo) u
fooSlow (C2 c) = C2 $ fooSlow c

fooFast :: (Num r, Eq r) => Bar r -> Bar r
{-# INLINABLE fooFast #-}
fooFast (C1 u) = either (fromEitherBar . eitherFoo) (fromEitherBar . eitherFoo) u
fooFast (C2 c) = C2 $ fooFast c

Using criterion, fooSlow is about 3x slower than fooFast. Main.hs needs criterion and deepseq, but the problem should be evident just from inspecting the core of Bar.hs, which only relies on base. Code was compiled with GHC-8.0.1 with -O2.

For some context, in my real code from which this example was extracted, the 'fast' (no constraint synonym) version takes 78 microseconds, while the 'slow' version with the synonym takes 360 microseconds.

Attachments (1)

csynbug.tar.gz (1.3 KB) - added by crockeea 3 years ago.

Download all attachments as: .zip

Change History (5)

Changed 3 years ago by crockeea

Attachment: csynbug.tar.gz added

comment:1 Changed 3 years ago by mpickering

Cc: mpickering added

comment:2 Changed 3 years ago by crockeea

Description: modified (diff)

comment:3 Changed 3 years ago by RyanGlScott

Cc: RyanGlScott added

comment:4 Changed 3 years ago by RyanGlScott

Does the fix for #12791 (748b79741652028827b6225c36b8ab55d22bdeb0) help here?

Note: See TracTickets for help on using tickets.