How fastjson2 Boosts JSON Speed: Lambda Mapping, Zero‑Copy, and Type Parsing

This article explains how fastjson2 achieves significant performance gains by replacing reflection with Java 8 Lambda‑generated function mappings, applying zero‑copy String construction, and implementing hand‑crafted parsers for common types such as dates, backed by benchmark data.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
How fastjson2 Boosts JSON Speed: Lambda Mapping, Zero‑Copy, and Type Parsing

Overview

fastjson is a widely used JSON library in Java. fastjson2 (v2.0) introduces several core techniques to improve speed: replacing frequent reflection with Lambda‑generated function mappings, zero‑copy String handling, and optimized parsing for common types.

1. Lambda‑generated function mapping

Traditional reflection uses Method.invoke which ultimately calls a native method, incurring checks and JNI overhead. fastjson2 generates a java.util.function.ToIntFunction via LambdaMetafactory to map methods like Bean::getId to a function, eliminating reflection at runtime.

public class Bean {
    int id;
    public int getId() { return id; }
}
Method methodGetId = Bean.class.getMethod("getId");
Bean bean = createInstance();
int value = (Integer) methodGetId.invoke(bean);

Rewritten with Lambda:

java.util.function.ToIntFunction<Bean> function = Bean::getId;
int i = function.applyAsInt(bean);

Benchmark shows Method.invoke takes ~25 ms for 10 000 calls, while the generated Lambda function takes ~1 ms, a 25× speedup. fastjson2 caches the generated functions, so the creation cost is paid once during class loading.

2. Zero‑copy String handling

Zero‑copy avoids copying data between buffers during I/O. In JDK 8, a non‑public String constructor can create a String directly from a char array without copying. fastjson2 creates a Lambda mapping to this constructor and caches it, enabling fast construction of formatted dates.

static BiFunction<char[], Boolean, String> STRING_CREATOR_JDK8;
static {
    CallSite cs = LambdaMetafactory.metafactory(
        caller, "apply", methodType(BiFunction.class),
        methodType(Object.class, Object.class, Object.class),
        handle, methodType(String.class, char[].class, boolean.class));
    STRING_CREATOR_JDK8 = (BiFunction<char[], Boolean, String>) cs.getTarget().invokeExact();
}
static String formatYYYYMMDD(LocalDate date) {
    // build char[] for "yyyy-MM-dd"
    char[] chars = new char[10];
    // ... fill chars ...
    return STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
}

3. Common type parsing optimizations

fastjson2 provides hand‑crafted parsers for frequent types such as Date. For the fixed pattern "yyyy‑MM‑dd HH:mm:ss" it parses the string by extracting characters and computing the epoch milliseconds directly, avoiding generic parsing overhead.

public static Date parseYYYYMMDDHHMMSS19(String str) {
    char y0 = str.charAt(0);
    // ... extract other characters ...
    int year = (y0-'0')*1000 + (y1-'0')*100 + (y2-'0')*10 + (y3-'0');
    // compute epoch day and milliseconds...
    return new Date(millis);
}

JMH benchmarks show this specialized parser runs in ~0.425 µs/op, far faster than SimpleDateFormat (~11.5 µs/op) or DateTimeFormatter (~7.6 µs/op).

Conclusion

fastjson2’s speed advantage comes from replacing reflection with cached Lambda functions, employing zero‑copy String creation, and writing dedicated parsers for hot paths. These techniques can be applied to other Java back‑end services to improve throughput and latency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceLambdaJSONZero CopyBenchmarkFastjson2
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.