let bytes = KotlinByteArray(size: hundredMB) { _ in 5 }
For some reason, this takes about 1:20 minutes to complete and about 7 GB of RAM (iPhone Simulator).
This made me curious. Does anyone know why?
I already found the note that the init function is being called for each and every entry of the Array.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/-init-.html
But why so much memory usage? Also, NSData achieves the same in mere seconds.
p
Paul Woitaschek
11/10/2021, 6:33 PM
What if you omit the optional initializer?
b
Bastian
11/11/2021, 12:07 PM
Then it also works "instantly", only taking up around 100 MiB more than the normal app's footprint.
The
KotlinByteArray
gets initialised with all 0s.
Also, the memory seems to be freed completely when ditching the reference (which isn't the case when turning
NSData
into a
ByteArray
in my experience).
Bastian
11/11/2021, 12:08 PM
I was actually curious about the actual reasons behind the performance of initialising
KotlinByteArray
with a custom init param from within Swift. But I guess, I will just try to be very careful about this or just try to generate sample data in different ways if I don't want to stick with