gpt4 book ai didi

optimization - 解包严格字段如何与多态性结合在一起?

转载 作者:行者123 更新时间:2023-12-03 15:28:28 26 4
gpt4 key购买 nike

{-# UNPACK #-} pragma 告诉编译器消除多余的构造函数。报价 Haskell wiki :

For example, given this:

data T = T {-# UNPACK #-} !(Int,Float)

GHC will represent the type T like this:

data T = T Int Float

eliminating the tuple. This is commonly used to put unboxed Ints directly in a constructor:

data T = T {-# UNPACK #-} !Int

will be represented as

data T = T Int#


我想知道, 当要解包的字段是多态的时,这是否也有效? 例如,如果我定义
data S' a   = S String {-# UNPACK #-} !a

type S1 = S' Int
newtype S2 = S2 (S' Int)

将使用 S1 的函数或 S2被优化?

最佳答案

引自唐的blog关于开箱

It does not make sense for polymorphic fields, since they have variable size, and the compiler does not (except in very specific circumstances) have access to information about what types are in use.



您也可以阅读 Tibell 的 reply到邮件列表。

Unfortunately unpacking doesn't work for polymorphic fields (the new warning for ineffective unpack pragmas in GHC head should warn about this) ...

关于optimization - 解包严格字段如何与多态性结合在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13098375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com