Scala:什么是CompactBuffer?

问题描述 投票:1回答:1

我试图找出CompactBuffer的含义。它和迭代器一样吗?

请解释一下差异。

scala apache-spark
1个回答
4
投票

根据Spark的文档,它是ArrayBuffer的替代品,可以提供更好的性能,因为它分配的内存更少。

以下是CompactBuffer类文档的摘录:

/**
* An append-only buffer similar to ArrayBuffer, but more memory-efficient for small buffers.
* ArrayBuffer always allocates an Object array to store the data, with 16 entries by default,
* so it has about 80-100 bytes of overhead. In contrast, CompactBuffer can keep up to two
* elements in fields of the main object, and only allocates an Array[AnyRef] if there are more
* entries than that. This makes it more efficient for operations like groupBy where we expect
* some keys to have very few elements.
*/
© www.soinside.com 2019 - 2024. All rights reserved.