可存储和未装箱矢量之间的差异 [英] Differences between Storable and Unboxed Vectors
问题描述
所以...我最好现在就使用未装箱的矢量(来自vector
包),而无需多加考虑. vector-th-unbox
使为它们创建实例变得轻而易举,所以为什么不这样做.
So ... I've used unboxed vectors (from the vector
package) preferably now without giving it much consideration. vector-th-unbox
makes creating instances for them a breeze, so why not.
现在,我遇到了一个实例,在该实例中,我无法自动派生这些实例,即带有幻像类型参数的数据类型(如Vector (s :: Nat) a
,其中s
编码长度).
Now I ran into an instance where it is not possible for me to automatically derive those instances, a data type with phantom type parameters (as in Vector (s :: Nat) a
, where s
encodes the length).
这使我想到了Storable
和Unboxed
向量之间的差异.我自己弄清楚的事情:
This made me think about the differences between Storable
and Unboxed
vectors. Things I figured out on my own:
-
Unboxed
通过将元组存储为单独的向量,从而在不需要这些值之一的情况下避免浪费带宽,从而获得更好的缓存位置. -
Storable
仍将编译为简单的(可能是有效的)readArray#
,它们返回未装箱的值(通过阅读核心可以明显看出). -
Storable
允许直接指针访问,从而可以与外来代码进行互操作.Unboxed
不会. - [edit]
Storable
实例实际上比Unbox
(即Vector
和MVector
)实例更容易手工编写.
Unboxed
will store eg tuples as separate vectors leading to better cache locality, by not wasting bandwidth when only one of those values is needed.Storable
will still be compiled to simple (and probably efficient)readArray#
s that return unboxed values (as evident by reading core).Storable
allows direct pointer access which allows interoperability with foreign code.Unboxed
doesn't.- [edit]
Storable
instances are actually easier to write by hand thanUnbox
(that isVector
andMVector
) ones.
仅凭这一点并不能使我明白为什么Unboxed
甚至存在,似乎没有什么好处.可能我在那里缺少什么?
That alone doesn't make it evident to me why Unboxed
even exists, there seem to be little benefit to it. Probably I am missing something there?
推荐答案
从 https://haskell- lang.org/library/vector
可存储和未装箱的向量都将其数据存储在字节数组中,避免了 指针间接.这样可以提高内存效率,并更好地利用 缓存.可存储向量和非盒装向量之间的区别很细微:
Storable and unboxed vectors both store their data in a byte array, avoiding pointer indirection. This is more memory efficient and allows better usage of caches. The distinction between storable and unboxed vectors is subtle:
- 可存储向量需要的数据是
Storable
类型 类. 此数据存储在malloc
固定在 pin 中的内存中(垃圾 收集器无法移动它).这可能导致内存碎片,但是 允许通过C FFI共享数据. - 未装箱的矢量需要的数据是
- Storable vectors require data which is an instance of the
Storable
type class. This data is stored inmalloc
ed memory, which is pinned (the garbage collector can't move it around). This can lead to memory fragmentation, but allows the data to be shared over the C FFI. - Unboxed vectors require data which is an instance of the
Prim
type class. This data is stored in GC-managed unpinned memory, which helps avoid memory fragmentation. However, this data cannot be shared over the C FFI.
Storable
和Prim
类型类都提供了一种将值存储为
个字节,并将字节加载到一个值中.区别是什么类型的
使用了字节数组.
Both the Storable
and Prim
typeclasses provide a way to store a value as
bytes, and to load bytes into a value. The distinction is what type of
bytearray is used.
像往常一样,唯一真正的性能衡量标准是基准测试.然而, 作为一般准则:
As usual, the only true measure of performance will be benchmarking. However, as a general guideline:
- 如果您不需要将值传递到C FFI,并且您有
Prim
实例, 使用未装箱的矢量. - 如果您有
Storable
实例,请使用可存储的向量. - 否则,请使用盒装矢量.
- If you don't need to pass values to a C FFI, and you have a
Prim
instance, use unboxed vectors. - If you have a
Storable
instance, use a storable vector. - Otherwise, use a boxed vector.
还有其他要考虑的问题,例如盒装矢量的事实
是Functor
的实例,而可存储和未装箱的矢量则不是.
There are also other issues to consider, such as the fact that boxed vectors
are instances of Functor
while storable and unboxed vectors are not.
这篇关于可存储和未装箱矢量之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!