[英]Memory footprint of existentially quantified types and related optimization techniques
Consider the following data model utilizing an existential: 考虑使用存在主义的以下数据模型:
data Node a = Node a (Map TypeRep AnyNode)
data AnyNode = forall a. Show a => AnyNode a
The rules about memory footprint of standard types have been explained previously . 先前已经解释了关于标准类型的存储器占用的规则。 Now, what are the rules for existential types, like AnyNode
? 现在,存在类型的规则是什么,比如AnyNode
?
Are there any optimization techniques, eg some workarounds using unsafeCoerce
making it possible to elude the existential declaration? 是否有任何优化技术,例如使用unsafeCoerce
一些变通方法unsafeCoerce
存在性声明? I'm asking this because a type similar to Node
is going to be placed in a cost centre of a highly memory-intensive lib, so memory footprint is all, that's why the most dirty hacks are welcome. 我问这个是因为类似于Node
的类型将被放置在高内存密集型lib的成本中心,所以内存占用就足够了,这就是为什么最脏的黑客是受欢迎的。
The ghc-datasize package may be of help here: ghc-datasize包可能在这里有所帮助:
{-# LANGUAGE RankNTypes, GADTs #-}
import GHC.DataSize
data Node = forall a. Show a => Node a
main = do
s <- closureSize $ Node 0
print s -- 24 bytes on my 64-bit system
So, it seems that Node
takes one extra word compared to the plain unary data constructor, presumably because of the Show
class dictionary pointer. 因此,与普通的一元数据构造函数相比, Node
似乎需要一个额外的单词,大概是因为Show
类字典指针。 Also, I tried adding more class constraints to Node
, and each of them takes one extra word of space. 此外,我尝试向Node
添加更多类约束,并且每个约束都需要一个额外的空格。
I don't know for sure whether it is possible to magic away the dictionary pointer in specific circumstances. 我不确定是否有可能在特定情况下魔法消除字典指针。 I think it isn't possible if you'd like to keep the existential type. 我想如果你想保留存在主义类型是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.