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 { ...@@ -841,6 +841,9 @@ class LZ4FrameCodec final : public Codec {
void resetDCtx(); void resetDCtx();
int level_; int level_;
#ifdef FOLLY_USE_LZ4_FAST_RESET
LZ4F_compressionContext_t cctx_{nullptr};
#endif
LZ4F_decompressionContext_t dctx_{nullptr}; LZ4F_decompressionContext_t dctx_{nullptr};
bool dirty_{false}; bool dirty_{false};
}; };
...@@ -908,6 +911,11 @@ LZ4FrameCodec::~LZ4FrameCodec() { ...@@ -908,6 +911,11 @@ LZ4FrameCodec::~LZ4FrameCodec() {
if (dctx_) { if (dctx_) {
LZ4F_freeDecompressionContext(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) { std::unique_ptr<IOBuf> LZ4FrameCodec::doCompress(const IOBuf* data) {
...@@ -917,6 +925,13 @@ 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(); clone = data->cloneCoalescedAsValue();
data = &clone; data = &clone;
} }
#ifdef FOLLY_USE_LZ4_FAST_RESET
if (!cctx_) {
lz4FrameThrowOnError(LZ4F_createCompressionContext(&cctx_, LZ4F_VERSION));
}
#endif
// Set preferences // Set preferences
const auto uncompressedLength = data->length(); const auto uncompressedLength = data->length();
LZ4F_preferences_t prefs{}; LZ4F_preferences_t prefs{};
...@@ -924,12 +939,25 @@ std::unique_ptr<IOBuf> LZ4FrameCodec::doCompress(const IOBuf* data) { ...@@ -924,12 +939,25 @@ std::unique_ptr<IOBuf> LZ4FrameCodec::doCompress(const IOBuf* data) {
prefs.frameInfo.contentSize = uncompressedLength; prefs.frameInfo.contentSize = uncompressedLength;
// Compress // Compress
auto buf = IOBuf::create(maxCompressedLength(uncompressedLength)); auto buf = IOBuf::create(maxCompressedLength(uncompressedLength));
const size_t written = lz4FrameThrowOnError(LZ4F_compressFrame( const size_t written = lz4FrameThrowOnError(
buf->writableTail(), #ifdef FOLLY_USE_LZ4_FAST_RESET
buf->tailroom(), LZ4F_compressFrame_usingCDict(
data->data(), cctx_,
data->length(), buf->writableTail(),
&prefs)); buf->tailroom(),
data->data(),
data->length(),
nullptr,
&prefs)
#else
LZ4F_compressFrame(
buf->writableTail(),
buf->tailroom(),
data->data(),
data->length(),
&prefs)
#endif
);
buf->append(written); buf->append(written);
return buf; 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