Fast Array Copying - Issues with Multiplatform and Unsigned Types

There are two issues but I combined them because there may be a single solution.

  1. Given two arrays, how do I do a fast copy from one to the other in common code of a multiplatform project? On the JVM System.arraycopy exists, but I believe this is a candidate for including in all platforms. I suggest copyTo unless this already exists and I’m missing it? Surely we’re not expecting a loop for array copying on native and JS platforms, both of which have faster approaches.

  2. Regardless of platform, the paradigm of unsigned type arrays in 1.3 is causing me problems when I want to copy. I am unable to access the underlying storage. The proposal mentions something like UIntArray.toIntArray should exist, but I can’t find it. How should I, even on the JVM, do a fast copy between two existing UIntArrays? Or UByteArrays? These are fairly common use cases.

  1. I’m also running into issues creating large unsigned arrays. How do I, say, create a zeroed UByteArray of size 10000?

Mutliplatform is still in development so I’m not sure whether this exists already. I’d take a look here and see if there is an issue already and create one if not.

1.3 is still a preview build. It does not yet contain all features, so if the proposal says this function will be there I guess you just have to wait. Or you could check if it has an assignee and implement it yourself.

I guess the same way you do with any kotlin array UByteArray(100000) { 0 }.

Done: https://youtrack.jetbrains.com/issue/KT-25874

No, that is not how I’d do it with any Kotlin array. I’d just do ByteArray(100000) (or arrayOfNulls for non-primitives), it makes no sense to provide the trailing lambda and have 100000 iterations. It is a very unoptimal approach to array creation. I opened an issue to address this and generic issues with how the unsigned arrays hide their raw arrays: https://youtrack.jetbrains.com/issue/KT-25875

There will be a secondary constructor to create unsigned arrays from size parameter. It was omitted in 1.3-M1 because of a temporary limitation of inline classes.

1 Like

https://youtrack.jetbrains.net/issue/KT-17392