scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

Vector[Byte] is backing by Array[AnyRef] #12931

Closed He-Pin closed 5 months ago

He-Pin commented 5 months ago

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 of system.arraycopy directly it cause an array store exception, because the Vector's underlying is Object[] and I can copy a byte[] 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

som-snytt commented 5 months ago

@scala/collections as Seth would say.

Ichoran commented 5 months ago

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 ints; (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.

jrudolph commented 5 months ago

Previous discussion: https://github.com/scala/bug/issues/4495