Commit 11c10340 authored by Felix Handte's avatar Felix Handte Committed by Facebook Github Bot

Introduce Singleton Context Pools for Zstd Contexts

Summary:
This is intentionally not hidden in `folly/compression/Zstd.cpp`, so that it
can be used by other Zstd users as well.

Reviewed By: bimbashrestha

Differential Revision: D18890515

fbshipit-source-id: e87eb6acd03f4b4d5f68201ef924984047d0160e
parent 7897d0e4
......@@ -546,5 +546,13 @@ bool hasCodec(CodecType type);
* Check if a specified codec is supported and supports streaming.
*/
bool hasStreamCodec(CodecType type);
/**
* Added here so users of folly can figure out whether the singletons are
* usable, and therefore whether to even try to include the header.
*
* 1 means that the header exists and ZSTD_CCtx and ZSTD_DCtx pools are defined.
*/
#define FOLLY_COMPRESSION_HAS_CONTEXT_POOL_SINGLETONS 1
} // namespace io
} // namespace folly
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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.
*/
#include <folly/compression/CompressionContextPoolSingletons.h>
namespace folly {
namespace compression {
namespace contexts {
namespace {
// These objects have no static dependencies and therefore no SIOF issues.
ZSTD_CCtx_Pool zstd_cctx_pool_singleton;
ZSTD_DCtx_Pool zstd_dctx_pool_singleton;
} // anonymous namespace
ZSTD_CCtx* ZSTD_CCtx_Creator::operator()() const noexcept {
return ZSTD_createCCtx();
}
ZSTD_DCtx* ZSTD_DCtx_Creator::operator()() const noexcept {
return ZSTD_createDCtx();
}
void ZSTD_CCtx_Deleter::operator()(ZSTD_CCtx* ctx) const noexcept {
ZSTD_freeCCtx(ctx);
}
void ZSTD_DCtx_Deleter::operator()(ZSTD_DCtx* ctx) const noexcept {
ZSTD_freeDCtx(ctx);
}
ZSTD_CCtx_Pool::Ref getZSTD_CCtx() {
return zstd_cctx_pool_singleton.get();
}
ZSTD_DCtx_Pool::Ref getZSTD_DCtx() {
return zstd_dctx_pool_singleton.get();
}
ZSTD_CCtx_Pool::Ref getNULL_ZSTD_CCtx() {
return zstd_cctx_pool_singleton.getNull();
}
ZSTD_DCtx_Pool::Ref getNULL_ZSTD_DCtx() {
return zstd_dctx_pool_singleton.getNull();
}
} // namespace contexts
} // namespace compression
} // namespace folly
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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.
*/
#pragma once
#include <zstd.h>
#include <folly/compression/CompressionCoreLocalContextPool.h>
namespace folly {
namespace compression {
namespace contexts {
struct ZSTD_CCtx_Creator {
ZSTD_CCtx* operator()() const noexcept;
};
struct ZSTD_DCtx_Creator {
ZSTD_DCtx* operator()() const noexcept;
};
struct ZSTD_CCtx_Deleter {
void operator()(ZSTD_CCtx* ctx) const noexcept;
};
struct ZSTD_DCtx_Deleter {
void operator()(ZSTD_DCtx* ctx) const noexcept;
};
using ZSTD_CCtx_Pool = CompressionCoreLocalContextPool<
ZSTD_CCtx,
ZSTD_CCtx_Creator,
ZSTD_CCtx_Deleter,
4>;
using ZSTD_DCtx_Pool = CompressionCoreLocalContextPool<
ZSTD_DCtx,
ZSTD_DCtx_Creator,
ZSTD_DCtx_Deleter,
4>;
ZSTD_CCtx_Pool::Ref getZSTD_CCtx();
ZSTD_DCtx_Pool::Ref getZSTD_DCtx();
ZSTD_CCtx_Pool::Ref getNULL_ZSTD_CCtx();
ZSTD_DCtx_Pool::Ref getNULL_ZSTD_DCtx();
} // namespace contexts
} // namespace compression
} // namespace folly
......@@ -96,6 +96,10 @@ class CompressionCoreLocalContextPool {
return Ref(ptr, get_deleter());
}
Ref getNull() {
return Ref(nullptr, get_deleter());
}
private:
ReturnToPoolDeleter get_deleter() {
return ReturnToPoolDeleter(this);
......
......@@ -19,6 +19,7 @@
#include <folly/portability/GTest.h>
#include <folly/compression/CompressionContextPool.h>
#include <folly/compression/CompressionContextPoolSingletons.h>
#include <folly/compression/CompressionCoreLocalContextPool.h>
using namespace testing;
......@@ -258,5 +259,15 @@ TEST_F(CompressionCoreLocalContextPoolTest, testMultithread) {
EXPECT_LE(numFoos.load(), numThreads);
}
TEST(CompressionContextPoolSingletonsTest, testSingletons) {
EXPECT_NE(contexts::getZSTD_CCtx(), nullptr);
EXPECT_NE(contexts::getZSTD_DCtx(), nullptr);
}
TEST(CompressionContextPoolSingletonsTest, testSingletonsNull) {
EXPECT_EQ(contexts::getNULL_ZSTD_CCtx(), nullptr);
EXPECT_EQ(contexts::getNULL_ZSTD_DCtx(), nullptr);
}
} // namespace compression
} // namespace folly
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