Skip to content

Commit 5dde912

Browse files
authored
avoid code duplication of cell vectors hascode and equals (#12437)
1 parent e5f6c12 commit 5dde912

File tree

5 files changed

+77
-132
lines changed

5 files changed

+77
-132
lines changed

ydb/core/kqp/runtime/kqp_stream_lookup_worker.cpp

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -62,70 +62,7 @@ NScheme::TTypeInfo UnpackTypeInfo(NKikimr::NMiniKQL::TType* type) {
6262
return NScheme::TypeInfoFromMiniKQLType(type);
6363
}
6464

65-
struct THashableKey {
66-
TConstArrayRef<TCell> Cells;
67-
68-
template <typename H>
69-
friend H AbslHashValue(H h, const THashableKey& key) {
70-
h = H::combine(std::move(h), key.Cells.size());
71-
for (const TCell& cell : key.Cells) {
72-
h = H::combine(std::move(h), cell.IsNull());
73-
if (!cell.IsNull()) {
74-
h = H::combine(std::move(h), cell.Size());
75-
h = H::combine_contiguous(std::move(h), cell.Data(), cell.Size());
76-
}
77-
}
78-
return h;
79-
}
80-
};
81-
82-
struct TKeyHash {
83-
using is_transparent = void;
84-
85-
size_t operator()(TConstArrayRef<TCell> key) const {
86-
return absl::Hash<THashableKey>()(THashableKey{ key });
87-
}
88-
};
89-
90-
struct TKeyEq {
91-
using is_transparent = void;
92-
93-
bool operator()(TConstArrayRef<TCell> a, TConstArrayRef<TCell> b) const {
94-
if (a.size() != b.size()) {
95-
return false;
96-
}
97-
98-
const TCell* pa = a.data();
99-
const TCell* pb = b.data();
100-
if (pa == pb) {
101-
return true;
102-
}
103-
104-
size_t left = a.size();
105-
while (left > 0) {
106-
if (pa->IsNull()) {
107-
if (!pb->IsNull()) {
108-
return false;
109-
}
110-
} else {
111-
if (pb->IsNull()) {
112-
return false;
113-
}
114-
if (pa->Size() != pb->Size()) {
115-
return false;
116-
}
117-
if (pa->Size() > 0 && ::memcmp(pa->Data(), pb->Data(), pa->Size()) != 0) {
118-
return false;
119-
}
120-
}
121-
++pa;
122-
++pb;
123-
--left;
124-
}
12565

126-
return true;
127-
}
128-
};
12966
} // !namespace
13067

13168
TKqpStreamLookupWorker::TKqpStreamLookupWorker(NKikimrKqp::TKqpStreamLookupSettings&& settings,
@@ -1001,7 +938,7 @@ class TKqpJoinRows : public TKqpStreamLookupWorker {
1001938
std::deque<std::pair<TOwnedCellVec, NUdf::TUnboxedValue>> UnprocessedRows;
1002939
std::deque<TOwnedTableRange> UnprocessedKeys;
1003940
std::unordered_map<ui64, std::vector<TOwnedTableRange>> PendingKeysByReadId;
1004-
absl::flat_hash_map<TOwnedCellVec, TLeftRowInfo, TKeyHash, TKeyEq> PendingLeftRowsByKey;
941+
absl::flat_hash_map<TOwnedCellVec, TLeftRowInfo, NKikimr::TCellVectorsHash, NKikimr::TCellVectorsEquals> PendingLeftRowsByKey;
1005942
std::unordered_map<ui64, TResultBatch> ResultRowsBySeqNo;
1006943
ui64 InputRowSeqNo = 0;
1007944
ui64 CurrentResultSeqNo = 0;

ydb/core/scheme/scheme_tablecell.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <ydb/library/actors/core/actorid.h>
55
#include <util/string/escape.h>
66

7+
#include <library/cpp/containers/absl_flat_hash/flat_hash_map.h>
8+
79
namespace NKikimr {
810

911
void TOwnedCellVec::TData::operator delete(void* mem) noexcept {
@@ -64,6 +66,63 @@ TOwnedCellVec::TInit TOwnedCellVec::Allocate(TOwnedCellVec::TCellVec cells) {
6466
};
6567
}
6668

69+
struct THashableKey {
70+
TConstArrayRef<TCell> Cells;
71+
72+
template <typename H>
73+
friend H AbslHashValue(H h, const THashableKey& key) {
74+
h = H::combine(std::move(h), key.Cells.size());
75+
for (const TCell& cell : key.Cells) {
76+
h = H::combine(std::move(h), cell.IsNull());
77+
if (!cell.IsNull()) {
78+
h = H::combine(std::move(h), cell.Size());
79+
h = H::combine_contiguous(std::move(h), cell.Data(), cell.Size());
80+
}
81+
}
82+
return h;
83+
}
84+
};
85+
86+
size_t TCellVectorsHash::operator()(TConstArrayRef<TCell> key) const {
87+
return absl::Hash<THashableKey>()(THashableKey{ key });
88+
}
89+
90+
bool TCellVectorsEquals::operator() (TConstArrayRef<TCell> a, TConstArrayRef<TCell> b) const {
91+
if (a.size() != b.size()) {
92+
return false;
93+
}
94+
95+
const TCell* pa = a.data();
96+
const TCell* pb = b.data();
97+
if (pa == pb) {
98+
return true;
99+
}
100+
101+
size_t left = a.size();
102+
while (left > 0) {
103+
if (pa->IsNull()) {
104+
if (!pb->IsNull()) {
105+
return false;
106+
}
107+
} else {
108+
if (pb->IsNull()) {
109+
return false;
110+
}
111+
if (pa->Size() != pb->Size()) {
112+
return false;
113+
}
114+
if (pa->Size() > 0 && ::memcmp(pa->Data(), pb->Data(), pa->Size()) != 0) {
115+
return false;
116+
}
117+
}
118+
++pa;
119+
++pb;
120+
--left;
121+
}
122+
123+
return true;
124+
}
125+
67126
namespace {
68127

69128
struct TCellHeader {

ydb/core/scheme/scheme_tablecell.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,22 @@ inline size_t EstimateSize(TCellsRef cells) {
179179
size += AlignUp(cellSize);
180180
}
181181
}
182-
182+
183183
return size;
184184
}
185185

186+
struct TCellVectorsHash {
187+
using is_transparent = void;
188+
189+
size_t operator()(TConstArrayRef<TCell> key) const;
190+
};
191+
192+
struct TCellVectorsEquals {
193+
using is_transparent = void;
194+
195+
bool operator()(TConstArrayRef<TCell> a, TConstArrayRef<TCell> b) const;
196+
};
197+
186198
inline int CompareCellsAsByteString(const TCell& a, const TCell& b, bool isDescending) {
187199
const char* pa = (const char*)a.Data();
188200
const char* pb = (const char*)b.Data();
@@ -558,7 +570,7 @@ class TSerializedCellVec {
558570
explicit operator bool() const
559571
{
560572
return !Cells.empty();
561-
}
573+
}
562574

563575
// read headers, assuming the buf is correct and append additional cells at the end
564576
static bool UnsafeAppendCells(TConstArrayRef<TCell> cells, TString& serializedCellVec);

ydb/core/scheme/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ PEERDIR(
2525
# temporary.
2626
ydb/library/pretty_types_print/protobuf
2727
library/cpp/lwtrace/mon
28+
library/cpp/containers/absl_flat_hash
2829
)
2930

3031
END()

ydb/core/tx/datashard/conflicts_cache.h

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -23,71 +23,6 @@ class TDataShard;
2323
* for.
2424
*/
2525
class TTableConflictsCache {
26-
struct THashableKey {
27-
TConstArrayRef<TCell> Cells;
28-
29-
template <typename H>
30-
friend H AbslHashValue(H h, const THashableKey& key) {
31-
h = H::combine(std::move(h), key.Cells.size());
32-
for (const TCell& cell : key.Cells) {
33-
h = H::combine(std::move(h), cell.IsNull());
34-
if (!cell.IsNull()) {
35-
h = H::combine(std::move(h), cell.Size());
36-
h = H::combine_contiguous(std::move(h), cell.Data(), cell.Size());
37-
}
38-
}
39-
return h;
40-
}
41-
};
42-
43-
struct TKeyHash {
44-
using is_transparent = void;
45-
46-
size_t operator()(TConstArrayRef<TCell> key) const {
47-
return absl::Hash<THashableKey>()(THashableKey{ key });
48-
}
49-
};
50-
51-
struct TKeyEq {
52-
using is_transparent = void;
53-
54-
bool operator()(TConstArrayRef<TCell> a, TConstArrayRef<TCell> b) const {
55-
if (a.size() != b.size()) {
56-
return false;
57-
}
58-
59-
const TCell* pa = a.data();
60-
const TCell* pb = b.data();
61-
if (pa == pb) {
62-
return true;
63-
}
64-
65-
size_t left = a.size();
66-
while (left > 0) {
67-
if (pa->IsNull()) {
68-
if (!pb->IsNull()) {
69-
return false;
70-
}
71-
} else {
72-
if (pb->IsNull()) {
73-
return false;
74-
}
75-
if (pa->Size() != pb->Size()) {
76-
return false;
77-
}
78-
if (pa->Size() > 0 && ::memcmp(pa->Data(), pb->Data(), pa->Size()) != 0) {
79-
return false;
80-
}
81-
}
82-
++pa;
83-
++pb;
84-
--left;
85-
}
86-
87-
return true;
88-
}
89-
};
90-
9126
struct TWriteKey {
9227
TOwnedCellVec Key;
9328
absl::flat_hash_set<ui64> UncommittedWrites;
@@ -102,7 +37,8 @@ class TTableConflictsCache {
10237
absl::flat_hash_set<TWriteKey*> WriteKeys;
10338
};
10439

105-
using TWriteKeys = absl::flat_hash_map<TOwnedCellVec, std::unique_ptr<TWriteKey>, TKeyHash, TKeyEq>;
40+
using TWriteKeys = absl::flat_hash_map<TOwnedCellVec, std::unique_ptr<TWriteKey>,
41+
NKikimr::TCellVectorsHash, NKikimr::TCellVectorsEquals>;
10642
using TUncommittedWrites = absl::flat_hash_map<ui64, std::unique_ptr<TUncommittedWrite>>;
10743
using TDistributedWrites = absl::flat_hash_map<ui64, std::unique_ptr<TDistributedWrite>>;
10844

0 commit comments

Comments
 (0)