perf: direct-write stdout bypass StringWriter/StringBuffer allocation#680
Draft
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Draft
perf: direct-write stdout bypass StringWriter/StringBuffer allocation#680He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
When writing to stdout (no --output-file), render directly through OutputStreamWriter(BufferedOutputStream(stdout, 65536)) instead of accumulating in a StringWriter then calling toString + println. For large outputs this eliminates ~3x output-size of intermediate char[] allocations from StringBuffer doubling growth + toString copy. The mainConfigured() API gains an optional stdout parameter (default null) that preserves backward compatibility — callers not passing stdout get the existing StringWriter behavior. Upstream: b09647c0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The current output path materializes the entire JSON result into a
StringviaStringWriter, then writes it to stdout. For large outputs, this doubles memory usage (JSON in memory + string copy) and adds GC pressure.Key Design Decision
Write directly to stdout via
OutputStreamWriter, bypassing the intermediateStringWriter/StringBufferallocation entirely. The renderer writes characters directly to the output stream.Modification
StringWriterallocation for stdout outputBenchmark Results
JMH (JVM, 3 iterations warmup + 3 measurement)
Analysis
The realistic2 improvement (-18.1%) is very significant because realistic2 produces large JSON output. By writing directly to stdout, we eliminate the ~303KB intermediate string allocation and the subsequent copy. The comparison2 improvement (-22.2%) also benefits from avoiding the string buffer for its output. This is one of the highest-impact PRs for realistic2 with only 27 lines of code.
References
b09647c0Result
All 5 tests pass. All benchmarks strongly positive, especially realistic2. No regressions.