Skip to content
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

KTOR-4809 Add Flag to disable CharArray pooling #3153

Merged
merged 1 commit into from Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -6,13 +6,23 @@ package io.ktor.http.cio.internals

import io.ktor.utils.io.pool.*

internal expect val DISABLE_CHAR_ARRAY_POOLING: Boolean

internal const val CHAR_ARRAY_POOL_SIZE = 4096

/**
* Number of characters that an array from the pool can store
*/
internal const val CHAR_BUFFER_ARRAY_LENGTH: Int = 4096 / 2

internal val CharArrayPool: ObjectPool<CharArray> = object : DefaultPool<CharArray>(CHAR_ARRAY_POOL_SIZE) {
override fun produceInstance(): CharArray = CharArray(CHAR_BUFFER_ARRAY_LENGTH)
internal val CharArrayPool: ObjectPool<CharArray> = if (DISABLE_CHAR_ARRAY_POOLING) {
object : NoPoolImpl<CharArray>() {
override fun borrow(): CharArray {
return CharArray(CHAR_BUFFER_ARRAY_LENGTH)
}
}
} else {
object : DefaultPool<CharArray>(CHAR_ARRAY_POOL_SIZE) {
override fun produceInstance(): CharArray = CharArray(CHAR_BUFFER_ARRAY_LENGTH)
}
}
@@ -0,0 +1,7 @@
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.http.cio.internals

internal actual val DISABLE_CHAR_ARRAY_POOLING: Boolean = false
@@ -0,0 +1,8 @@
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.http.cio.internals

internal actual val DISABLE_CHAR_ARRAY_POOLING: Boolean =
System.getProperty("ktor.internal.cio.disable.chararray.pooling")?.toBoolean() ?: false
@@ -0,0 +1,7 @@
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.http.cio.internals

internal actual val DISABLE_CHAR_ARRAY_POOLING: Boolean = false
2 changes: 1 addition & 1 deletion ktor-io/jvm/src/io/ktor/utils/io/pool/DefaultPool.kt
Expand Up @@ -25,7 +25,7 @@ actual constructor(actual final override val capacity: Int) : ObjectPool<T> {
// factory
protected actual abstract fun produceInstance(): T

// optional cleaning of poped items
// optional cleaning of popped items
protected actual open fun clearInstance(instance: T): T = instance

// optional validation for recycled items
Expand Down