Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion core/trino-main/src/main/java/io/trino/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -105,6 +106,9 @@

public final class JsonUtil
{
// StringReader outperforms InputStreamReader for small inputs. Limit based on Jackson benchmarks {@link https://github.com/FasterXML/jackson-benchmarks/pull/9}
private static final int STRING_READER_LENGTH_LIMIT = 8192;

private JsonUtil() {}

// This object mapper is constructed without .configure(ORDER_MAP_ENTRIES_BY_KEYS, true) because
Expand All @@ -124,7 +128,12 @@ public static JsonParser createJsonParser(JsonFactory factory, Slice json)
throws IOException
{
// Jackson tries to detect the character encoding automatically when using InputStream
// so we pass an InputStreamReader instead.
// so we pass StringReader or an InputStreamReader instead.
if (json.length() < STRING_READER_LENGTH_LIMIT) {
// StringReader is more performant than InputStreamReader for small inputs
return factory.createParser(new StringReader(new String(json.getBytes(), UTF_8)));
}

return factory.createParser(new InputStreamReader(json.getInput(), UTF_8));
}

Expand Down