Closed He-Pin closed 5 months ago
@scala/collections as Seth would say.
All of the Scala collections are backed by elements as objects; Vector
is not an exception. Array
is also not an exception, because it isn't part of the Scala collections (but is retrofitted to look like it sorta is).
The actual exceptions are (1) BitSet
is backed by bits packed into Longs; (2) IntMap
has a key that is a primitive int
; (3) LongMap
has a key that is a primitive long
; (4) Range
doesn't even store elements, and the start, end, and skip values are all in primitive int
s; (5) NumericRange
also doesn't store elements (but the start, end, and skip are objects); (6) WrappedString
is exactly what it sounds like (characters aren't boxed); (7) WrappedArray
is exactly what it sounds like (underlying elements aren't boxed); (8) ArraySeq
I believe can go either way.
Anyway, it is a non-goal for the Scala collections, thus far, to be efficient with primitives. It is basically up to you to use Array
in clever ways, outside of the explicit exceptions above. Note that IntMap
and LongMap
are deprecated in practice, if not actually in the library. The docs encourage you to use HashMap
instead.
One could envision a high-performance primitive-embracing library, but the standard collections library isn't it.
Previous discussion: https://github.com/scala/bug/issues/4495
I think this maybe by design, but I was expecting it backing by
Array[Byte]
. eg: https://github.com/apache/incubator-pekko/pull/1001 when I try to make use ofsystem.arraycopy
directly it cause an array store exception, because the Vector's underlying isObject[]
and I can copy abyte[]
to it.And that will make
Vector[Byte]
not an option.refs: https://github.com/scodec/scodec-bits/blob/main/core/shared/src/main/scala/scodec/bits/ByteVector.scala