Skip to content

Slow kotlinx.io.core.toByteArray #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Alex009 opened this issue Apr 23, 2019 · 1 comment
Closed

Slow kotlinx.io.core.toByteArray #47

Alex009 opened this issue Apr 23, 2019 · 1 comment

Comments

@Alex009
Copy link

Alex009 commented Apr 23, 2019

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

@fzhinkin
Copy link
Collaborator

We're rebooting the kotlinx-io development (see #131), all issues related to the previous versions will be closed. Consider reopening it if the issue remains (or the feature is still missing) in a new version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants