Optimize FilePath.readToString #3862
Merged
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.
While discussing an
OutOfMemoryErrorwhose investigation led to jenkinsci/workflow-durable-task-step-plugin#96, @dwnusbaum pointed out thatFilePath.readToStringwinds up allocating aIOUtils.DEFAULT_BUFFER_SIZE~ 4Kib buffer. This is clearly inefficient for the common case of reading a small text file, not to mention the overhead of usingProxyOutputStreamin the Remoting channel if the file is remote. Better in typical cases to just read the whole file at once remotely and send back one packet with the result. (The current implementation would only make sense for huge text files, and using this method for such cases is likely a sign of poor code anyway.)Note that I avoided
FileUtils.readFileToStringfrom Commons IO since it has the same 4Kib buffer size, perversely.Also note that the rewritten implementation alters the behavior slightly: the default charset being used is now that of the agent, rather than the master. This is generally what you would want, and matches the behavior of
FilePath.write(content, null).Proposed changelog entries