Skip to content

perf: cache ASCII field names up to 32 bytes - #7971

Open
hutiefang76 wants to merge 3 commits into
alibaba:mainfrom
hutiefang76:frank/perf-fastjson7968-long-field-cache-20261125
Open

hutiefang76 wants to merge 3 commits into
alibaba:mainfrom
hutiefang76:frank/perf-fastjson7968-long-field-cache-20261125

Conversation

@hutiefang76

@hutiefang76 hutiefang76 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

Repeated unescaped ASCII field names longer than 16 bytes currently allocate a new String when parsing UTF-8 JSON into a JSONObject. This adds a bounded cache for names of 17–32 bytes, covering the repeated long keys in the OTLP metrics example from #7968.

Summary of your change

  • Add a separate 8,192-slot cache, so long names do not displace existing short-name entries. Cache hits compare the full name to handle hash collisions.
  • Cover both the default ASCII reader and the UTF-8 reader. Preserve the existing short-name paths and escaped/non-ASCII handling; retain no reference to mutable input buffers.
  • Add regressions for reuse, boundary lengths, collisions, offsets and reused buffers, concurrency, both reader routes, and Latin-1 names.
  • Add a JMH benchmark with the pinned, Apache-2.0-licensed OpenTelemetry metrics fixture and a short-key control. The control changes only the three long property names during setup.

JMH 1.37, JDK 17.0.18, macOS/aarch64, G1, 256 MiB heap, one thread, two forks, 3 × 1 s warmup and 5 × 1 s measurement per fork, GC profiler:

Payload Baseline B/op Patched B/op Baseline ops/µs Patched ops/µs
OTLP 11096.02 10648.02 0.26214 ± 0.00148 0.26430 ± 0.00694
Short keys 10648.02 10648.02 0.27625 ± 0.00875 0.27875 ± 0.00500

The OTLP payload allocates approximately 448 fewer bytes per parse (4.04%). Throughput error ranges overlap in both comparisons; these measurements do not establish a throughput improvement. The new helper is 285 bytecodes, below the 325-byte C2 frequent-inlining threshold; actual JIT inlining is not claimed.

Local validation:

  • JDK 17 full reactor, ./mvnw -B -Pgen-code-cov clean test: 10,077 tests, zero failures/errors/skips.
  • JDK 17 full reactor, ./mvnw -B -Dfastjson2.creator=reflect clean test: 10,077 tests, zero failures/errors, three skipped.
  • Six explicit ISO-8859-1/ASCII-reader regressions pass on JDK 8 and 17. On the original 460d2e4 baseline, the four long-name identity cases fail and the collision/buffer correctness controls pass. The buffer test rechecks the first result after the input bytes are overwritten.
  • Local JDK 17 JaCoCo (gen-code-cov): 31 of 34 added executable production lines covered (91.18%); both added ASCII-reader lines covered. This is a local report, not an upstream Codecov result.
  • JDK 8 and 17 native probes: default ASCII and explicit UTF-8 readers reuse 17–32-byte keys; short keys and non-ASCII values remain correct.
  • ./mvnw -B validate: passed across the reactor.
  • ./mvnw -V --no-transfer-progress -Pgen-javadoc clean package at 5319278, before the test-only follow-up: passed on JDK 17, including 10,071 tests with zero failures/errors/skips and Javadoc generation. Production and benchmark sources are unchanged by the test follow-up.
  • Direct benchmark Javadoc checks with all doclint checks enabled: passed on JDK 8, 17, and 21.
  • ./mvnw -B -pl benchmark -am -DskipTests clean package: passed; this package check skips tests.

AI-assisted implementation and review; the reported tests and measurements were executed locally.

Refs #7968.

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed. No public API or configuration changes; benchmark and fixture attribution are included.
中文

将 UTF-8 JSON 解析为 JSONObject 时,长度超过 16 字节且不含转义的 ASCII 字段名目前会重复分配 String。本补丁增加独立的 8,192 槽位缓存,覆盖 17–32 字节字段名,并在命中时比较完整名称,避免哈希碰撞混淆。默认 ASCII reader 和显式 UTF-8 reader 都已覆盖;原有短名称、转义及非 ASCII 处理保持原路径,缓存不保留可变输入缓冲区。

新增回归覆盖字段名复用、边界长度、碰撞、偏移和缓冲区复用、并发、两条 reader 路径及 Latin-1 名称。基准包含固定版本的 OpenTelemetry 官方指标样本、Apache-2.0 授权说明,以及仅在 setup 阶段缩短三个长字段名的对照组。

在上述同机 JMH 参数下,OTLP 每次解析分配量约减少 448 字节(4.04%);短键对照的分配量保持相同。两组吞吐量误差范围均重叠,因此不据此宣称吞吐量提升。新辅助方法为 285 字节码,低于 C2 热方法 325 字节的内联阈值,不代表已验证实际内联。

JDK 17 全仓 clean 标准模式(启用 gen-code-cov)10,077 项测试通过,无失败、错误或跳过;全仓 clean reflect 模式同样 10,077 项,无失败或错误,跳过三项。JDK 8/17 两条 reader 的原生探针、全仓静态检查及跳过测试的基准模块 clean package 已通过。两条全仓命令均已成功完成。另已在 5319278(本次仅测试的修订之前)以 JDK 17 完成全仓 -Pgen-javadoc clean package,10,071 项测试及文档生成均通过;运行时及基准源码在这次测试修订中保持相同,JDK 8/17/21 的基准类 doclint 检查也均通过。

新增六例显式 ISO-8859-1/ASCII reader 回归在 JDK 8/17 均通过;原始 460d2e4 基线的四例长键复用断言失败,碰撞与缓冲区正确性控制通过。缓冲区用例会在覆盖输入后重新检查第一次解析的结果。本地 JDK 17 JaCoCo 显示新增可执行生产行 31/34(91.18%)已覆盖,其中 ASCII reader 新增两行均覆盖;该结果不代表上游 Codecov。

使用 AI 辅助实现和审查;所报告的数据与测试均在本地实际执行。关联 #7968。

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.47059% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ain/java/com/alibaba/fastjson2/JSONReaderUTF8.java 77.41% 3 Missing and 4 partials ⚠️
...in/java/com/alibaba/fastjson2/JSONReaderASCII.java 50.00% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@hutiefang76

hutiefang76 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

The current run at 52c023b finished with 14 jobs passing and one failure. The Ubuntu/JDK 21 job failed before compilation while downloading Spring BOMs from Maven Central with HTTP 429, including its built-in retry. Could a maintainer rerun that failed job?

The six explicit ASCII-reader regressions pass locally on JDK 8 and 17. Both local full-reactor modes report 10,077 tests with zero failures/errors; reflect skips three. This request is for the current Ubuntu/JDK 21 job and supersedes the older-head Ubuntu/JDK 25 request.

中文

52c023b 当前 CI 已结束:14 项通过,唯一失败的是 Ubuntu/JDK 21 作业。已读取完整日志,两次尝试均在编译前下载 Spring BOM 时遇到 Maven Central HTTP 429。请维护者协助重跑该失败作业。

六例显式 ASCII reader 回归在本地 JDK 8/17 均通过;两种本地全仓模式均为 10,077 项、无失败或错误,reflect 跳过三项。本次请求对应当前 Ubuntu/JDK 21 作业,取代旧提交 Ubuntu/JDK 25 的请求。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants