Skip to content
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

feat: Optimize serializer decompress buffer for BufferInputStream #11836

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jinchengchenghh
Copy link
Contributor

@jinchengchenghh jinchengchenghh commented Dec 12, 2024

Read the BufferInputStream by new added function readBytes to avoid copying from the buffer cache. Extract the uncompress process to a new function can help release the compressed buffer in time.

FileInputStream reuse the read buffer, so we can get the IOBuf without copy only if the required bytes is in one buffer, which is not the common case, so I don't do the optimization for it.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 12, 2024
Copy link

netlify bot commented Dec 12, 2024

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit 38fe41c
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/6763b06231aacc0008891d07

@jinchengchenghh jinchengchenghh changed the title feat: Optimize PrestoSerializer compress buffer feat: Optimize PrestoSerializer decompress buffer for BufferInputStream Dec 13, 2024
@jinchengchenghh
Copy link
Contributor Author

Can you help review again? Thanks! @Yuhta

auto newBuf = folly::IOBuf::wrapBuffer(
current_->buffer + current_->position, readBytes);
if (result) {
result->prev()->appendChain(std::move(newBuf));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of buffers here is incorrect. Just do result->appendToChain(std::move(newBuf)).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order is same with IOBufOutputStream::getIOBuf(), appendToChain is also OK. I will change as you say.

common::CompressionKind compressionKind,
memory::MemoryPool& pool) {
const auto codec = common::compressionKindToCodec(compressionKind);
if (dynamic_cast<BufferInputStream*>(source)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just do the dynamic_cast once: if (const auto* bufferSource = dynamic_cast<BufferInputStream*>(source))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implement the readBytes return IOBuf in FileInputStream, so we don't need to do the cast, and other compression code can also benefit from this.

auto byteStream = createStream(byteRanges);
auto bufferStream = dynamic_cast<BufferInputStream*>(byteStream.get());
for (int offset = 0; offset < streamSize;) {
auto iobuf = bufferStream->readBytes(streamSize / 8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not testing the case one IOBuf chain cross 2 buffers. Let's cover that as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.

@jinchengchenghh jinchengchenghh changed the title feat: Optimize PrestoSerializer decompress buffer for BufferInputStream feat: Optimize PrestoSerializer decompress buffer Dec 14, 2024
@jinchengchenghh
Copy link
Contributor Author

Failed by this issue #11857

@jinchengchenghh
Copy link
Contributor Author

Can you help review again? Thanks! @Yuhta

current_->position += size;
return iobuf;
}
auto iobuf = folly::IOBuf::create(size);
Copy link
Contributor

@Yuhta Yuhta Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allocation should be avoided. Can we make a chain that takeOwnership() of the buffers instead?

We would also need to put pins on the buffers so that they are not overwritten while we have IOBuf on top of them. If this feels too complicated we can leave this out for next PR and just cast to BufferInputStream for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is complicated, and it breaks the FileInputStream should only reserve memory as QueryConfig::readBufferSize. Alternatively, we could add a new argument MemoryPool* pool = nullptr, allocated the Buffer from pool, and wrap the buffer as IOBuf. Which way do you think is better? This maybe in a follow up PR.

@jinchengchenghh jinchengchenghh changed the title feat: Optimize PrestoSerializer decompress buffer feat: Optimize serializer decompress buffer for BufferInputStream Dec 19, 2024
@jinchengchenghh
Copy link
Contributor Author

I move to the cast to BufferInputStream as you say, and compress Row has merged, so I update it in same way. Can you help review again? Thanks~ @Yuhta

@jinchengchenghh
Copy link
Contributor Author

Mac13 failed by arrow download, velox release uts failed by existing issue hdfs test, other tests passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants