Commit 8acd0060 authored by Felix Handte's avatar Felix Handte Committed by Facebook Github Bot

Init on First Use and Retain LZ4F Contexts

Summary:
Reusing a context can provide significant efficiency benefits.

Depends on D8172871.

Reviewed By: yfeldblum, terrelln

Differential Revision: D8287767

fbshipit-source-id: 2565e7a959b2ac0911f0a4d6e1596f9da6d12ee8
parent 5b5e8e88
......@@ -841,6 +841,9 @@ class LZ4FrameCodec final : public Codec {
void resetDCtx();
int level_;
#ifdef FOLLY_USE_LZ4_FAST_RESET
LZ4F_compressionContext_t cctx_{nullptr};
#endif
LZ4F_decompressionContext_t dctx_{nullptr};
bool dirty_{false};
};
......@@ -908,6 +911,11 @@ LZ4FrameCodec::~LZ4FrameCodec() {
if (dctx_) {
LZ4F_freeDecompressionContext(dctx_);
}
#ifdef FOLLY_USE_LZ4_FAST_RESET
if (cctx_) {
LZ4F_freeCompressionContext(cctx_);
}
#endif
}
std::unique_ptr<IOBuf> LZ4FrameCodec::doCompress(const IOBuf* data) {
......@@ -917,6 +925,13 @@ std::unique_ptr<IOBuf> LZ4FrameCodec::doCompress(const IOBuf* data) {
clone = data->cloneCoalescedAsValue();
data = &clone;
}
#ifdef FOLLY_USE_LZ4_FAST_RESET
if (!cctx_) {
lz4FrameThrowOnError(LZ4F_createCompressionContext(&cctx_, LZ4F_VERSION));
}
#endif
// Set preferences
const auto uncompressedLength = data->length();
LZ4F_preferences_t prefs{};
......@@ -924,12 +939,25 @@ std::unique_ptr<IOBuf> LZ4FrameCodec::doCompress(const IOBuf* data) {
prefs.frameInfo.contentSize = uncompressedLength;
// Compress
auto buf = IOBuf::create(maxCompressedLength(uncompressedLength));
const size_t written = lz4FrameThrowOnError(LZ4F_compressFrame(
buf->writableTail(),
buf->tailroom(),
data->data(),
data->length(),
&prefs));
const size_t written = lz4FrameThrowOnError(
#ifdef FOLLY_USE_LZ4_FAST_RESET
LZ4F_compressFrame_usingCDict(
cctx_,
buf->writableTail(),
buf->tailroom(),
data->data(),
data->length(),
nullptr,
&prefs)
#else
LZ4F_compressFrame(
buf->writableTail(),
buf->tailroom(),
data->data(),
data->length(),
&prefs)
#endif
);
buf->append(written);
return buf;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment