这又是一个性能优化Bug修复的版本更新版本,大家按需升级。
这个版本的性能优化包括:
序列化时,写字符串检测是否存在特别字符是一个性能关键点,这个版本使用SWAR(SIMD Within A Register)的技巧来做快速检测。如下
package com.alibaba.fastjson2; class JSONWriterUTF8 { protected final long byteVectorQuote; JSONWriterUTF8(Context ctx) { // " -> 0x22, ' -> 0x27 this.byteVectorQuote = this.useSingleQuote ? 0x2727_2727_2727_2727L : 0x2222_2222_2222_2222L; } public void writeStringLatin1(byte[] value) { final long vecQuote = this.byteVectorQuote; int i = 0; final int upperBound = (value.length - i) & ~7; // 这里一次检测8个byte是否存在特别字符 for (; i < upperBound; i += 8) { if (containsEscaped(IOUtils.getLongLittleEndian(value, i), vecQuote)) { break; } } // ... } static boolean containsEscaped(long v, long quote) { /* for (int i = 0; i < 8; ++i) { byte c = (byte) data; if (c == quote || c == '\\' || c < ' ') { return true; } data >>>= 8; } return false; */ long x22 = v ^ quote; // " -> 0x22, ' -> 0x27 long x5c = v ^ 0x5c5c5c5c5c5c5c5cL; x22 = (x22 - 0x0101010101010101L) & ~x22; x5c = (x5c - 0x0101010101010101L) & ~x5c; return ((x22 | x5c | (0x7F7F_7F7F_7F7F_7F7FL - v + 0x1010_1010_1010_1010L) | v) & 0x8080808080808080L) != 0; } }
https://github.com/alibaba/fastjson2/blob/2.0.54/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java#L484
在JDK 16+的版本下,使用StringLatin1.indexOfChar方法加速扫描特殊字符,优化readString的性能。这个算法来自 wycst 的贡献。
class JSONReaderASCII { static final int ESCAPE_INDEX_NOT_SET = -2; protected int nextEscapeIndex = ESCAPE_INDEX_NOT_SET; public String readString() { int slashIndex = nextEscapeIndex; if (slashIndex == ESCAPE_INDEX_NOT_SET || (slashIndex != -1 && slashIndex < offset)) { nextEscapeIndex = slashIndex = IOUtils.indexOfChar(bytes, '\\', offset, end); } if (slashIndex == -1 || slashIndex > index) { valueLength = index - offset; offset = index; // ... } }
优化的技巧是一次性读取两个数字,如下:
package com.alibaba.fastjson2; class JSONReaderUTF8 { public final int readInt32Value() { // ... while (inRange && offset + 1 < end && (digit = IOUtils.digit2(bytes, offset)) != -1 ) { // max digits is 19, no need to check inRange (result == MULT_MIN_100 && digit <= (MULT_MIN_100 * 100 - limit)) if (inRange = (result > INT_32_MULT_MIN_100)) { result = result * 100 - digit; offset += 2; } } } }
package com.alibaba.fastjson2.util; class IOUtils { public static int digit2(byte[] bytes, int off) { short x = UNSAFE.getShort(bytes, ARRAY_BYTE_BASE_OFFSET + off); if (BIG_ENDIAN) { x = Short.reverseBytes(x); } int d; if ((((x & 0xF0F0) - 0x3030) | (((d = x & 0x0F0F) + 0x0606) & 0xF0F0)) != 0 ) { return -1; } return ((d & 0xF) << 3) + ((d & 0xF) << 1) // (d & 0xF) * 10 + (d >> 8); } }
这个优化最初灵感源泉来自 https://github.com/wycst/wast 的 io.github.wycst.wast.json.JSONTypeDeserializer.NumberImpl#deserializeInteger所采用的算法,然后做了进一步的改进。
{
import com.alibaba.fastjson2.JSONFactory; // 手工关闭 JSONFactory.setUseGsonAnnotation(false);
也支持通过JVM启动参数关闭
-Dfastjson2.useGsonAnnotation=false
<dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.54</version> </dependency>
<dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.54.android5</version> </dependency>
<dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.54.android8</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.54</version> </dependency>
<dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2-extension-spring5</artifactId> <version>2.0.54</version> </dependency>
<dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2-extension-spring6</artifactId> <version>2.0.54</version> </dependency>
评论删除后,数据将无法恢复
FASTJSON 2.0.54 版本发布,性能进一步提
这又是一个性能优化Bug修复的版本更新版本,大家按需升级。
1. 性能优化
这个版本的性能优化包括:
1.1 使用SWAR(SIMD Within A Register)技巧来优化序列化字符串的性能
序列化时,写字符串检测是否存在特别字符是一个性能关键点,这个版本使用SWAR(SIMD Within A Register)的技巧来做快速检测。如下
https://github.com/alibaba/fastjson2/blob/2.0.54/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java#L484
1.2 优化在JDK 16+的readString性能
在JDK 16+的版本下,使用StringLatin1.indexOfChar方法加速扫描特殊字符,优化readString的性能。这个算法来自 wycst 的贡献。
1.3 int/long/float/double的读取写性能
优化的技巧是一次性读取两个数字,如下:
这个优化最初灵感源泉来自 https://github.com/wycst/wast 的 io.github.wycst.wast.json.JSONTypeDeserializer.NumberImpl#deserializeInteger所采用的算法,然后做了进一步的改进。
2. Issues
{不报错的问题 #2592fastjson2缺省是能识别Gson的Annotation的,这个可以通过接口或者JVM启动参数关闭
也支持通过JVM启动参数关闭
MAVEN依赖配置
这个版本支持java.time和Optional
3. 相关链接