-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Reduce memory usage in writer with more memory efficient output buffer implementation #24913
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
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
36 changes: 36 additions & 0 deletions
36
presto-orc/src/main/java/com/facebook/presto/orc/OrcChunkedOutputBuffer.java
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.facebook.presto.orc; | ||
|
|
||
| import io.airlift.slice.SliceOutput; | ||
|
|
||
| public interface OrcChunkedOutputBuffer | ||
| { | ||
| void writeTo(SliceOutput outputStream); | ||
|
|
||
| void reset(); | ||
|
|
||
| int size(); | ||
|
|
||
| long getRetainedSize(); | ||
|
|
||
| // need to be called before writing | ||
| void ensureAvailable(int minLength, int length); | ||
|
|
||
| void writeHeader(int value); | ||
|
|
||
| void writeBytes(byte[] source, int sourceIndex, int length); | ||
|
|
||
| String toString(); | ||
| } |
145 changes: 145 additions & 0 deletions
145
presto-orc/src/main/java/com/facebook/presto/orc/OrcLazyChunkedOutputBuffer.java
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 |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.facebook.presto.orc; | ||
|
|
||
| import io.airlift.slice.SliceOutput; | ||
| import org.openjdk.jol.info.ClassLayout; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static java.lang.Math.min; | ||
| import static java.lang.Math.toIntExact; | ||
|
|
||
| public class OrcLazyChunkedOutputBuffer | ||
| implements OrcChunkedOutputBuffer | ||
| { | ||
| private static final int INSTANCE_SIZE = ClassLayout.parseClass(ChunkedSliceOutput.class).instanceSize(); | ||
| private byte[] buffer; | ||
| private final List<byte[]> closedBuffers = new ArrayList<>(); | ||
| private final List<Integer> closedBufferLengths = new ArrayList<>(); | ||
| private long closedBuffersRetainedSize; | ||
|
|
||
| /** | ||
| * Offset of buffer within stream. | ||
| */ | ||
| private long streamOffset; | ||
|
|
||
| /** | ||
| * Current position for writing in buffer. | ||
| */ | ||
| private int bufferPosition; | ||
|
|
||
| @Override | ||
| public void writeTo(SliceOutput outputStream) | ||
| { | ||
| for (int i = 0; i < closedBuffers.size(); i++) { | ||
| outputStream.writeBytes(closedBuffers.get(i), 0, closedBufferLengths.get(i)); | ||
| } | ||
| if (bufferPosition > 0) { | ||
| outputStream.writeBytes(buffer, 0, bufferPosition); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() | ||
| { | ||
| closedBuffers.clear(); | ||
| closedBufferLengths.clear(); | ||
| closedBuffersRetainedSize = 0; | ||
| streamOffset = 0; | ||
| bufferPosition = 0; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() | ||
| { | ||
| return toIntExact(streamOffset + bufferPosition); | ||
| } | ||
|
|
||
| @Override | ||
| public long getRetainedSize() | ||
| { | ||
| return buffer.length + closedBuffersRetainedSize + INSTANCE_SIZE; | ||
| } | ||
|
|
||
| // need to be called before writing | ||
| @Override | ||
| public void ensureAvailable(int minLength, int length) | ||
| { | ||
| if (buffer == null) { | ||
| buffer = new byte[length]; | ||
| bufferPosition = 0; | ||
| } | ||
| // no room for minLength | ||
| if (bufferPosition + minLength > buffer.length) { | ||
| closeChunk(length); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeBytes(byte[] source, int sourceIndex, int length) | ||
| { | ||
| while (length > 0) { | ||
| int batch = ensureBatchSize(length); | ||
| System.arraycopy(source, sourceIndex, buffer, bufferPosition, batch); | ||
| bufferPosition += batch; | ||
| sourceIndex += batch; | ||
| length -= batch; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeHeader(int value) | ||
| { | ||
| buffer[bufferPosition] = (byte) (value & 0x00_00FF); | ||
| bufferPosition += 1; | ||
| buffer[bufferPosition] = (byte) ((value & 0x00_FF00) >> 8); | ||
| bufferPosition += 1; | ||
| buffer[bufferPosition] = (byte) ((value & 0xFF_0000) >> 16); | ||
| bufferPosition += 1; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() | ||
| { | ||
| StringBuilder builder = new StringBuilder("OrcLazyChunkedOutputBuffer{"); | ||
| builder.append("position=").append(size()); | ||
| builder.append('}'); | ||
| return builder.toString(); | ||
| } | ||
|
|
||
| private int ensureBatchSize(int length) | ||
| { | ||
| // no room | ||
| if (bufferPosition >= buffer.length) { | ||
| closeChunk(length); | ||
| } | ||
| return min(length, buffer.length - bufferPosition); | ||
| } | ||
|
|
||
| private void closeChunk(int length) | ||
| { | ||
| // add trimmed view of slice to closed slices | ||
| closedBuffers.add(buffer); | ||
| closedBufferLengths.add(bufferPosition); | ||
| closedBuffersRetainedSize += buffer.length; | ||
|
|
||
| // create a new buffer | ||
| buffer = new byte[length]; | ||
|
|
||
| streamOffset += bufferPosition; | ||
| bufferPosition = 0; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Please change to a more concise: