Skip to content

Commit c5eecd5

Browse files
scanner simple speed up (#13281)
1 parent 2b30a70 commit c5eecd5

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

ydb/core/tx/columnshard/engines/reader/simple_reader/iterator/scanner.cpp

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ void TScanHead::OnSourceReady(const std::shared_ptr<IDataSource>& source, std::s
1818
Context->GetCommonContext()->GetCounters().OnSourceFinished(
1919
source->GetRecordsCount(), source->GetUsedRawBytes(), tableExt ? tableExt->num_rows() : 0);
2020

21-
if ((!tableExt || !tableExt->num_rows()) && Context->GetCommonContext()->GetReadMetadata()->HasLimit() && InFlightLimit < MaxInFlight) {
22-
InFlightLimit = 2 * InFlightLimit;
23-
}
2421
source->MutableStageResult().SetResultChunk(std::move(tableExt), startIndex, recordsCount);
2522
while (FetchingSources.size()) {
2623
auto frontSource = FetchingSources.front();
@@ -59,13 +56,18 @@ void TScanHead::OnSourceReady(const std::shared_ptr<IDataSource>& source, std::s
5956
AFL_VERIFY(FetchingSourcesByIdx.erase(frontSource->GetSourceIdx()));
6057
FetchingSources.pop_front();
6158
frontSource->ClearResult();
62-
if (Context->GetCommonContext()->GetReadMetadata()->HasLimit() && FetchingSources.size() && frontSource->GetResultRecordsCount()) {
63-
FinishedSources.emplace(frontSource);
64-
while (FinishedSources.size() && (*FinishedSources.begin())->GetFinish() < FetchingSources.front()->GetStart()) {
65-
auto fetchingSource = FetchingSources.front();
59+
if (Context->GetCommonContext()->GetReadMetadata()->HasLimit() && SortedSources.size() && frontSource->GetResultRecordsCount()) {
60+
AFL_VERIFY(FetchingInFlightSources.erase(frontSource));
61+
AFL_VERIFY(FinishedSources.emplace(frontSource).second);
62+
while (FinishedSources.size() && (*FinishedSources.begin())->GetFinish() < SortedSources.front()->GetStart()) {
6663
auto finishedSource = *FinishedSources.begin();
64+
if (!finishedSource->GetResultRecordsCount() && Context->GetCommonContext()->GetReadMetadata()->HasLimit() &&
65+
InFlightLimit < MaxInFlight) {
66+
InFlightLimit = 2 * InFlightLimit;
67+
}
6768
FetchedCount += finishedSource->GetResultRecordsCount();
6869
FinishedSources.erase(FinishedSources.begin());
70+
--IntervalsInFlightCount;
6971
AFL_DEBUG(NKikimrServices::TX_COLUMNSHARD)("event", "source_finished")("source_id", finishedSource->GetSourceId())(
7072
"source_idx", finishedSource->GetSourceIdx())("limit", Context->GetCommonContext()->GetReadMetadata()->GetLimitRobust())(
7173
"fetched", finishedSource->GetResultRecordsCount());
@@ -119,14 +121,41 @@ TConclusion<bool> TScanHead::BuildNextInterval() {
119121
if (!Context->IsActive()) {
120122
return false;
121123
}
124+
if (InFlightLimit <= IntervalsInFlightCount) {
125+
return false;
126+
}
127+
if (SortedSources.size() == 0) {
128+
return false;
129+
}
122130
bool changed = false;
123-
while (SortedSources.size() && FetchingSources.size() < InFlightLimit) {
131+
ui32 inFlightCountLocal = 0;
132+
if (SortedSources.size()) {
133+
for (auto it = FetchingInFlightSources.begin(); it != FetchingInFlightSources.end(); ++it) {
134+
if ((*it)->GetFinish() < SortedSources.front()->GetStart()) {
135+
++inFlightCountLocal;
136+
}
137+
}
138+
}
139+
AFL_VERIFY(IntervalsInFlightCount == inFlightCountLocal)("count_global", IntervalsInFlightCount)("count_local", inFlightCountLocal);
140+
while (SortedSources.size() && inFlightCountLocal < InFlightLimit) {
124141
SortedSources.front()->StartProcessing(SortedSources.front());
125142
FetchingSources.emplace_back(SortedSources.front());
126143
FetchingSourcesByIdx.emplace(SortedSources.front()->GetSourceIdx(), SortedSources.front());
144+
AFL_VERIFY(FetchingInFlightSources.emplace(SortedSources.front()).second);
127145
SortedSources.pop_front();
146+
if (SortedSources.size()) {
147+
ui32 inFlightCountLocalNew = 0;
148+
for (auto it = FetchingInFlightSources.begin(); it != FetchingInFlightSources.end(); ++it) {
149+
if ((*it)->GetFinish() < SortedSources.front()->GetStart()) {
150+
++inFlightCountLocalNew;
151+
}
152+
}
153+
AFL_VERIFY(inFlightCountLocal <= inFlightCountLocalNew);
154+
inFlightCountLocal = inFlightCountLocalNew;
155+
}
128156
changed = true;
129157
}
158+
IntervalsInFlightCount = inFlightCountLocal;
130159
return changed;
131160
}
132161

ydb/core/tx/columnshard/engines/reader/simple_reader/iterator/scanner.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class TScanHead {
2929
std::deque<std::shared_ptr<IDataSource>> SortedSources;
3030
std::deque<std::shared_ptr<IDataSource>> FetchingSources;
3131
std::set<std::shared_ptr<IDataSource>, IDataSource::TCompareFinishForScanSequence> FinishedSources;
32+
std::set<std::shared_ptr<IDataSource>, IDataSource::TCompareFinishForScanSequence> FetchingInFlightSources;
33+
TPositiveControlInteger IntervalsInFlightCount;
3234
ui64 FetchedCount = 0;
3335
ui64 InFlightLimit = 1;
3436
ui64 MaxInFlight = 256;

ydb/library/accessor/positive_integer.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,10 @@ ui64 TPositiveControlInteger::Val() const {
3535
return Value;
3636
}
3737

38-
}
38+
}
39+
40+
template<>
41+
void Out<NKikimr::TPositiveControlInteger>(IOutputStream& o,
42+
typename TTypeTraits<NKikimr::TPositiveControlInteger>::TFuncParam x) {
43+
o << x.Val();
44+
}

ydb/library/accessor/positive_integer.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22
#include <util/system/types.h>
3+
#include <util/stream/output.h>
4+
#include <util/generic/typetraits.h>
35

46
namespace NKikimr {
57

@@ -11,6 +13,10 @@ class TPositiveControlInteger {
1113
TPositiveControlInteger(const ui64 value)
1214
: Value(value) {
1315

16+
}
17+
TPositiveControlInteger(const ui32 value)
18+
: Value(value) {
19+
1420
}
1521
TPositiveControlInteger(const i64 value);
1622
ui64 Add(const ui64 value);
@@ -37,4 +43,4 @@ class TPositiveControlInteger {
3743
}
3844
};
3945

40-
}
46+
}

0 commit comments

Comments
 (0)