Skip to content

Increase initial capacity across JSON string outputs - #3147

Merged
eamonnmcmanus merged 3 commits into
google:mainfrom
Mr-KnightCLI:perf-tojson-buffer-capacity
Nov 16, 2026
Merged

eamonnmcmanus merged 3 commits into
google:mainfrom
Mr-KnightCLI:perf-tojson-buffer-capacity

Conversation

@Mr-KnightCLI

@Mr-KnightCLI Mr-KnightCLI commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Apply the initial-buffer improvement discussed in #2645 consistently to Gson APIs that produce a JSON String.

Description

  • Centralize the 256-character initial capacity in a package-private JsonElement helper. Keeping it there allows Gson's subset build to compile JsonElement without Gson.
  • Use the helper in Gson.toJson, JsonElement.toString, and TypeAdapter.toJson.
  • Leave caller-provided Appendable output unchanged. JSON serialization output is unchanged; this reduces buffer growth for common-sized results.
  • Add a focused unit test for the shared default capacity.

Validation

  • git diff --check passes.
  • Maven tests could not be run locally because Maven is unavailable. GitHub Actions is running for the updated commit.

@Marcono1234

Copy link
Copy Markdown
Contributor

Thanks! Maybe it would be useful to apply this to JsonElement and TypeAdapter as well, see #3125 (unfortunately that PR was closed due to a missing CLA).

@Mr-KnightCLI Mr-KnightCLI changed the title Increase initial buffer for toJson output Increase initial capacity across JSON string outputs Oct 19, 2026
Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
@eamonnmcmanus
eamonnmcmanus merged commit b8bc582 into google:main Nov 16, 2026
22 checks passed
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.

3 participants