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
1b8644f to
b2862ad
Compare
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
b2862ad to
e48a9f2
Compare
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
When outputting to stdout (no
--output-file), sjsonnet renders JSON into aStringWriter, callstoString()to get the full string, thenprintln()to write it. This creates a redundant copy: the JSON is materialized in memory as aStringbefore being written. For large outputs (e.g., Kubernetes manifests), this doubles peak memory usage.Key Design Decision
When writing to stdout (no output file), use a
ByteArrayOutputStream+OutputStreamWriterpipeline instead ofStringWriter. This:String)baos.writeTo(stdout)for a single bulk writeModification
sjsonnet/src-jvm-native/sjsonnet/SjsonnetMainBase.scala:stdout: PrintStreamparameter towriteToFileandrenderNormalstdout != nulland no output file:ByteArrayOutputStream(65536)→OutputStreamWriter→writeTo(stdout)stdoutthroughprocessFile→renderNormal→writeToFileBenchmark Results
This optimization targets CLI I/O throughput, not JMH evaluation speed. The benefit is:
Stringfor stdout outputwriteTocall vstoString()+println()time sjsonnet large_file.jsonnet > /dev/nullAnalysis
ByteArrayOutputStreambuffers all output. On error, the buffer is discarded — no partial output reaches stdout.--output-filepath unchanged.References
ByteArrayOutputStream.writeTo(OutputStream)— zero-copy transfer to stdoutStringWriter.toStringallocation patternResult
Eliminates intermediate String allocation for stdout output. Reduces peak memory and I/O copies for CLI usage. Draft PR pending native benchmark data.