Skip to content

Commit 9ca7f96

Browse files
martin.foo838claude
andcommitted
[improvement](be) Reuse one zstd context and buffer per SNII index source
### What problem does this PR solve? Issue Number: None Related PR: #69649 Problem Summary: Every zstd .prx window a query reads was decompressed with ZSTD_decompress, which creates and frees a decompression context (about 96 KB) per call, into a zero-filled vector that is also allocated per call. Timed per window on a 10M-row AgentLogs load, the decompression took 6.8 us with a context per call and 4.8 us with a reused one. ZstdDecompressor keeps one context, created at the first frame, and one output buffer that only grows and is not zero-filled (decompression writes every byte before it is read). SniiIndexSource owns one, and the cursors it opens pass it to the .prx decoder through PrxDecodeContext; the cursors of a source decode on its thread one window at a time, and the decoded plain payload is consumed before the next window is read. A source lives for one query on one segment, so a query holds at most one context and one buffer per segment it reads, created only if it decodes a zstd window; before, each window created and released them. Compaction decodes through an allocation gate, which keeps the old path, as does a cursor given no decompressor. Hot phrase queries on data whose position windows are mostly zstd (10M rows, interleaved A/B in one process, on top of the plain-decode change): 0.95-0.96x for the two most position-heavy queries, the others within noise. ### Release note None ### Check List (For Author) - Test: Unit Test (SniiZstdDecompressor: frames of different sizes through one decompressor, corruption reported and the next frame decoded; PrxPodTest: random windows decoded through one shared decompressor) - Behavior changed: No - Does this need documentation: No Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 299715b commit 9ca7f96

10 files changed

Lines changed: 146 additions & 8 deletions

File tree

‎be/src/storage/index/inverted/snii/encoding/zstd_codec.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,36 @@ Status zstd_decompress(Slice input, size_t expected_uncomp_len, std::vector<uint
4949
return Status::OK();
5050
}
5151

52+
ZstdDecompressor::~ZstdDecompressor() {
53+
ZSTD_freeDCtx(static_cast<ZSTD_DCtx*>(_context));
54+
}
55+
56+
Status ZstdDecompressor::decompress(Slice input, size_t expected_uncomp_len, Slice* out) {
57+
if (_context == nullptr) {
58+
_context = ZSTD_createDCtx();
59+
if (_context == nullptr) {
60+
return Status::Error<ErrorCode::MEM_ALLOC_FAILED, false>(
61+
"zstd decompress: cannot create a context");
62+
}
63+
}
64+
if (expected_uncomp_len > _capacity) {
65+
// Every byte is written by the decompression before it is read, so the buffer is not
66+
// zero-filled.
67+
_buffer.reset(new uint8_t[expected_uncomp_len]);
68+
_capacity = expected_uncomp_len;
69+
}
70+
const size_t n = ZSTD_decompressDCtx(static_cast<ZSTD_DCtx*>(_context), _buffer.get(),
71+
expected_uncomp_len, input.data(), input.size());
72+
if (ZSTD_isError(n)) {
73+
return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>(
74+
std::string("zstd decompress: ") + ZSTD_getErrorName(n));
75+
}
76+
if (n != expected_uncomp_len) {
77+
return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>(
78+
"zstd decompressed length mismatch");
79+
}
80+
*out = Slice(_buffer.get(), expected_uncomp_len);
81+
return Status::OK();
82+
}
83+
5284
} // namespace doris::snii

‎be/src/storage/index/inverted/snii/encoding/zstd_codec.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <cstddef>
2121
#include <cstdint>
22+
#include <memory>
2223
#include <vector>
2324

2425
#include "common/status.h"
@@ -30,4 +31,23 @@ namespace doris::snii {
3031
Status zstd_compress(Slice input, int level, std::vector<uint8_t>* out);
3132
Status zstd_decompress(Slice input, size_t expected_uncomp_len, std::vector<uint8_t>* out);
3233

34+
// Decompresses many small frames in a row -- one per .prx window of a posting -- with one
35+
// decompression context and one output buffer, instead of a context created and a zeroed vector
36+
// allocated for every frame. The view returned by decompress() is valid until the next call.
37+
// Not thread-safe; owned by a single reader.
38+
class ZstdDecompressor {
39+
public:
40+
ZstdDecompressor() = default;
41+
~ZstdDecompressor();
42+
ZstdDecompressor(const ZstdDecompressor&) = delete;
43+
ZstdDecompressor& operator=(const ZstdDecompressor&) = delete;
44+
45+
Status decompress(Slice input, size_t expected_uncomp_len, Slice* out);
46+
47+
private:
48+
void* _context = nullptr; // ZSTD_DCtx, created on the first frame
49+
std::unique_ptr<uint8_t[]> _buffer;
50+
size_t _capacity = 0;
51+
};
52+
3353
} // namespace doris::snii

‎be/src/storage/index/inverted/snii/format/prx_decode_stats.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
#include "common/status.h"
2525

26+
namespace doris::snii {
27+
class ZstdDecompressor;
28+
} // namespace doris::snii
29+
2630
namespace doris::snii::format {
2731

2832
// Optional allocation seam for full CSR decode callers that must gate every
@@ -73,6 +77,9 @@ struct PrxDecodeContext {
7377
PrxDecodeStats* stats = nullptr;
7478
PrxDecodedShape* shape = nullptr;
7579
PrxCsrAllocationGate* allocation_gate = nullptr;
80+
// Optional: a reader that decodes many windows in a row passes its own decompressor, so the
81+
// zstd windows reuse one context and one buffer. An allocation gate takes precedence.
82+
ZstdDecompressor* zstd = nullptr;
7683
};
7784

7885
} // namespace doris::snii::format

‎be/src/storage/index/inverted/snii/format/prx_pod.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ Status decode_csr_frame(const PrxFrameView& encoded, std::span<const uint32_t> d
11101110
bool decode_all_docs, bool all_docs_selected,
11111111
std::vector<uint32_t>* pos_flat, std::vector<uint32_t>* pos_off,
11121112
PrxDecodeStats* stats, PrxDecodedShape* shape,
1113-
PrxCsrAllocationGate* allocation_gate) {
1113+
PrxCsrAllocationGate* allocation_gate, ZstdDecompressor* zstd) {
11141114
if (!decode_all_docs && allocation_gate != nullptr) {
11151115
return Status::Error<ErrorCode::INVALID_ARGUMENT, false>(
11161116
"prx: selective decode cannot use an allocation gate");
@@ -1125,7 +1125,9 @@ Status decode_csr_frame(const PrxFrameView& encoded, std::span<const uint32_t> d
11251125

11261126
std::vector<uint8_t> local_decompressed;
11271127
Slice plain = encoded.payload;
1128-
if (encoded.codec == PrxCodec::kZstd) {
1128+
if (encoded.codec == PrxCodec::kZstd && allocation_gate == nullptr && zstd != nullptr) {
1129+
RETURN_IF_ERROR(zstd->decompress(encoded.payload, encoded.uncompressed_length, &plain));
1130+
} else if (encoded.codec == PrxCodec::kZstd) {
11291131
std::vector<uint8_t>* decompressed = &local_decompressed;
11301132
if (allocation_gate != nullptr) {
11311133
RETURN_IF_ERROR(allocation_gate->reserve_decompression(encoded.uncompressed_length,
@@ -1200,7 +1202,8 @@ Status read_prx_window_csr_impl(ByteSource* source, std::span<const uint32_t> do
12001202
RETURN_IF_ERROR(decode_csr_frame(encoded, doc_ordinals, decode_all_docs, all_docs_selected,
12011203
pos_flat, pos_off, stats,
12021204
context == nullptr ? nullptr : context->shape,
1203-
context == nullptr ? nullptr : context->allocation_gate));
1205+
context == nullptr ? nullptr : context->allocation_gate,
1206+
context == nullptr ? nullptr : context->zstd));
12041207
if (collect_stats) {
12051208
// Exclude the logical-selection scan from decode timing.
12061209
frame_stats.decode_ns = elapsed_ns(decode_start);

‎be/src/storage/index/inverted/snii/reader/snii_index_source.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Status SniiIndexSource::_cursor(Term& term, bool positions, bool scoring, SniiRe
124124
}
125125
*out = std::make_unique<SniiPostingsCursor>(_idx, term.hit.entry, term.hit.frq_base,
126126
term.hit.prx_base, positions, scoring, norms, wave,
127-
_prx_stats);
127+
_prx_stats, &_zstd);
128128
if (auto prelude = term.prelude.lock()) {
129129
(*out)->set_prelude(std::move(prelude));
130130
}

‎be/src/storage/index/inverted/snii/reader/snii_index_source.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "common/status.h"
3030
#include "storage/index/inverted/query/spi/index_source.h"
31+
#include "storage/index/inverted/snii/encoding/zstd_codec.h"
3132
#include "storage/index/inverted/snii/format/frq_prelude.h"
3233
#include "storage/index/inverted/snii/format/norms_pod.h"
3334
#include "storage/index/inverted/snii/reader/dict_block_cache.h"
@@ -89,6 +90,9 @@ class SniiIndexSource final : public index_query::IndexSource {
8990

9091
const LogicalIndexReader& _idx;
9192
format::PrxDecodeStats* _prx_stats;
93+
// The cursors decode on the source's thread one window at a time, so they share one zstd
94+
// context and buffer instead of creating them for every window.
95+
ZstdDecompressor _zstd;
9296
SniiReadWave _wave;
9397
std::unordered_map<std::string, Term> _terms;
9498
DictBlockCache _scoring_dict_cache;

‎be/src/storage/index/inverted/snii/reader/snii_postings_cursor.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ uint64_t SniiReadWave::held_bytes() const {
9898
SniiPostingsCursor::SniiPostingsCursor(const LogicalIndexReader& idx, format::DictEntry entry,
9999
uint64_t frq_base, uint64_t prx_base, bool positions,
100100
bool scoring, const format::NormsPodReader* norms,
101-
SniiReadWave* wave, format::PrxDecodeStats* prx_stats)
101+
SniiReadWave* wave, format::PrxDecodeStats* prx_stats,
102+
ZstdDecompressor* zstd)
102103
: _idx(idx),
103104
_entry(std::move(entry)),
104105
_frq_base(frq_base),
@@ -108,6 +109,7 @@ SniiPostingsCursor::SniiPostingsCursor(const LogicalIndexReader& idx, format::Di
108109
_norms(norms),
109110
_wave(wave),
110111
_prx_stats(prx_stats),
112+
_zstd(zstd),
111113
_kind(_posting_kind(_entry)) {
112114
if (_wave == nullptr) {
113115
_own_wave = std::make_unique<SniiReadWave>(_idx.reader());
@@ -533,7 +535,7 @@ Status SniiPostingsCursor::_ensure_positions(std::span<const uint32_t> ordinals)
533535
const uint32_t window = _current_window;
534536
RETURN_IF_ERROR(_ensure_prx(window));
535537
ByteSource source(_windows[window].prx);
536-
format::PrxDecodeContext context {.stats = _prx_stats};
538+
format::PrxDecodeContext context {.stats = _prx_stats, .zstd = _zstd};
537539
if (ordinals.empty() || ordinals.size() == _doc_count) {
538540
RETURN_IF_ERROR(format::read_prx_window_csr(&source, &_pos_flat, &_pos_off, &context));
539541
} else {
@@ -795,7 +797,7 @@ Status SniiPostingsCursor::_decode_selected(std::span<const uint32_t> ordinals,
795797
RETURN_IF_ERROR(_ensure_prx(_current_window));
796798
ByteSource source(_windows[_current_window].prx);
797799
format::PrxDecodedShape shape;
798-
format::PrxDecodeContext context {.stats = _prx_stats, .shape = &shape};
800+
format::PrxDecodeContext context {.stats = _prx_stats, .shape = &shape, .zstd = _zstd};
799801
RETURN_IF_ERROR(format::read_prx_window_csr_selective(&source, ordinals, &_selected_flat,
800802
&_selected_off, &context));
801803
if (!source.eof()) {

‎be/src/storage/index/inverted/snii/reader/snii_postings_cursor.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class SniiPostingsCursor final : public index_query::PostingsCursor,
8585
SniiPostingsCursor(const LogicalIndexReader& idx, format::DictEntry entry, uint64_t frq_base,
8686
uint64_t prx_base, bool positions, bool scoring,
8787
const format::NormsPodReader* norms, SniiReadWave* wave = nullptr,
88-
format::PrxDecodeStats* prx_stats = nullptr);
88+
format::PrxDecodeStats* prx_stats = nullptr,
89+
ZstdDecompressor* zstd = nullptr);
8990
~SniiPostingsCursor() override;
9091

9192
// A prelude the caller already read; call before the first read.
@@ -187,6 +188,8 @@ class SniiPostingsCursor final : public index_query::PostingsCursor,
187188
const format::NormsPodReader* _norms;
188189
SniiReadWave* _wave;
189190
format::PrxDecodeStats* _prx_stats;
191+
// Decompresses the zstd .prx windows when given; each window decompresses on its own otherwise.
192+
ZstdDecompressor* _zstd;
190193
Kind _kind;
191194
// The wave of a cursor given none.
192195
std::unique_ptr<SniiReadWave> _own_wave;

‎be/test/storage/index/inverted/snii/encoding/zstd_codec_test.cpp‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <gtest/gtest.h>
2121

22+
#include <algorithm>
2223
#include <vector>
2324

2425
#include "common/status.h"
@@ -49,3 +50,51 @@ TEST(SniiZstd, EmptyInput) {
4950
ASSERT_TRUE(zstd_decompress(Slice(comp), 0, &decomp).ok());
5051
EXPECT_TRUE(decomp.empty());
5152
}
53+
54+
namespace {
55+
std::vector<uint8_t> pattern(size_t n, int seed) {
56+
std::vector<uint8_t> v(n);
57+
for (size_t i = 0; i < n; ++i) {
58+
v[i] = static_cast<uint8_t>((i * 31 + seed) % 13);
59+
}
60+
return v;
61+
}
62+
} // namespace
63+
64+
// One decompressor serves frames that grow and shrink; every output equals its input.
65+
TEST(SniiZstdDecompressor, ReusedAcrossFramesOfDifferentSizes) {
66+
ZstdDecompressor decompressor;
67+
for (size_t n : {100UL, 5000UL, 300UL, 0UL, 20000UL, 7UL}) {
68+
const std::vector<uint8_t> in = pattern(n, static_cast<int>(n));
69+
std::vector<uint8_t> comp;
70+
ASSERT_TRUE(zstd_compress(Slice(in), 3, &comp).ok());
71+
Slice out;
72+
ASSERT_TRUE(decompressor.decompress(Slice(comp), in.size(), &out).ok()) << n;
73+
ASSERT_EQ(out.size(), in.size());
74+
EXPECT_TRUE(std::equal(in.begin(), in.end(), out.data())) << n;
75+
}
76+
}
77+
78+
// The errors are the ones zstd_decompress reports, and a failed frame does not spoil the next one.
79+
TEST(SniiZstdDecompressor, ReportsCorruptionAndRecovers) {
80+
const std::vector<uint8_t> in = pattern(1000, 3);
81+
std::vector<uint8_t> comp;
82+
ASSERT_TRUE(zstd_compress(Slice(in), 3, &comp).ok());
83+
ZstdDecompressor decompressor;
84+
Slice out;
85+
86+
const doris::Status wrong_length = decompressor.decompress(Slice(comp), in.size() - 1, &out);
87+
EXPECT_TRUE(wrong_length.is<doris::ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>())
88+
<< wrong_length.to_string();
89+
90+
std::vector<uint8_t> garbage = comp;
91+
garbage[garbage.size() / 2] ^= 0xFF;
92+
garbage.resize(garbage.size() - 3);
93+
const doris::Status corrupt = decompressor.decompress(Slice(garbage), in.size(), &out);
94+
EXPECT_TRUE(corrupt.is<doris::ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>())
95+
<< corrupt.to_string();
96+
97+
ASSERT_TRUE(decompressor.decompress(Slice(comp), in.size(), &out).ok());
98+
ASSERT_EQ(out.size(), in.size());
99+
EXPECT_TRUE(std::equal(in.begin(), in.end(), out.data()));
100+
}

‎be/test/storage/index/inverted/snii/format/prx_pod_test.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ TEST(PrxPodTest, CodecSelectionMatrixMatchesCsrOracleAtLowAndHighTf) {
551551
// selections, and every result must equal the positions the window was built from.
552552
TEST(PrxPodTest, SelectivePlainDecodeMatchesInputForRandomWindows) {
553553
std::mt19937 rng(20261028);
554+
// A reader-owned decompressor, shared by every window below as a cursor shares it.
555+
doris::snii::ZstdDecompressor shared_zstd;
554556
for (int round = 0; round < 300; ++round) {
555557
const uint32_t doc_count = 1 + rng() % 400;
556558
const uint32_t style = rng() % 4;
@@ -597,6 +599,22 @@ TEST(PrxPodTest, SelectivePlainDecodeMatchesInputForRandomWindows) {
597599
ASSERT_TRUE(read_prx_window_csr_selective(&source, ordinals, &flat, &offsets).ok());
598600
EXPECT_EQ(flat, expected_flat);
599601
EXPECT_EQ(offsets, expected_offsets);
602+
603+
// The same window through the shared decompressor, selectively and in full.
604+
PrxDecodeContext context {.zstd = &shared_zstd};
605+
ByteSource reused_source((Slice(encoded)));
606+
ASSERT_TRUE(read_prx_window_csr_selective(&reused_source, ordinals, &flat, &offsets,
607+
&context)
608+
.ok());
609+
EXPECT_EQ(flat, expected_flat);
610+
EXPECT_EQ(offsets, expected_offsets);
611+
std::vector<uint32_t> all(doc_count);
612+
std::iota(all.begin(), all.end(), 0U);
613+
ExpectedSelectiveCsr(docs, all, &expected_flat, &expected_offsets);
614+
ByteSource full_source((Slice(encoded)));
615+
ASSERT_TRUE(read_prx_window_csr(&full_source, &flat, &offsets, &context).ok());
616+
EXPECT_EQ(flat, expected_flat);
617+
EXPECT_EQ(offsets, expected_offsets);
600618
}
601619
}
602620
}

0 commit comments

Comments
 (0)