From d893bd239fde881a1fb2546be1616cb8f71f570b Mon Sep 17 00:00:00 2001 From: ivanmorozov333 Date: Wed, 25 Dec 2024 14:01:03 +0300 Subject: [PATCH 1/7] bloom ngramms speed up --- .../engines/storage/indexes/bloom/checker.h | 22 ++++ .../storage/indexes/bloom_ngramm/meta.cpp | 121 ++++++++++++------ .../storage/indexes/bloom_ngramm/meta.h | 20 --- 3 files changed, 104 insertions(+), 59 deletions(-) diff --git a/ydb/core/tx/columnshard/engines/storage/indexes/bloom/checker.h b/ydb/core/tx/columnshard/engines/storage/indexes/bloom/checker.h index 740af9f1720d..23d46e21b557 100644 --- a/ydb/core/tx/columnshard/engines/storage/indexes/bloom/checker.h +++ b/ydb/core/tx/columnshard/engines/storage/indexes/bloom/checker.h @@ -11,6 +11,28 @@ class TFixStringBitsStorage { : Data(data) {} + TFixStringBitsStorage(const std::vector& bitsVector) + : TFixStringBitsStorage(bitsVector.size()) { + ui32 byteIdx = 0; + ui8 byteCurrent = 0; + ui8 shiftCurrent = 0; + for (ui32 i = 0; i < bitsVector.size(); ++i) { + if (i && i % 8 == 0) { + Data[byteIdx] = (char)byteCurrent; + byteCurrent = 0; + shiftCurrent = 1; + ++byteIdx; + } + if (bitsVector[i]) { + byteCurrent += shiftCurrent; + } + shiftCurrent = (shiftCurrent << 1); + } + if (byteCurrent) { + Data[byteIdx] = (char)byteCurrent; + } + } + ui32 GetSizeBits() const { return Data.size() * 8; } diff --git a/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp b/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp index af139065b9cf..9a4157421a33 100644 --- a/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp +++ b/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp @@ -15,8 +15,70 @@ namespace NKikimr::NOlap::NIndexes::NBloomNGramm { class TNGrammBuilder { private: - NArrow::NHash::NXX64::TStreamStringHashCalcer HashCalcer; TBuffer Zeros; + const ui32 HashesCount; + + static const ui64 HashesConstructorP = 9223372036854775783; + static const ui64 HashesConstructorA = 1; + + template + class THashesBuilder { + public: + template + static void Build(const ui64 originalHash, const TActor& actor) { + actor((HashesConstructorA * originalHash + HashIdx) % HashesConstructorP); + } + }; + + template <> + class THashesBuilder<0> { + public: + template + static void Build(const ui64 /*originalHash*/, const TActor& /*actor*/) { + } + }; + + template + void BuildHashesSet(const ui64 originalHash, const TActor& actor) const { + if (HashesCount == 1) { + THashesBuilder<1>::Build(originalHash, actor); + } else if (HashesCount == 2) { + THashesBuilder<2>::Build(originalHash, actor); + } else if (HashesCount == 3) { + THashesBuilder<3>::Build(originalHash, actor); + } else if (HashesCount == 4) { + THashesBuilder<4>::Build(originalHash, actor); + } else if (HashesCount == 5) { + THashesBuilder<5>::Build(originalHash, actor); + } else if (HashesCount == 6) { + THashesBuilder<6>::Build(originalHash, actor); + } else if (HashesCount == 7) { + THashesBuilder<7>::Build(originalHash, actor); + } else if (HashesCount == 8) { + THashesBuilder<8>::Build(originalHash, actor); + } else { + for (ui32 b = 1; b <= HashesCount; ++b) { + const ui64 hash = (HashesConstructorA * originalHash + b) % HashesConstructorP; + actor(hash); + } + } + } + + ui64 CalcHash(const char* data, const ui32 size) const { + if (size == 3) { + return ((ui64)data[0]) | (((ui64)data[1]) << 8) | (((ui64)data[2]) << 16); + } else if (size == 4) { + return *(ui32*)&data[0]; + } else { + uint64_t h = 2166136261; + for (size_t i = 0; i < size; i++) { + h = h ^ uint64_t(data[i]); + h = h * 16777619; + } + return h; + } + } + template void BuildNGramms(const char* data, const ui32 dataSize, const std::optional op, const ui32 nGrammSize, const TAction& pred) const { @@ -28,24 +90,26 @@ class TNGrammBuilder { if (fakeStart.size() < nGrammSize) { fakeStart.Append(Zeros.data(), nGrammSize - fakeStart.size()); } - pred(fakeStart.data()); + BuildHashesSet(CalcHash(fakeStart.data(), nGrammSize), pred); } } for (ui32 c = 0; c < dataSize; ++c) { if (c + nGrammSize <= dataSize) { - pred(data + c); + pred(CalcHash(data + c, nGrammSize)); } else if (!op || op == NRequest::TLikePart::EOperation::EndsWith) { TBuffer fakeStart; fakeStart.Append(data + c, dataSize - c); fakeStart.Append(Zeros.data(), nGrammSize - fakeStart.size()); - pred(fakeStart.data()); + BuildHashesSet(CalcHash(fakeStart.data(), nGrammSize), pred); } } } public: - TNGrammBuilder() - : HashCalcer(0) { + TNGrammBuilder(const ui32 hashesCount) + : HashesCount(hashesCount) + { + AFL_VERIFY((ui64)HashesCount < HashesConstructorP); Zeros.Fill('\0', 1024); } @@ -64,15 +128,7 @@ class TNGrammBuilder { } if constexpr (arrow::has_string_view()) { auto value = typedArray.GetView(row); - if (value.size() < nGrammSize) { - continue; - } - const auto pred = [&](const char* data) { - HashCalcer.Start(); - HashCalcer.Update((const ui8*)data, nGrammSize); - fillData(HashCalcer.Finish()); - }; - BuildNGramms(value.data(), value.size(), {}, nGrammSize, pred); + BuildNGramms(value.data(), value.size(), {}, nGrammSize, fillData); } else { AFL_VERIFY(false); } @@ -83,33 +139,23 @@ class TNGrammBuilder { template void FillNGrammHashes(const ui32 nGrammSize, const NRequest::TLikePart::EOperation op, const TString& userReq, const TFiller& fillData) { - const auto pred = [&](const char* value) { - HashCalcer.Start(); - HashCalcer.Update((const ui8*)value, nGrammSize); - fillData(HashCalcer.Finish()); - }; - BuildNGramms(userReq.data(), userReq.size(), op, nGrammSize, pred); + BuildNGramms(userReq.data(), userReq.size(), op, nGrammSize, fillData); } }; TString TIndexMeta::DoBuildIndexImpl(TChunkedBatchReader& reader) const { AFL_VERIFY(reader.GetColumnsCount() == 1)("count", reader.GetColumnsCount()); - TNGrammBuilder builder; + TNGrammBuilder builder(HashesCount); - TFixStringBitsStorage bits(FilterSizeBytes * 8); - - const auto pred = [&](const ui64 hash) { - const auto predSet = [&](const ui64 hashSecondary) { - bits.Set(true, hashSecondary % bits.GetSizeBits()); - }; - BuildHashesSet(hash, predSet); + std::vector bitsVector(FilterSizeBytes * 8, false); + const auto predSet = [&](const ui64 hashSecondary) { + bitsVector[hashSecondary % (FilterSizeBytes * 8)] = true; }; for (reader.Start(); reader.IsCorrect();) { - builder.FillNGrammHashes(NGrammSize, reader.begin()->GetCurrentChunk(), pred); + builder.FillNGrammHashes(NGrammSize, reader.begin()->GetCurrentChunk(), predSet); reader.ReadNext(reader.begin()->GetCurrentChunk()->length()); } - - return bits.GetData(); + return TFixStringBitsStorage(bitsVector).GetData(); } void TIndexMeta::DoFillIndexCheckers( @@ -133,16 +179,13 @@ void TIndexMeta::DoFillIndexCheckers( } std::set hashes; - const auto pred = [&](const ui64 hash) { - const auto predSet = [&](const ui64 hashSecondary) { - hashes.emplace(hashSecondary); - }; - BuildHashesSet(hash, predSet); + const auto predSet = [&](const ui64 hashSecondary) { + hashes.emplace(hashSecondary); }; - TNGrammBuilder builder; + TNGrammBuilder builder(HashesCount); for (auto&& c : foundColumns) { for (auto&& ls : c.second.GetLikeSequences()) { - builder.FillNGrammHashes(NGrammSize, ls.second.GetOperation(), ls.second.GetValue(), pred); + builder.FillNGrammHashes(NGrammSize, ls.second.GetOperation(), ls.second.GetValue(), predSet); } } branch->MutableIndexes().emplace_back(std::make_shared(GetIndexId(), std::move(hashes))); diff --git a/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.h b/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.h index 98af4556a5a5..c548b958de55 100644 --- a/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.h +++ b/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.h @@ -23,26 +23,6 @@ class TIndexMeta: public TIndexByColumns { AFL_VERIFY(NGrammSize > 2); } - static const ui64 HashesConstructorP = ((ui64)2 << 31) - 1; - static const ui64 HashesConstructorA = (ui64)2 << 16; - - template - void BuildHashesSet(const ui64 originalHash, const TActor& actor) const { - AFL_VERIFY(HashesCount < HashesConstructorP); - for (ui32 b = 1; b <= HashesCount; ++b) { - const ui64 hash = (HashesConstructorA * originalHash + b) % HashesConstructorP; - actor(hash); - } - } - - template - void BuildHashesSet(const TContainer& originalHashes, const TActor& actor) const { - AFL_VERIFY(HashesCount < HashesConstructorP); - for (auto&& hOriginal : originalHashes) { - BuildHashesSet(hOriginal, actor); - } - } - protected: virtual TConclusionStatus DoCheckModificationCompatibility(const IIndexMeta& /*newMeta*/) const override { return TConclusionStatus::Fail("not supported"); From d967cfcf361fedaa0f0b211a2e9c72eb420c3216 Mon Sep 17 00:00:00 2001 From: ivanmorozov333 Date: Wed, 25 Dec 2024 14:23:54 +0300 Subject: [PATCH 2/7] improve test --- ydb/core/kqp/ut/olap/indexes_ut.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ydb/core/kqp/ut/olap/indexes_ut.cpp b/ydb/core/kqp/ut/olap/indexes_ut.cpp index 014b5c6abe3e..be8e4e7eae8c 100644 --- a/ydb/core/kqp/ut/olap/indexes_ut.cpp +++ b/ydb/core/kqp/ut/olap/indexes_ut.cpp @@ -482,7 +482,7 @@ Y_UNIT_TEST_SUITE(KqpOlapIndexes) { } { ResetZeroLevel(csController); - ui32 requestsCount = 100; + ui32 requestsCount = 500; for (ui32 i = 0; i < requestsCount; ++i) { const ui32 idx = RandomNumber(uids.size()); const auto query = [](const TString& res, const TString& uid, const ui32 level) { @@ -494,12 +494,12 @@ Y_UNIT_TEST_SUITE(KqpOlapIndexes) { }; ExecuteSQL(query(resourceIds[idx], uids[idx], levels[idx]), "[[1u;]]"); } - AFL_VERIFY(csController->GetIndexesSkippingOnSelect().Val() - SkipStart > 1)("approved", csController->GetIndexesApprovedOnSelect().Val() - ApproveStart)( + AFL_VERIFY(csController->GetIndexesSkippingOnSelect().Val() - SkipStart)("approved", csController->GetIndexesApprovedOnSelect().Val() - ApproveStart)( "skipped", csController->GetIndexesSkippingOnSelect().Val() - SkipStart); } { ResetZeroLevel(csController); - ui32 requestsCount = 100; + ui32 requestsCount = 500; for (ui32 i = 0; i < requestsCount; ++i) { const ui32 idx = RandomNumber(uids.size()); const auto query = [](const TString& res, const TString& uid, const ui32 level) { @@ -511,13 +511,13 @@ Y_UNIT_TEST_SUITE(KqpOlapIndexes) { }; ExecuteSQL(query(resourceIds[idx], uids[idx], levels[idx]), "[[1u;]]"); } - AFL_VERIFY(csController->GetIndexesSkippingOnSelect().Val() - SkipStart > 1)( + AFL_VERIFY(csController->GetIndexesSkippingOnSelect().Val() - SkipStart)( "approved", csController->GetIndexesApprovedOnSelect().Val() - ApproveStart)( "skipped", csController->GetIndexesSkippingOnSelect().Val() - SkipStart); } { ResetZeroLevel(csController); - ui32 requestsCount = 100; + ui32 requestsCount = 500; for (ui32 i = 0; i < requestsCount; ++i) { const ui32 idx = RandomNumber(uids.size()); const auto query = [](const TString& res, const TString& uid, const ui32 level) { @@ -529,7 +529,7 @@ Y_UNIT_TEST_SUITE(KqpOlapIndexes) { }; ExecuteSQL(query(resourceIds[idx], uids[idx], levels[idx]), "[[1u;]]"); } - AFL_VERIFY(csController->GetIndexesSkippingOnSelect().Val() - SkipStart > 1)( + AFL_VERIFY(csController->GetIndexesSkippingOnSelect().Val() - SkipStart)( "approved", csController->GetIndexesApprovedOnSelect().Val() - ApproveStart)( "skipped", csController->GetIndexesSkippingOnSelect().Val() - SkipStart); } From 5d3f63b578cfa8b20510a1b02bf6b1ae25328404 Mon Sep 17 00:00:00 2001 From: ivanmorozov333 Date: Wed, 25 Dec 2024 15:06:10 +0300 Subject: [PATCH 3/7] speed up --- .../storage/indexes/bloom_ngramm/meta.cpp | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp b/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp index 9a4157421a33..e23d78eb687f 100644 --- a/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp +++ b/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp @@ -82,25 +82,30 @@ class TNGrammBuilder { template void BuildNGramms(const char* data, const ui32 dataSize, const std::optional op, const ui32 nGrammSize, const TAction& pred) const { + TBuffer fakeString; + AFL_VERIFY(nGrammSize >= 3)("value", nGrammSize); if (!op || op == NRequest::TLikePart::EOperation::StartsWith) { for (ui32 c = 1; c <= nGrammSize; ++c) { - TBuffer fakeStart; - fakeStart.Fill('\0', nGrammSize - c); - fakeStart.Append(data, std::min(c, dataSize)); - if (fakeStart.size() < nGrammSize) { - fakeStart.Append(Zeros.data(), nGrammSize - fakeStart.size()); + fakeString.Clear(); + fakeString.Fill('\0', nGrammSize - c); + fakeString.Append(data, std::min(c, dataSize)); + if (fakeString.size() < nGrammSize) { + fakeString.Fill('\0', nGrammSize - fakeString.size()); } - BuildHashesSet(CalcHash(fakeStart.data(), nGrammSize), pred); + BuildHashesSet(CalcHash(fakeString.data(), nGrammSize), pred); } } - for (ui32 c = 0; c < dataSize; ++c) { - if (c + nGrammSize <= dataSize) { - pred(CalcHash(data + c, nGrammSize)); - } else if (!op || op == NRequest::TLikePart::EOperation::EndsWith) { - TBuffer fakeStart; - fakeStart.Append(data + c, dataSize - c); - fakeStart.Append(Zeros.data(), nGrammSize - fakeStart.size()); - BuildHashesSet(CalcHash(fakeStart.data(), nGrammSize), pred); + ui32 c = 0; + for (; c + nGrammSize <= dataSize; ++c) { + pred(CalcHash(data + c, nGrammSize)); + } + + if (!op || op == NRequest::TLikePart::EOperation::EndsWith) { + for (; c < dataSize; ++c) { + fakeString.Clear(); + fakeString.Append(data + c, dataSize - c); + fakeString.Fill('\0', nGrammSize - fakeString.size()); + BuildHashesSet(CalcHash(fakeString.data(), nGrammSize), pred); } } } From 8da475e02accedf657d3e67b4a1eb0af88afd8ea Mon Sep 17 00:00:00 2001 From: ivanmorozov333 Date: Wed, 25 Dec 2024 15:23:22 +0300 Subject: [PATCH 4/7] speed up 0.6% --- .../columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp b/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp index e23d78eb687f..28b158ab3483 100644 --- a/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp +++ b/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp @@ -153,8 +153,9 @@ TString TIndexMeta::DoBuildIndexImpl(TChunkedBatchReader& reader) const { TNGrammBuilder builder(HashesCount); std::vector bitsVector(FilterSizeBytes * 8, false); + bool* memAccessor = &bitsVector[0]; const auto predSet = [&](const ui64 hashSecondary) { - bitsVector[hashSecondary % (FilterSizeBytes * 8)] = true; + memAccessor[hashSecondary % (FilterSizeBytes * 8)] = true; }; for (reader.Start(); reader.IsCorrect();) { builder.FillNGrammHashes(NGrammSize, reader.begin()->GetCurrentChunk(), predSet); From fafb00dcfde713be261f21a78663d3f49325e91e Mon Sep 17 00:00:00 2001 From: ivanmorozov333 Date: Wed, 25 Dec 2024 16:38:53 +0300 Subject: [PATCH 5/7] speed up --- .../engines/storage/indexes/bloom_ngramm/meta.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp b/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp index 28b158ab3483..514f1fc9dff7 100644 --- a/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp +++ b/ydb/core/tx/columnshard/engines/storage/indexes/bloom_ngramm/meta.cpp @@ -66,9 +66,14 @@ class TNGrammBuilder { ui64 CalcHash(const char* data, const ui32 size) const { if (size == 3) { - return ((ui64)data[0]) | (((ui64)data[1]) << 8) | (((ui64)data[2]) << 16); + return (*(const ui32*)data) & 0x00FFFFFF; +// TStringBuilder sb; +// sb << res << "/" << (ui32)((ui8*)&res)[0] << "/" << (ui32)((ui8*)&res)[1] << "/" << (ui32)((ui8*)&res)[2] << "/" +// << (ui32)((ui8*)&res)[3] << " vs " << (ui64)data[0] << "/" << (((ui64)data[1])) << "/" << (((ui64)data[2])) << Endl; +// Cerr << sb; +// return (ui64(*(const ui32*)data)) >> 8; } else if (size == 4) { - return *(ui32*)&data[0]; + return *(const ui32*)data; } else { uint64_t h = 2166136261; for (size_t i = 0; i < size; i++) { From ea682c9a6d1ead5a95ec19a529478b56260240ae Mon Sep 17 00:00:00 2001 From: ivanmorozov333 Date: Wed, 25 Dec 2024 16:41:10 +0300 Subject: [PATCH 6/7] correct signals --- .../engines/reader/common_reader/iterator/fetching.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ydb/core/tx/columnshard/engines/reader/common_reader/iterator/fetching.h b/ydb/core/tx/columnshard/engines/reader/common_reader/iterator/fetching.h index 14ca43ec9960..bd972ec728ff 100644 --- a/ydb/core/tx/columnshard/engines/reader/common_reader/iterator/fetching.h +++ b/ydb/core/tx/columnshard/engines/reader/common_reader/iterator/fetching.h @@ -27,12 +27,12 @@ class TFetchingStepSignals: public NColumnShard::TCommonCountersOwner { public: TFetchingStepSignals(NColumnShard::TCommonCountersOwner&& owner) : TBase(std::move(owner)) - , DurationCounter(TBase::GetDeriviative("duration_ms")) - , BytesCounter(TBase::GetDeriviative("bytes_ms")) { + , DurationCounter(TBase::GetDeriviative("Duration/Us")) + , BytesCounter(TBase::GetDeriviative("Bytes/Count")) { } void AddDuration(const TDuration d) const { - DurationCounter->Add(d.MilliSeconds()); + DurationCounter->Add(d.MicroSeconds()); } void AddBytes(const ui32 v) const { From d10ed7735f75946225721c032957b70db604ff51 Mon Sep 17 00:00:00 2001 From: ivanmorozov333 Date: Wed, 25 Dec 2024 16:45:33 +0300 Subject: [PATCH 7/7] correction --- .../engines/reader/common_reader/iterator/fetching.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ydb/core/tx/columnshard/engines/reader/common_reader/iterator/fetching.h b/ydb/core/tx/columnshard/engines/reader/common_reader/iterator/fetching.h index bd972ec728ff..34b60a608f21 100644 --- a/ydb/core/tx/columnshard/engines/reader/common_reader/iterator/fetching.h +++ b/ydb/core/tx/columnshard/engines/reader/common_reader/iterator/fetching.h @@ -56,7 +56,7 @@ class TFetchingStepsSignalsCollection: public NColumnShard::TCommonCountersOwner public: TFetchingStepsSignalsCollection() - : TBase("scan_steps") { + : TBase("ScanSteps") { } static TFetchingStepSignals GetSignals(const TString& name) {