json-iterator / java

jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
http://jsoniter.com/
MIT License
1.51k stars 518 forks source link

Kotlin encoding is 4x faster with @JsonProperty(required = true) in DYNAMIC_MODE #265

Open yaoligang opened 4 years ago

yaoligang commented 4 years ago

Recently we did some benchmarks in Kotlin and Java. According to our benchmarks, most encoding and decoding tasks' performance are similar in Kotlin and Java. But there is a special case when using _EncodingMode.DYNAMICMODE and @JsonProperty(required = true) together.

Environment:

Test classes:

class KotlinRequest {
    var sessionId: String? = null
    var token: String? = null
    var operatorId: Int? = null
    var gameId: Int? = null
    var username: String? = null
    var isPromotion: Boolean? = null
    var transactionKey: String? = null
    var debit: Long = 0
    var credit: Long = 0
}

class KotlinRequestWithAnnotation {
    @JsonProperty(required = true)
    var sessionId: String? = null

    @JsonProperty(required = true)
    var token: String? = null

    @JsonProperty(required = true)
    var operatorId: Int? = null

    @JsonProperty(required = true)
    var gameId: Int? = null

    @JsonProperty(required = true)
    var username: String? = null

    @JsonProperty(required = true)
    var isPromotion: Boolean? = null

    @JsonProperty(required = true)
    var transactionKey: String? = null

    @JsonProperty(required = true)
    var debit: Long = 0

    @JsonProperty(required = true)
    var credit: Long = 0
}

Test Data:

@Benchmark
fun encodeKotlinClass(): String {
    val data = KotlinRequest()

    data.sessionId = "a285a7dcd761"
    data.token = "bad4cae6ff11a57829087fc260bfe4bd"
    data.operatorId = 10000
    data.gameId = 3
    data.username = "yourname"
    data.isPromotion = false
    data.transactionKey = "1321569482648137527"
    data.debit = 500
    data.credit = 0

    return JsonStream.serialize(data)
}

@Benchmark
fun encodeKotlinClassWithAnnotation(): String {
    val data = KotlinRequestWithAnnotation()

    data.sessionId = "a285a7dcd761"
    data.token = "bad4cae6ff11a57829087fc260bfe4bd"
    data.operatorId = 10000
    data.gameId = 3
    data.username = "yaoligang"
    data.isPromotion = false
    data.transactionKey = "1321569482648137527"
    data.debit = 500
    data.credit = 0

    return JsonStream.serialize(data)
}

Results

You can see after using @JsonProperty(required = true) annotation, the encoding speed is about 4x faster in dynamic mode. It is over 9x faster than the default encoding in reflection mode.

I think maybe you will be interested about why Kotlin can speed up the encoding quite a lot in this case.