可存储和未装箱矢量之间的差异 [英] Differences between Storable and Unboxed Vectors

查看:77
本文介绍了可存储和未装箱矢量之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以...我最好现在就使用未装箱的矢量(来自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).

这使我想到了StorableUnboxed向量之间的差异.我自己弄清楚的事情:

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(即VectorMVector)实例更容易手工编写.
  • 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 than Unbox (that is Vector and MVector) 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 in malloced 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.

StorablePrim类型类都提供了一种将值存储为 个字节,并将字节加载到一个值中.区别是什么类型的 使用了字节数组.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆