-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance of JSON encoding (#1354)
* Instead of processing strings char-by-char with a lot of inner branching (compact strings, range checks etc.), read the whole string into a pre-allocated char array and process it instead. Also, optimistically use this very char array as a result and process escapes by shifting chars in this array * Pool char arrays to reduce allocation pressure * Benchmarks for JSON encoding and comparison with Jackson
- Loading branch information
Showing
14 changed files
with
410 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
formats/json/commonMain/src/kotlinx/serialization/json/internal/Composers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package kotlinx.serialization.json.internal | ||
|
||
import kotlinx.serialization.json.* | ||
import kotlin.jvm.* | ||
|
||
internal open class Composer(@JvmField internal val sb: JsonStringBuilder, @JvmField internal val json: Json) { | ||
private var level = 0 | ||
var writingFirst = true | ||
private set | ||
|
||
fun indent() { | ||
writingFirst = true | ||
level++ | ||
} | ||
|
||
fun unIndent() { | ||
level-- | ||
} | ||
|
||
fun nextItem() { | ||
writingFirst = false | ||
if (json.configuration.prettyPrint) { | ||
print("\n") | ||
repeat(level) { print(json.configuration.prettyPrintIndent) } | ||
} | ||
} | ||
|
||
fun space() { | ||
if (json.configuration.prettyPrint) | ||
print(' ') | ||
} | ||
|
||
fun print(v: Char) = sb.append(v) | ||
fun print(v: String) = sb.append(v) | ||
open fun print(v: Float) = sb.append(v.toString()) | ||
open fun print(v: Double) = sb.append(v.toString()) | ||
open fun print(v: Byte) = sb.append(v.toLong()) | ||
open fun print(v: Short) = sb.append(v.toLong()) | ||
open fun print(v: Int) = sb.append(v.toLong()) | ||
open fun print(v: Long) = sb.append(v) | ||
open fun print(v: Boolean) = sb.append(v.toString()) | ||
fun printQuoted(value: String): Unit = sb.appendQuoted(value) | ||
} | ||
|
||
@ExperimentalUnsignedTypes | ||
internal class ComposerForUnsignedNumbers(sb: JsonStringBuilder, json: Json) : Composer(sb, json) { | ||
override fun print(v: Int) { | ||
return super.print(v.toUInt().toString()) | ||
} | ||
|
||
override fun print(v: Long) { | ||
return super.print(v.toULong().toString()) | ||
} | ||
|
||
override fun print(v: Byte) { | ||
return super.print(v.toUByte().toString()) | ||
} | ||
|
||
override fun print(v: Short) { | ||
return super.print(v.toUShort().toString()) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonStringBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package kotlinx.serialization.json.internal | ||
|
||
internal expect class JsonStringBuilder constructor() { | ||
fun append(value: Long) | ||
fun append(ch: Char) | ||
fun append(string: String) | ||
fun appendQuoted(string: String) | ||
override fun toString(): String | ||
fun release() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.