Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.alibaba.fastjson2.benchmark;

import com.alibaba.fastjson2.JSON;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
* Benchmark for long field name caching in fastjson2.
* Compares parsing performance with standard OTLP metrics JSON containing long property names
* (e.g., startTimeUnixNano, aggregationTemporality, exponentialHistogram) versus a modified
* version where those names are shortened to 16 characters or fewer.
*
* Data source: OpenTelemetry metrics example (https://github.com/open-telemetry/opentelemetry-proto/blob/b363281e5c30f343bfd2405519cc3db93d11f252/examples/metrics.json)
* Licensed under Apache License 2.0.
*/
@State(Scope.Benchmark)
public class LongFieldNameCache {
@Param({"OTLP", "SHORT"})
public String payload;

private byte[] utf8Bytes;

@Setup
public void setup() throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
try (InputStream input = LongFieldNameCache.class.getClassLoader().getResourceAsStream("data/otlp-metrics.json")) {
if (input == null) {
throw new IOException("Missing data/otlp-metrics.json");
}
byte[] buffer = new byte[1024];
int count;
while ((count = input.read(buffer)) != -1) {
output.write(buffer, 0, count);
}
}
String json = new String(output.toByteArray(), StandardCharsets.UTF_8);

if ("SHORT".equals(payload)) {
// Replace long property names with shorter equivalents, preserving JSON structure.
// We only replace complete keys including quotes and colon to avoid affecting values.
json = json.replace("\"startTimeUnixNano\":", "\"startTime\":");
json = json.replace("\"aggregationTemporality\":", "\"temporality\":");
json = json.replace("\"exponentialHistogram\":", "\"expHistogram\":");
}
utf8Bytes = json.getBytes(StandardCharsets.UTF_8);
}

@Benchmark
public Object parse() {
return JSON.parseObject(utf8Bytes);
}
}
6 changes: 6 additions & 0 deletions benchmark/src/main/resources/data/otlp-metrics.LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Source: OpenTelemetry opentelemetry-proto, examples/metrics.json
https://github.com/open-telemetry/opentelemetry-proto/blob/b363281e5c30f343bfd2405519cc3db93d11f252/examples/metrics.json
Git blob: 33e72c4b53aa9fdad6549de5e6e10251aa0a5236

Licensed under the Apache License, Version 2.0. The license text is provided
in the repository LICENSE file.
140 changes: 140 additions & 0 deletions benchmark/src/main/resources/data/otlp-metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"resourceMetrics": [
{
"resource": {
"attributes": [
{
"key": "service.name",
"value": {
"stringValue": "my.service"
}
}
]
},
"scopeMetrics": [
{
"scope": {
"name": "my.library",
"version": "1.0.0",
"attributes": [
{
"key": "my.scope.attribute",
"value": {
"stringValue": "some scope attribute"
}
}
]
},
"metrics": [
{
"name": "my.counter",
"unit": "1",
"description": "I am a Counter",
"sum": {
"aggregationTemporality": 1,
"isMonotonic": true,
"dataPoints": [
{
"asDouble": 5,
"startTimeUnixNano": "1544712660300000000",
"timeUnixNano": "1544712660300000000",
"attributes": [
{
"key": "my.counter.attr",
"value": {
"stringValue": "some value"
}
}
]
}
]
}
},
{
"name": "my.gauge",
"unit": "1",
"description": "I am a Gauge",
"gauge": {
"dataPoints": [
{
"asDouble": 10,
"timeUnixNano": "1544712660300000000",
"attributes": [
{
"key": "my.gauge.attr",
"value": {
"stringValue": "some value"
}
}
]
}
]
}
},
{
"name": "my.histogram",
"unit": "1",
"description": "I am a Histogram",
"histogram": {
"aggregationTemporality": 1,
"dataPoints": [
{
"startTimeUnixNano": "1544712660300000000",
"timeUnixNano": "1544712660300000000",
"count": "2",
"sum": 2,
"bucketCounts": ["1","1"],
"explicitBounds": [1],
"min": 0,
"max": 2,
"attributes": [
{
"key": "my.histogram.attr",
"value": {
"stringValue": "some value"
}
}
]
}
]
}
},
{
"name": "my.exponential.histogram",
"unit": "1",
"description": "I am an Exponential Histogram",
"exponentialHistogram": {
"aggregationTemporality": 1,
"dataPoints": [
{
"startTimeUnixNano": "1544712660300000000",
"timeUnixNano": "1544712660300000000",
"count": "3",
"sum": 10,
"scale": 0,
"zeroCount": "1",
"positive": {
"offset": 1,
"bucketCounts": ["0","2"]
},
"min": 0,
"max": 5,
"zeroThreshold": 0,
"attributes": [
{
"key": "my.exponential.histogram.attr",
"value": {
"stringValue": "some value"
}
}
]
}
]
}
}
]
}
]
}
]
}
2 changes: 2 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public static String getProperty(String key) {

static final NameCacheEntry[] NAME_CACHE = new NameCacheEntry[8192];
static final NameCacheEntry2[] NAME_CACHE2 = new NameCacheEntry2[8192];
// Longer names do not displace the existing short-name entries.
static final String[] NAME_CACHE3 = new String[8192];

static int defaultDecimalMaxScale = 2048;
static int defaultMaxLevel;
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderASCII.java
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,9 @@ public final String readFieldName() {
+ (bytes[nameBegin + 8] & 0xFFL);
break;
default:
if (length >= 17 && length <= 32 && IOUtils.isASCII(bytes, nameBegin, length)) {
return getFieldNameUTF8Cache3(nameBegin, length);
}
break;
}

Expand Down
46 changes: 46 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -3574,6 +3574,9 @@ public String readFieldName() {
+ ((long) bytes[nameBegin + 8]);
break;
default:
if (length >= 17 && length <= 32) {
return getFieldNameUTF8Cache3(nameBegin, length);
}
break;
}

Expand Down Expand Up @@ -3641,6 +3644,49 @@ public String readFieldName() {
return getFieldName();
}

final String getFieldNameUTF8Cache3(int nameBegin, int length) {
final byte[] bytes = this.bytes;
// Boundary bytes keep hashing cheap; the full name comparison below disambiguates the middle.
int hash = length;
for (int i = 0; i < 8; i++) {
hash = hash * 31 + (bytes[nameBegin + i] & 0xff);
}
for (int i = length - 8; i < length; i++) {
hash = hash * 31 + (bytes[nameBegin + i] & 0xff);
}
hash ^= (hash >>> 16);
hash *= 0x85ebca6b;
hash ^= (hash >>> 13);
int index = hash & (NAME_CACHE3.length - 1);
String cached = NAME_CACHE3[index];
if (cached != null && cached.length() == length) {
boolean match = true;
for (int i = 0; i < length; i++) {
if (cached.charAt(i) != (char) (bytes[nameBegin + i] & 0xff)) {
match = false;
break;
}
}
if (match) {
return cached;
}
}
String name;
if (STRING_CREATOR_JDK8 != null) {
name = asciiStringJDK8(bytes, nameBegin, length);
} else if (ANDROID) {
name = getLatin1String(nameBegin, length);
} else if (STRING_CREATOR_JDK11 != null) {
name = STRING_CREATOR_JDK11.apply(
Arrays.copyOfRange(bytes, nameBegin, nameBegin + length),
LATIN1);
} else {
name = new String(bytes, nameBegin, length, ISO_8859_1);
}
NAME_CACHE3[index] = name;
return name;
}

@Override
public final int readInt32Value() {
int ch = this.ch;
Expand Down
Loading
Loading