Skip to content

Commit

Permalink
KTOR-4809 Add Flag to disable CharArray pooling (#3153)
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Sep 6, 2022
1 parent 0beebb9 commit 60b9269
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
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

0 comments on commit 60b9269

Please sign in to comment.