Scala:什么是CompactBuffer?

4

我正在尝试弄清CompactBuffer的含义。它是否与迭代器相同?

请解释它们之间的区别。


3
请阅读该类的javadoc注释。 https://github.com/apache/spark/blob/master/core/src/main/scala/org/apache/spark/util/collection/CompactBuffer.scala - OneCricketeer
1个回答

6
根据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.
*/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接