diff --git a/folly/compression/Compression.cpp b/folly/compression/Compression.cpp index 1e328cc94e5c43689a91dd43b95b5d55dc218fad..ab511f2d8972f8f2d296e035963fa25fc30f1efc 100644 --- a/folly/compression/Compression.cpp +++ b/folly/compression/Compression.cpp @@ -68,76 +68,8 @@ using folly::io::compression::detail::prefixToStringLE; namespace folly { namespace io { -Codec::Codec( - CodecType type, - Optional<int> level, - StringPiece name, - bool counters) - : type_(type) { - if (counters) { - bytesBeforeCompression_ = {type, - name, - level, - CompressionCounterKey::BYTES_BEFORE_COMPRESSION, - CompressionCounterType::SUM}; - bytesAfterCompression_ = {type, - name, - level, - CompressionCounterKey::BYTES_AFTER_COMPRESSION, - CompressionCounterType::SUM}; - bytesBeforeDecompression_ = { - type, - name, - level, - CompressionCounterKey::BYTES_BEFORE_DECOMPRESSION, - CompressionCounterType::SUM}; - bytesAfterDecompression_ = { - type, - name, - level, - CompressionCounterKey::BYTES_AFTER_DECOMPRESSION, - CompressionCounterType::SUM}; - compressions_ = {type, - name, - level, - CompressionCounterKey::COMPRESSIONS, - CompressionCounterType::SUM}; - decompressions_ = {type, - name, - level, - CompressionCounterKey::DECOMPRESSIONS, - CompressionCounterType::SUM}; - compressionMilliseconds_ = {type, - name, - level, - CompressionCounterKey::COMPRESSION_MILLISECONDS, - CompressionCounterType::SUM}; - decompressionMilliseconds_ = { - type, - name, - level, - CompressionCounterKey::DECOMPRESSION_MILLISECONDS, - CompressionCounterType::SUM}; - } -} - -namespace { -constexpr uint32_t kLoggingRate = 50; - -class Timer { - public: - explicit Timer(folly::detail::CompressionCounter& counter) - : counter_(&counter) {} - - ~Timer() { - *counter_ += timer_.elapsed().count(); - } - - private: - folly::detail::CompressionCounter* counter_; - stop_watch<std::chrono::milliseconds> timer_; -}; -} // namespace +Codec::Codec(CodecType type, Optional<int> /* level */, StringPiece /* name */) + : type_(type) {} // Ensure consistent behavior in the nullptr case std::unique_ptr<IOBuf> Codec::compress(const IOBuf* data) { @@ -148,16 +80,7 @@ std::unique_ptr<IOBuf> Codec::compress(const IOBuf* data) { if (len > maxUncompressedLength()) { throw std::runtime_error("Codec: uncompressed length too large"); } - bool const logging = folly::Random::oneIn(kLoggingRate); - folly::Optional<Timer> const timer = - logging ? Timer(compressionMilliseconds_) : folly::Optional<Timer>(); - auto result = doCompress(data); - if (logging) { - compressions_++; - bytesBeforeCompression_ += len; - bytesAfterCompression_ += result->computeChainDataLength(); - } - return result; + return doCompress(data); } std::string Codec::compress(const StringPiece data) { @@ -165,16 +88,7 @@ std::string Codec::compress(const StringPiece data) { if (len > maxUncompressedLength()) { throw std::runtime_error("Codec: uncompressed length too large"); } - bool const logging = folly::Random::oneIn(kLoggingRate); - folly::Optional<Timer> const timer = - logging ? Timer(compressionMilliseconds_) : folly::Optional<Timer>(); - auto result = doCompressString(data); - if (logging) { - compressions_++; - bytesBeforeCompression_ += len; - bytesAfterCompression_ += result.size(); - } - return result; + return doCompressString(data); } std::unique_ptr<IOBuf> Codec::uncompress( @@ -198,16 +112,7 @@ std::unique_ptr<IOBuf> Codec::uncompress( return IOBuf::create(0); } - bool const logging = folly::Random::oneIn(kLoggingRate); - folly::Optional<Timer> const timer = - logging ? Timer(decompressionMilliseconds_) : folly::Optional<Timer>(); - auto result = doUncompress(data, uncompressedLength); - if (logging) { - decompressions_++; - bytesBeforeDecompression_ += data->computeChainDataLength(); - bytesAfterDecompression_ += result->computeChainDataLength(); - } - return result; + return doUncompress(data, uncompressedLength); } std::string Codec::uncompress( @@ -228,16 +133,7 @@ std::string Codec::uncompress( return ""; } - bool const logging = folly::Random::oneIn(kLoggingRate); - folly::Optional<Timer> const timer = - logging ? Timer(decompressionMilliseconds_) : folly::Optional<Timer>(); - auto result = doUncompressString(data, uncompressedLength); - if (logging) { - decompressions_++; - bytesBeforeDecompression_ += data.size(); - bytesAfterDecompression_ += result.size(); - } - return result; + return doUncompressString(data, uncompressedLength); } bool Codec::needsUncompressedLength() const { diff --git a/folly/compression/Compression.h b/folly/compression/Compression.h index 65e07d0b7ad13dc6db5799788d32fbe005291dca..6488a271a7d2b256ef824577cf01b1dbeb665c7b 100644 --- a/folly/compression/Compression.h +++ b/folly/compression/Compression.h @@ -24,7 +24,6 @@ #include <folly/Optional.h> #include <folly/Range.h> -#include <folly/compression/Counters.h> #include <folly/io/IOBuf.h> /** @@ -221,8 +220,7 @@ class Codec { Codec( CodecType type, folly::Optional<int> level = folly::none, - folly::StringPiece name = {}, - bool counters = true); + folly::StringPiece name = {}); public: /** @@ -275,14 +273,6 @@ class Codec { folly::Optional<uint64_t> uncompressedLength) const; CodecType type_; - folly::detail::CompressionCounter bytesBeforeCompression_; - folly::detail::CompressionCounter bytesAfterCompression_; - folly::detail::CompressionCounter bytesBeforeDecompression_; - folly::detail::CompressionCounter bytesAfterDecompression_; - folly::detail::CompressionCounter compressions_; - folly::detail::CompressionCounter decompressions_; - folly::detail::CompressionCounter compressionMilliseconds_; - folly::detail::CompressionCounter decompressionMilliseconds_; }; class StreamCodec : public Codec { @@ -406,9 +396,8 @@ class StreamCodec : public Codec { StreamCodec( CodecType type, folly::Optional<int> level = folly::none, - folly::StringPiece name = {}, - bool counters = true) - : Codec(type, std::move(level), name, counters) {} + folly::StringPiece name = {}) + : Codec(type, std::move(level), name) {} // Returns the uncompressed length last passed to resetStream() or none if it // hasn't been called yet. diff --git a/folly/compression/Counters.cpp b/folly/compression/Counters.cpp deleted file mode 100644 index c1c2d66888579d34c2cc782186bb07bc98738c49..0000000000000000000000000000000000000000 --- a/folly/compression/Counters.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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/Counters.h> -#include <folly/Portability.h> - -namespace folly { -FOLLY_ATTR_WEAK folly::Function<void(double)> makeCompressionCounterHandler( - folly::io::CodecType, - folly::StringPiece, - folly::Optional<int>, - CompressionCounterKey, - CompressionCounterType) { - return {}; -} -} // namespace folly diff --git a/folly/compression/Counters.h b/folly/compression/Counters.h deleted file mode 100644 index 7bbb7d8398ddd5d9693eb07cd1cf9ec434602ca2..0000000000000000000000000000000000000000 --- a/folly/compression/Counters.h +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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 <cassert> -#include <string> - -#include <folly/Function.h> -#include <folly/Optional.h> -#include <folly/Range.h> - -namespace folly { -namespace io { -enum class CodecType; -} // namespace io - -enum class CompressionCounterKey { - BYTES_BEFORE_COMPRESSION = 0, - BYTES_AFTER_COMPRESSION = 1, - BYTES_BEFORE_DECOMPRESSION = 2, - BYTES_AFTER_DECOMPRESSION = 3, - COMPRESSIONS = 4, - DECOMPRESSIONS = 5, - COMPRESSION_MILLISECONDS = 6, - DECOMPRESSION_MILLISECONDS = 7, -}; - -enum class CompressionCounterType { - AVG = 0, - SUM = 1, -}; - -/** - * This functions is an extension point when FOLLY_HAVE_WEAK_SYMBOLS is true. - * There is a default no-op implementation provided which can be overrided by - * linking in a library which provides its own definition. - * - * @param codecType The type of the codec for this counter. - * @param codecName The name of the codec for this counter. If the codecName - * is empty it should be defaulted using the codecType. - * @param level Optionally the level used to construct the codec. - * @param key The key of the counter. - * @param counterType The type of the counter. - * @returns A function to increment the counter for the given key and - * type. It may be an empty folly::Function. - */ -folly::Function<void(double)> makeCompressionCounterHandler( - folly::io::CodecType codecType, - folly::StringPiece codecName, - folly::Optional<int> level, - CompressionCounterKey key, - CompressionCounterType counterType); - -namespace detail { - -/// Wrapper around the makeCompressionCounterHandler() extension point. -class CompressionCounter { - public: - CompressionCounter() : initialized_(true) {} - CompressionCounter( - folly::io::CodecType codecType, - folly::StringPiece codecName, - folly::Optional<int> level, - CompressionCounterKey key, - CompressionCounterType counterType) - : initialized_(false) { - initialize_ = [=]() { - return makeCompressionCounterHandler( - codecType, codecName, level, key, counterType); - }; - assert(!initialize_.heapAllocatedMemory()); - } - - void operator+=(double sum) { - performLazyInit(); - if (increment_) { - increment_(sum); - } - } - - void operator++() { - *this += 1.0; - } - - void operator++(int) { - *this += 1.0; - } - - bool hasImplementation() { - performLazyInit(); - return static_cast<bool>(increment_); - } - - private: - void performLazyInit() { - if (!initialized_) { - initialized_ = true; - increment_ = initialize_(); - initialize_ = {}; - } - } - - bool initialized_; - folly::Function<folly::Function<void(double)>()> initialize_; - folly::Function<void(double)> increment_; -}; - -} // namespace detail -} // namespace folly diff --git a/folly/compression/test/CompressionTest.cpp b/folly/compression/test/CompressionTest.cpp index 7484ec5f3c4027539e9f7e57dbacd8c564871f94..c39831523ab3b0278fc20b53d80a2cf158815231 100644 --- a/folly/compression/test/CompressionTest.cpp +++ b/folly/compression/test/CompressionTest.cpp @@ -464,35 +464,6 @@ static bool codecHasFlush(CodecType type) { return type != CodecType::BZIP2; } -namespace { -class NoCountersCodec : public Codec { - public: - NoCountersCodec() - : Codec(CodecType::NO_COMPRESSION, {}, {}, /* counters */ false) {} - - private: - uint64_t doMaxCompressedLength(uint64_t uncompressedLength) const override { - return uncompressedLength; - } - - std::unique_ptr<IOBuf> doCompress(const IOBuf* buf) override { - return buf->clone(); - } - - std::unique_ptr<IOBuf> doUncompress(const IOBuf* buf, Optional<uint64_t>) - override { - return buf->clone(); - } -}; -} // namespace - -TEST(CodecTest, NoCounters) { - NoCountersCodec codec; - for (size_t i = 0; i < 1000; ++i) { - EXPECT_EQ("hello", codec.uncompress(codec.compress("hello"))); - } -} - class StreamingUnitTest : public testing::TestWithParam<CodecType> { protected: void SetUp() override { diff --git a/folly/compression/test/CountersTest.cpp b/folly/compression/test/CountersTest.cpp deleted file mode 100644 index 10158874168a17922325f30d5cfa1bc8b9c0a8f3..0000000000000000000000000000000000000000 --- a/folly/compression/test/CountersTest.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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/Counters.h> -#include <folly/compression/Compression.h> -#include <folly/portability/GTest.h> - -using ::folly::CompressionCounterType; -using ::folly::detail::CompressionCounter; - -namespace { -constexpr auto kCodecType = folly::io::CodecType::USER_DEFINED; -constexpr folly::StringPiece kCodecName = "test"; -constexpr auto kKey = folly::CompressionCounterKey::BYTES_AFTER_COMPRESSION; -} // namespace - -TEST(FollyCountersTest, HasImplementation) { - CompressionCounter counter( - kCodecType, kCodecName, folly::none, kKey, CompressionCounterType::SUM); - EXPECT_FALSE(counter.hasImplementation()); -} - -TEST(FollyCountersTest, SumWorks) { - CompressionCounter counter( - kCodecType, kCodecName, folly::none, kKey, CompressionCounterType::SUM); - for (int i = 0; i < 100; ++i) { - ++counter; - counter++; - } -} - -TEST(FollyCountersTest, AvgWorks) { - CompressionCounter counter( - kCodecType, kCodecName, folly::none, kKey, CompressionCounterType::AVG); - for (int i = 0; i < 100; ++i) { - counter += 5; - } -} - -TEST(FollyCountersTest, DefaultConstruction) { - CompressionCounter counter; - ++counter; -}