Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8675457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:57:14+00:00 2026-06-12T19:57:14+00:00

According to this article , Enumerations don’t count as single-constructor types as far as

  • 0

According to this article,

Enumerations don’t count as single-constructor types as far as GHC is concerned, so they don’t benefit from unpacking when used as strict constructor fields, or strict function arguments. This is a deficiency in GHC, but it can be worked around.

And instead the use of newtypes is recommended. However, I cannot verify this with the following code:

{-# LANGUAGE MagicHash,BangPatterns #-}
{-# OPTIONS_GHC  -O2 -funbox-strict-fields -rtsopts -fllvm -optlc --x86-asm-syntax=intel #-}
module Main(main,f,g)
where       
import GHC.Base  
import Criterion.Main

data D = A | B | C
newtype E = E Int deriving(Eq)

f :: D -> Int#
f z | z `seq` False = 3422#
f z = case z of
  A -> 1234#
  B -> 5678#
  C -> 9012#

g :: E -> Int#
g z | z `seq` False = 7432#
g z = case z of
  (E 0) -> 2345#
  (E 1) -> 6789#
  (E 2) -> 3535#

f' x = I# (f x)
g' x = I# (g x)

main :: IO ()
main = defaultMain [ bench "f" (whnf f' A) 
                   , bench "g" (whnf g' (E 0)) 
                   ]

Looking at the assembly, the tags for each constructor of the enumeration D is actually unpacked and directly hard-coded in the instruction. Furthermore, the function f lacks error-handling code, and more than 10% faster than g. In a more realistic case I have also experienced a slowdown after converting a enumeration to a newtype. Can anyone give me some insight about this? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:57:15+00:00Added an answer on June 12, 2026 at 7:57 pm

    It depends on the use case. For the functions you have, it’s expected that the enumeration performs better. Basically, the three constructors of D become Ints resp. Int#s when the strictness analysis allows that, and GHC knows it’s statically checked that the argument can only have one of the three values 0#, 1#, 2#, so it needs not insert error handling code for f. For E, the static guarantee of only one of three values being possible isn’t given, so it needs to add error handling code for g, that slows things down significantly. If you change the definition of g so that the last case becomes

    E _ -> 3535#
    

    the difference vanishes completely or almost completely (I get a 1% – 2% better benchmark for f still, but I haven’t done enough testing to be sure whether that’s a real difference or an artifact of benchmarking).

    But this is not the use case the wiki page is talking about. What it’s talking about is unpacking the constructors into other constructors when the type is a component of other data, e.g.

    data FooD = FD !D !D !D
    
    data FooE = FE !E !E !E
    

    Then, if compiled with -funbox-strict-fields, the three Int#s can be unpacked into the constructor of FooE, so you’d basically get the equivalent of

    struct FooE {
        long x, y, z;
    };
    

    while the fields of FooD have the multi-constructor type D and cannot be unpacked into the constructor FD(1), so that would basically give you

    struct FooD {
        long *px, *py, *pz;
    }
    

    That can obviously have significant impact.

    I’m not sure about the case of single-constructor function arguments. That has obvious advantages for types with contained data, like tuples, but I don’t see how that would apply to plain enumerations, where you just have a case and splitting off a worker and a wrapper makes no sense (to me).

    Anyway, the worker/wrapper transformation isn’t so much a single-constructor thing, constructor specialisation can give the same benefit to types with few constructors. (For how many constructors specialisations would be created depends on the value of -fspec-constr-count.)


    (1) That might have changed, but I doubt it. I haven’t checked it though, so it’s possible the page is out of date.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to this article: http://subsonicproject.com/docs/3.0_Migrations Bottom line: if you're a developer that is concerned
According this article , generic JPA DAO(Data Access Object) is a pretty nice pattern.
According to this article , it says: Use a delegate in the following circumstances:
According to this article , it's possible to get multiline XML comments -- instead
According to this article about writing shell extensions in .Net, inheriting the shell interfaces
I am doing a tutorial according to this article . This is basic Hello
I've got a PHP-fpm setup on nginx setup according to this article: http://interfacelab.com/nginx-php-fpm-apc-awesome/ PHP
According to this article and a number of other documents, JDBC resources are deployed
according to this article http://electricbeach.org/?p=653 and many others it's trivial to make a layout
I implemented Dynamic DataSource Routing for Spring+Hibernate according to this article . I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.