-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Reduce memory usage in writer by freeing unused buffers #23724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| */ | ||
| package com.facebook.presto.orc; | ||
|
|
||
| import com.facebook.airlift.log.Logger; | ||
| import io.airlift.slice.Slice; | ||
| import io.airlift.slice.SliceOutput; | ||
| import io.airlift.slice.Slices; | ||
|
|
@@ -61,9 +62,9 @@ public final class ChunkedSliceOutput | |
| */ | ||
| private int bufferPosition; | ||
|
|
||
| public ChunkedSliceOutput(int minChunkSize, int maxChunkSize) | ||
| public ChunkedSliceOutput(int minChunkSize, int maxChunkSize, boolean resetOutputBuffer) | ||
| { | ||
| this.chunkSupplier = new ChunkSupplier(minChunkSize, maxChunkSize); | ||
| this.chunkSupplier = new ChunkSupplier(minChunkSize, maxChunkSize, resetOutputBuffer); | ||
|
|
||
| this.buffer = chunkSupplier.get(); | ||
| this.slice = Slices.wrappedBuffer(buffer); | ||
|
|
@@ -344,25 +345,37 @@ private void closeChunk() | |
| // reusing the buffers. | ||
| private static class ChunkSupplier | ||
| { | ||
| private static final Logger log = Logger.get(ChunkSupplier.class); | ||
| private final int maxChunkSize; | ||
| private boolean resetOutputBuffer; | ||
|
|
||
| private final List<byte[]> bufferPool = new ArrayList<>(); | ||
| private final List<byte[]> usedBuffers = new ArrayList<>(); | ||
|
|
||
| private int currentSize; | ||
|
|
||
| public ChunkSupplier(int minChunkSize, int maxChunkSize) | ||
| public ChunkSupplier(int minChunkSize, int maxChunkSize, boolean resetOutputBuffer) | ||
| { | ||
| checkArgument(minChunkSize >= MINIMUM_CHUNK_SIZE, "minimum chunk size of " + MINIMUM_CHUNK_SIZE + " required"); | ||
| checkArgument(maxChunkSize <= MAXIMUM_CHUNK_SIZE, "maximum chunk size of " + MAXIMUM_CHUNK_SIZE + " required"); | ||
| checkArgument(minChunkSize <= maxChunkSize, "minimum chunk size must be less than maximum chunk size"); | ||
|
|
||
| this.currentSize = minChunkSize; | ||
| this.maxChunkSize = maxChunkSize; | ||
| this.resetOutputBuffer = resetOutputBuffer; | ||
| } | ||
|
|
||
| public void reset() | ||
| { | ||
| if (!bufferPool.isEmpty() && resetOutputBuffer) { | ||
| log.info("Reset unused buffers, used %d chunks (%d bytes), unused %d chunks (%d bytes)", | ||
| usedBuffers.size(), | ||
| usedBuffers.stream().mapToInt(b -> b.length).sum(), | ||
| bufferPool.size(), | ||
| bufferPool.stream().mapToInt(b -> b.length).sum()); | ||
| bufferPool.clear(); | ||
| System.setProperty("RESET_OUTPUT_BUFFER", "RESET_OUTPUT_BUFFER"); | ||
| } | ||
| bufferPool.addAll(0, usedBuffers); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to save on the memory, do you really want to keep the last batch of chunks? I suppose you don't. |
||
| usedBuffers.clear(); | ||
| } | ||
|
|
||
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a tricky place. If you look at the ChunkSupplier.get method you will see that ChunkSupplier scales up chunks from 256 bytes all the way to 16MB when it needs to produce a new chunk. If you reset the pool and start producing new chunks you will eventually end up only with big 16MB new chunks. Which is probably opposite of your goal.
I don't know what is a best strategy here, because it's really depends on the data shapes, but I see a few options:
resetresetting the current size to min size. Good middle ground option.resetresetting the current size to min size AND using smaller max chunk size. Will perform even better in terms of overhead, won't regress as much as 1 for small streams.