Skip to content

Commit d6925c5

Browse files
author
tobo
committed
add zstd fast levels from 1 to 7
zstd supports negative compression levels (`--fast=XXX` param in zstd CLI), so add them here also commit_hash:ed7231a6f341cd120dfea0d4b0b481962df835ab
1 parent 38cb797 commit d6925c5

File tree

1 file changed

+18
-11
lines changed
  • library/cpp/blockcodecs/codecs/zstd

1 file changed

+18
-11
lines changed

library/cpp/blockcodecs/codecs/zstd/zstd.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,38 @@
55
#define ZSTD_STATIC_LINKING_ONLY
66
#include <contrib/libs/zstd/include/zstd.h>
77

8+
#include <utility>
9+
810
using namespace NBlockCodecs;
911

1012
namespace {
1113
struct TZStd08Codec: public TAddLengthCodec<TZStd08Codec> {
12-
inline TZStd08Codec(unsigned level)
14+
TZStd08Codec(int level, TString name)
1315
: Level(level)
14-
, MyName(TStringBuf("zstd08_") + ToString(Level))
16+
, MyName(std::move(name))
1517
{
1618
}
1719

18-
static inline size_t CheckError(size_t ret, const char* what) {
19-
if (ZSTD_isError(ret)) {
20+
static size_t CheckError(size_t ret, const char* what) {
21+
if (Y_UNLIKELY(ZSTD_isError(ret))) {
2022
ythrow yexception() << what << TStringBuf(" zstd error: ") << ZSTD_getErrorName(ret);
2123
}
2224

2325
return ret;
2426
}
2527

26-
static inline size_t DoMaxCompressedLength(size_t l) noexcept {
28+
static size_t DoMaxCompressedLength(size_t l) noexcept {
2729
return ZSTD_compressBound(l);
2830
}
2931

30-
inline size_t DoCompress(const TData& in, void* out) const {
32+
size_t DoCompress(const TData& in, void* out) const {
3133
return CheckError(ZSTD_compress(out, DoMaxCompressedLength(in.size()), in.data(), in.size(), Level), "compress");
3234
}
3335

34-
inline void DoDecompress(const TData& in, void* out, size_t dsize) const {
36+
static void DoDecompress(const TData& in, void* out, size_t dsize) {
3537
const size_t res = CheckError(ZSTD_decompress(out, dsize, in.data(), in.size()), "decompress");
3638

37-
if (res != dsize) {
39+
if (Y_UNLIKELY(res != dsize)) {
3840
ythrow TDecompressError(dsize, res);
3941
}
4042
}
@@ -43,15 +45,20 @@ namespace {
4345
return MyName;
4446
}
4547

46-
const unsigned Level;
48+
const int Level;
4749
const TString MyName;
4850
};
4951

5052
struct TZStd08Registrar {
5153
TZStd08Registrar() {
5254
for (int i = 1; i <= ZSTD_maxCLevel(); ++i) {
53-
RegisterCodec(MakeHolder<TZStd08Codec>(i));
54-
RegisterAlias("zstd_" + ToString(i), "zstd08_" + ToString(i));
55+
const TString name = "zstd08_"sv + ToString(i);
56+
RegisterCodec(MakeHolder<TZStd08Codec>(i, name));
57+
RegisterAlias("zstd_"sv + ToString(i), name);
58+
}
59+
60+
for (int i = 1; i <= 7; ++i) {
61+
RegisterCodec(MakeHolder<TZStd08Codec>(-i, "zstd_fast_"sv + ToString(i)));
5562
}
5663
}
5764
};

0 commit comments

Comments
 (0)