-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 PrestoSerializer decompress buffer #11836
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -431,6 +431,44 @@ TEST_P(InputByteStreamTest, inputStream) { | |
ASSERT_TRUE(byteStream->atEnd()); | ||
} | ||
|
||
TEST_P(InputByteStreamTest, iobuf) { | ||
if (!GetParam()) { | ||
return; | ||
} | ||
const auto streamSize = 4096; | ||
std::vector<ByteRange> byteRanges; | ||
std::uint8_t buffer[streamSize]; | ||
for (auto i = 0; i < streamSize; ++i) { | ||
buffer[i] = i % 256; | ||
} | ||
byteRanges.push_back(ByteRange{buffer, streamSize, 0}); | ||
|
||
std::uint8_t buffer2[streamSize]; | ||
for (auto i = 0; i < streamSize; ++i) { | ||
buffer2[i] = i % 13; | ||
} | ||
byteRanges.push_back(ByteRange{buffer2, streamSize, 0}); | ||
|
||
auto byteStream = createStream(byteRanges); | ||
auto bufferStream = dynamic_cast<BufferInputStream*>(byteStream.get()); | ||
for (int offset = 0; offset < streamSize;) { | ||
auto iobuf = bufferStream->readBytes(streamSize / 8); | ||
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. We are not testing the case one IOBuf chain cross 2 buffers. Let's cover that as well. |
||
ASSERT_EQ(iobuf->computeChainDataLength(), streamSize / 8); | ||
for (int i = 0; i < streamSize / 8; ++i, ++offset) { | ||
ASSERT_EQ(iobuf->data()[i], offset % 256); | ||
} | ||
} | ||
|
||
for (int offset = 0; offset < streamSize;) { | ||
auto iobuf = bufferStream->readBytes(streamSize / 8); | ||
ASSERT_EQ(iobuf->computeChainDataLength(), streamSize / 8); | ||
for (int i = 0; i < streamSize / 8; ++i, ++offset) { | ||
ASSERT_EQ(iobuf->data()[i], offset % 13); | ||
} | ||
} | ||
ASSERT_TRUE(bufferStream->atEnd()); | ||
} | ||
|
||
TEST_P(InputByteStreamTest, emptyInputStreamError) { | ||
if (GetParam()) { | ||
VELOX_ASSERT_THROW(createStream({}), "Empty BufferInputStream"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4359,6 +4359,27 @@ void readTopColumns( | |
readColumns( | ||
&source, childTypes, resultOffset, nullptr, 0, pool, opts, children); | ||
} | ||
|
||
std::unique_ptr<folly::IOBuf> uncompressStream( | ||
ByteInputStream* source, | ||
const PrestoHeader& header, | ||
common::CompressionKind compressionKind, | ||
memory::MemoryPool& pool) { | ||
const auto codec = common::compressionKindToCodec(compressionKind); | ||
if (dynamic_cast<BufferInputStream*>(source)) { | ||
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. Just do the |
||
// If the source is a BufferInputStream, we can avoid copying the data | ||
// by creating an IOBuf from the underlying buffer. | ||
const auto bufferSource = dynamic_cast<BufferInputStream*>(source); | ||
const auto iobuf = bufferSource->readBytes(header.compressedSize); | ||
// Process chained uncompressed results IOBufs. | ||
return codec->uncompress(iobuf.get(), header.uncompressedSize); | ||
} | ||
auto compressBuf = folly::IOBuf::create(header.compressedSize); | ||
source->readBytes(compressBuf->writableData(), header.compressedSize); | ||
compressBuf->append(header.compressedSize); | ||
// Process chained uncompressed results IOBufs. | ||
return codec->uncompress(compressBuf.get(), header.uncompressedSize); | ||
} | ||
} // namespace | ||
|
||
void PrestoVectorSerde::deserialize( | ||
|
@@ -4369,8 +4390,6 @@ void PrestoVectorSerde::deserialize( | |
vector_size_t resultOffset, | ||
const Options* options) { | ||
const auto prestoOptions = toPrestoOptions(options); | ||
const auto codec = | ||
common::compressionKindToCodec(prestoOptions.compressionKind); | ||
auto maybeHeader = PrestoHeader::read(source); | ||
VELOX_CHECK( | ||
maybeHeader.hasValue(), | ||
|
@@ -4413,13 +4432,8 @@ void PrestoVectorSerde::deserialize( | |
if (!isCompressedBitSet(header.pageCodecMarker)) { | ||
readTopColumns(*source, type, pool, *result, resultOffset, prestoOptions); | ||
} else { | ||
auto compressBuf = folly::IOBuf::create(header.compressedSize); | ||
source->readBytes(compressBuf->writableData(), header.compressedSize); | ||
compressBuf->append(header.compressedSize); | ||
|
||
// Process chained uncompressed results IOBufs. | ||
auto uncompress = | ||
codec->uncompress(compressBuf.get(), header.uncompressedSize); | ||
uncompressStream(source, header, prestoOptions.compressionKind, *pool); | ||
auto uncompressedSource = std::make_unique<BufferInputStream>( | ||
byteRangesFromIOBuf(uncompress.get())); | ||
readTopColumns( | ||
|
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.
The order of buffers here is incorrect. Just do
result->appendToChain(std::move(newBuf))
.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.
The order is same with IOBufOutputStream::getIOBuf(), appendToChain is also OK. I will change as you say.