Skip to content

Commit

Permalink
chore: use a ArrayDeque when parsing arrays in JsonParser
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 15, 2020
1 parent c0a459c commit 6207985
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,63 +36,78 @@ class JsonPerformanceSpec {

@Test
void 'test Pact JSON parser'() {
Map<String, Long> result = [:]

Map<String, BigInteger> result = [:]
jsonFiles.each { entry ->
long start = System.nanoTime()
JsonParser.INSTANCE.parseString(entry.value)
long time = System.nanoTime() - start
result[entry.key] = time
result[entry.key] = BigInteger.ZERO
}

100.times {
jsonFiles.each { entry ->
long start = System.nanoTime()
JsonParser.INSTANCE.parseString(entry.value)
long time = System.nanoTime() - start
result[entry.key] += time.toBigInteger()
}
}

println 'RESULT:'
long total = 0
BigInteger total = 0
result.keySet().toSorted().each { key ->
println("${key.padRight(40)}: ${result[key]}")
total += result[key]
println("${key.padRight(40)}: ${result[key] / 100}")
total += result[key].div(100).toBigInteger()
}
println("${'TOTAL'.padRight(40)}: ${total}")
println()
}

@Test
void 'test GSON parser'() {
Map<String, Long> result = [:]

Map<String, BigInteger> result = [:]
jsonFiles.each { entry ->
long start = System.nanoTime()
com.google.gson.JsonParser.parseString(entry.value)
long time = System.nanoTime() - start
result[entry.key] = time
result[entry.key] = BigInteger.ZERO
}

100.times {
jsonFiles.each { entry ->
long start = System.nanoTime()
com.google.gson.JsonParser.parseString(entry.value)
long time = System.nanoTime() - start
result[entry.key] += time.toBigInteger()
}
}

println 'RESULT:'
long total = 0
BigInteger total = 0
result.keySet().toSorted().each { key ->
println("${key.padRight(40)}: ${result[key]}")
total += result[key]
println("${key.padRight(40)}: ${result[key] / 100}")
total += result[key].div(100).toBigInteger()
}
println("${'TOTAL'.padRight(40)}: ${total}")
println()
}

@Test
void 'test Groovy Json slurper parser'() {
Map<String, Long> result = [:]
JsonSlurper slurper = new JsonSlurper()

Map<String, BigInteger> result = [:]
jsonFiles.each { entry ->
long start = System.nanoTime()
slurper.parseText(entry.value)
long time = System.nanoTime() - start
result[entry.key] = time
result[entry.key] = BigInteger.ZERO
}

100.times {
jsonFiles.each { entry ->
long start = System.nanoTime()
slurper.parseText(entry.value)
long time = System.nanoTime() - start
result[entry.key] += time.toBigInteger()
}
}

println 'RESULT:'
long total = 0
BigInteger total = 0
result.keySet().toSorted().each { key ->
println("${key.padRight(40)}: ${result[key]}")
total += result[key]
println("${key.padRight(40)}: ${result[key] / 100}")
total += result[key].div(100).toBigInteger()
}
println("${'TOTAL'.padRight(40)}: ${total}")
println()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.io.BufferedInputStream
import java.io.InputStream
import java.io.InputStreamReader
import java.io.Reader
import java.util.ArrayDeque

class JsonException(message: String) : RuntimeException(message)

Expand Down Expand Up @@ -449,7 +450,7 @@ object JsonParser {
}

private fun parseArray(lexer: JsonLexer): JsonValue.Array {
val array = mutableListOf<JsonValue>()
val array = ArrayDeque<JsonValue>()
var token: JsonToken?

do {
Expand Down Expand Up @@ -483,7 +484,7 @@ object JsonParser {
"Invalid Json document (${lexer.documentPointer()}) - found end of document while parsing array")
}

return JsonValue.Array(array)
return JsonValue.Array(array.toMutableList())
}

private fun nextTokenOrThrow(lexer: JsonLexer): JsonToken? {
Expand Down

0 comments on commit 6207985

Please sign in to comment.