Skip to content

Increase default JSON string buffer capacity - #3125

Open
sb123sb123 wants to merge 1 commit into
google:mainfrom
sb123sb123:fix/2645-string-capacity
Open

sb123sb123 wants to merge 1 commit into
google:mainfrom
sb123sb123:fix/2645-string-capacity

Conversation

@sb123sb123

Copy link
Copy Markdown

Fixes #2645

Why

The implicit JSON string output paths still started with the default StringBuilder capacity of 16. For a 33-character string, the JSON output is 35 characters but the builder grows to a capacity of 70, causing an avoidable resize and copy. This is the remaining issue discussed after #2852 switched the implicit output path from StringWriter to StringBuilder.

What changed

  • Centralize the initial capacity for Gson-created JSON string builders at 256 characters.
  • Use that capacity for Gson.toJson(...), JsonElement.toString(), and TypeAdapter.toJson(...).
  • Leave caller-provided Appendable instances unchanged.

Tests

  • Focused GsonTest: 15 tests, 0 failures.
  • Gson module test suite: 4,674 tests, 0 failures, 21 skipped.
  • mvn -B -ntp -pl gson -Dmaven.repo.local= verify: passed, including compilation, ProGuard processing, OSGi integration tests, Spotless checks, and packaging.

The full multi-module mvn clean verify was not run because the shared Windows G: drive was concurrently space-constrained; the affected gson module completed verify.

AI assistance

AI assistance was used during issue/PR investigation and implementation. The submitted diff was reviewed and the listed checks were run before submission.

@google-cla

google-cla Bot commented Sep 21, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.

Poor performance for String serialization

1 participant