kotlinx-io icon indicating copy to clipboard operation
kotlinx-io copied to clipboard

Slow kotlinx.io.core.toByteArray

Open Alex009 opened this issue 6 years ago • 0 comments

i use kotlinx-io not directly - just by ktor. My project is mobile app with shared library for ios & android, when i send very large textcontent by ktor (4mb photo from ios device encoded by base64) app stuck on call kotlinx.io.core.toByteArray and worked with 100% cpu usage by 30mins, and after it request is done and all complete. For fast fix i create my own TextContent, which use not kotlinx.io.core.toByteArray, but kotlinx.serialization.toUtf8Bytes on iOS platform. Here code:

class LargeTextContent(
    val text: String,
    override val contentType: ContentType,
    override val status: HttpStatusCode? = null
) : OutgoingContent.ByteArrayContent() {
    private val bytes by lazy(LazyThreadSafetyMode.NONE) {
        text.toByteArrayPlatform(contentType.charset() ?: Charsets.UTF_8)
    }

    override val contentLength: Long
        get() = bytes.size.toLong()

    override fun bytes(): ByteArray = bytes

    override fun toString(): String = "LargeTextContent[$contentType] \"${text.take(30)}\""
}

expect fun String.toByteArrayPlatform(charset: Charset): ByteArray

android (original version of toByteArray):

import kotlinx.io.charsets.Charset
import kotlinx.io.core.toByteArray

actual fun String.toByteArrayPlatform(charset: Charset): ByteArray {
    return toByteArray(charset)
}

ios (fixed for my case):

import kotlinx.io.charsets.Charset
import kotlinx.serialization.toUtf8Bytes

actual fun String.toByteArrayPlatform(charset: Charset): ByteArray {
    return toUtf8Bytes()
}

when i debugging i saw this stacktrace: image (1)

here used versions: kotlin_version=1.3.30 coroutines_version=1.2.0 ktor_version=1.1.4 klock_version=1.4.0 serialization_version=0.11.0

and i get this bug at this versions too: kotlin_version=1.3.21 coroutines_version=1.1.1 ktor_version=1.1.3 klock_version=1.2.2 serialization_version=0.10.0

Alex009 avatar Apr 23 '19 16:04 Alex009