Skip to content

Commit cfc581e

Browse files
committed
Add benchmarking code
1 parent f738b40 commit cfc581e

File tree

3 files changed

+173
-31
lines changed

3 files changed

+173
-31
lines changed

src/engine/Server.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -653,12 +653,28 @@ void Server::executeUpdateQuery(const QueryExecutionTree& qet,
653653
AD_FAIL();
654654
}
655655
};
656-
656+
// #define QL_UPDATE_VECTOR
657+
// #define QL_UPDATE_PRESORT
658+
659+
#ifdef QL_UPDATE_VECTOR
660+
LOG(INFO) << "Executing update using std::vector" << std::endl;
661+
#else
662+
LOG(INFO) << "Executing update using IdTable" << std::endl;
663+
#endif
664+
#ifdef QL_UPDATE_PRESORT
665+
LOG(INFO) << "Sorting triples before location" << std::endl;
666+
#else
667+
LOG(INFO) << "Locating triples without sorting before" << std::endl;
668+
#endif
669+
670+
#ifdef QL_UPDATE_VECTOR
671+
std::vector<std::array<Id, 3>> toInsert;
672+
std::vector<std::array<Id, 3>> toDelete;
673+
#else
657674
IdTable toInsert(3, qet.getRootOperation()->allocator());
658-
// std::vector<std::array<Id, 3>> toInsert;
659-
toInsert.reserve(res->size() * toInsertTemplates.size());
660675
IdTable toDelete(3, qet.getRootOperation()->allocator());
661-
// std::vector<std::array<Id, 3>> toDelete;
676+
#endif
677+
toInsert.reserve(res->size() * toInsertTemplates.size());
662678
toDelete.reserve(res->size() * toDeleteTemplates.size());
663679
// Result size is size(query result) x num template rows
664680
// TODO: ideas only process template rows with variables here, do ones with
@@ -701,19 +717,29 @@ void Server::executeUpdateQuery(const QueryExecutionTree& qet,
701717
step.start();
702718
auto locatePermutation = [&toDelete, &toInsert, &step,
703719
&qet](Permutation::Enum permutation) {
720+
#ifdef QL_UPDATE_PRESORT
704721
auto sortOrderTmp = Permutation::toKeyOrder(permutation);
705722
const std::vector<ColumnIndex> sortOrder{sortOrderTmp.begin(),
706723
sortOrderTmp.end()};
724+
#ifdef QL_UPDATE_VECTOR
725+
std::sort(toInsert.begin(), toInsert.end());
726+
std::sort(toDelete.begin(), toDelete.end());
727+
#else
707728
Engine::sort(toInsert, sortOrder);
708729
Engine::sort(toDelete, sortOrder);
709-
// std::sort(toInsert.begin(), toInsert.end());
710-
// std::sort(toDelete.begin(), toDelete.end());
730+
#endif
711731
LOG(INFO) << "Sorting triples took " << step.msecs() << std::endl;
712732
step.start();
733+
#endif
713734
const Permutation& pos =
714735
qet.getQec()->getIndex().getImpl().getPermutation(permutation);
736+
#ifdef QL_UPDATE_PRESORT
715737
LocatedTriple::locateSortedTriplesInPermutation(toInsert, pos);
716738
LocatedTriple::locateSortedTriplesInPermutation(toDelete, pos);
739+
#else
740+
LocatedTriple::locateTriplesInPermutation(toInsert, pos);
741+
LocatedTriple::locateTriplesInPermutation(toDelete, pos);
742+
#endif
717743
LOG(INFO) << "Locating triples took " << step.msecs() << std::endl;
718744
step.start();
719745
};

src/index/LocatedTriples.cpp

Lines changed: 123 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,112 @@ std::vector<LocatedTriple> LocatedTriple::locateSortedTriplesInPermutation(
4444
return out;
4545
}
4646

47+
// Added for benchmarking
48+
std::vector<LocatedTriple> LocatedTriple::locateSortedTriplesInPermutation(
49+
const vector<IdTriple>& triples, const Permutation& permutation) {
50+
if (triples.size() == 0) return {};
51+
52+
auto keyOrder = permutation.keyOrder();
53+
const Permutation::MetaData& meta = permutation.metaData();
54+
const vector<CompressedBlockMetadata>& blocks = meta.blockData();
55+
56+
vector<LocatedTriple> out{triples.size()};
57+
size_t currentBlockIndex = 0;
58+
59+
for (auto [id1, id2, id3] : triples) {
60+
CompressedBlockMetadata::PermutedTriple triple = {id1, id2, id3};
61+
62+
while (triple > blocks.at(currentBlockIndex).lastTriple_ &&
63+
currentBlockIndex > blocks.size() - 1) {
64+
currentBlockIndex++;
65+
}
66+
67+
out.push_back(
68+
{currentBlockIndex, LocatedTriple::NO_ROW_INDEX, id1, id2, id3, false});
69+
}
70+
71+
return out;
72+
}
73+
74+
// Added for benchmarking
75+
std::vector<LocatedTriple> LocatedTriple::locateTriplesInPermutation(
76+
const IdTable& triples, const Permutation& permutation) {
77+
AD_CONTRACT_CHECK(triples.numColumns() == 3);
78+
79+
if (triples.numRows() == 0) return {};
80+
81+
auto keyOrder = permutation.keyOrder();
82+
const Permutation::MetaData& meta = permutation.metaData();
83+
const vector<CompressedBlockMetadata>& blocks = meta.blockData();
84+
85+
vector<LocatedTriple> out{triples.size()};
86+
size_t currentBlockIndex;
87+
for (size_t i : std::views::iota((size_t)0, triples.size())) {
88+
auto id1 = triples(i, keyOrder[0]);
89+
auto id2 = triples(i, keyOrder[1]);
90+
auto id3 = triples(i, keyOrder[2]);
91+
currentBlockIndex =
92+
std::lower_bound(blocks.begin(), blocks.end(),
93+
std::array<Id, 3>{id1, id2, id3},
94+
[&](const CompressedBlockMetadata& block,
95+
const auto& triple) -> bool {
96+
// std::array<Id, 3> kann auch < aus den komponenten
97+
const auto& lastTriple = block.lastTriple_;
98+
if (lastTriple.col0Id_ < triple[0]) {
99+
return true;
100+
} else if (lastTriple.col0Id_ == triple[0]) {
101+
if (lastTriple.col1Id_ < triple[1]) {
102+
return true;
103+
} else if (lastTriple.col1Id_ == triple[1]) {
104+
return lastTriple.col2Id_ < triple[2];
105+
}
106+
}
107+
return false;
108+
}) -
109+
blocks.begin();
110+
out.push_back(
111+
{currentBlockIndex, LocatedTriple::NO_ROW_INDEX, id1, id2, id3, false});
112+
}
113+
114+
return out;
115+
}
116+
117+
// Added for benchmarking
118+
std::vector<LocatedTriple> LocatedTriple::locateTriplesInPermutation(
119+
const std::vector<IdTriple>& triples, const Permutation& permutation) {
120+
// Triples können auch sortiert sein.
121+
const Permutation::MetaData& meta = permutation.metaData();
122+
const vector<CompressedBlockMetadata>& blocks = meta.blockData();
123+
124+
vector<LocatedTriple> out{triples.size()};
125+
size_t currentBlockIndex;
126+
for (auto& [id1, id2, id3] : triples) {
127+
currentBlockIndex =
128+
std::lower_bound(blocks.begin(), blocks.end(),
129+
std::array<Id, 3>{id1, id2, id3},
130+
[&](const CompressedBlockMetadata& block,
131+
const auto& triple) -> bool {
132+
// std::array<Id, 3> kann auch < aus den komponenten
133+
const auto& lastTriple = block.lastTriple_;
134+
if (lastTriple.col0Id_ < triple[0]) {
135+
return true;
136+
} else if (lastTriple.col0Id_ == triple[0]) {
137+
if (lastTriple.col1Id_ < triple[1]) {
138+
return true;
139+
} else if (lastTriple.col1Id_ == triple[1]) {
140+
return lastTriple.col2Id_ < triple[2];
141+
}
142+
}
143+
return false;
144+
}) -
145+
blocks.begin();
146+
out.push_back(
147+
{currentBlockIndex, LocatedTriple::NO_ROW_INDEX, id1, id2, id3, false});
148+
}
149+
150+
return out;
151+
}
152+
47153
// ____________________________________________________________________________
48154
LocatedTriple LocatedTriple::locateTripleInPermutation(
49155
Id id1, Id id2, Id id3, const Permutation& permutation) {
@@ -118,14 +224,16 @@ LocatedTriple LocatedTriple::locateTripleInPermutation(
118224
DecompressedBlock blockTuples =
119225
reader.readAndDecompressBlock(*matchingBlock, columnIndices);
120226

121-
if(matchingBlock->firstTriple_.col0Id_ <= id1) {
227+
if (matchingBlock->firstTriple_.col0Id_ <= id1) {
122228
// The triple is actually inside this block.
123229
locatedTriple.rowIndexInBlock =
124230
std::lower_bound(
125-
blockTuples.begin(),
126-
blockTuples.end(), std::array<Id, 3>{id1, id2, id3},
231+
blockTuples.begin(), blockTuples.end(),
232+
std::array<Id, 3>{id1, id2, id3},
127233
[](const auto& a, const auto& b) {
128-
return a[0] < b[0] || (a[0] == b[0] && (a[1] < b[1] || (a[1] == b[1] && a[2] < b[2])));
234+
return a[0] < b[0] ||
235+
(a[0] == b[0] &&
236+
(a[1] < b[1] || (a[1] == b[1] && a[2] < b[2])));
129237
}) -
130238
blockTuples.begin();
131239
// Check if the triple at the found position is equal to `id1 id2 id3` to
@@ -163,8 +271,8 @@ std::pair<size_t, size_t> LocatedTriplesPerBlock::numTriples(
163271
(scanSpec.col0Id().value() == locatedTriple.id1 &&
164272
(!scanSpec.col1Id() ||
165273
(scanSpec.col1Id().value() == locatedTriple.id2 &&
166-
(!scanSpec.col2Id() || scanSpec.col2Id().value() == locatedTriple.id3)))))
167-
{
274+
(!scanSpec.col2Id() ||
275+
scanSpec.col2Id().value() == locatedTriple.id3))))) {
168276
if (locatedTriple.existsInIndex) {
169277
++countExists;
170278
} else {
@@ -175,13 +283,10 @@ std::pair<size_t, size_t> LocatedTriplesPerBlock::numTriples(
175283
return {countNew, countExists};
176284
}
177285

178-
size_t LocatedTriplesPerBlock::mergeTriples(size_t blockIndex,
179-
std::optional<IdTable> block,
180-
IdTable& result,
181-
size_t offsetInResult,
182-
ScanSpecification scanSpec,
183-
size_t rowIndexInBlockBegin,
184-
size_t rowIndexInBlockEnd) const {
286+
size_t LocatedTriplesPerBlock::mergeTriples(
287+
size_t blockIndex, std::optional<IdTable> block, IdTable& result,
288+
size_t offsetInResult, ScanSpecification scanSpec,
289+
size_t rowIndexInBlockBegin, size_t rowIndexInBlockEnd) const {
185290
// TODO:
186291
AD_CONTRACT_CHECK(!scanSpec.col2Id().has_value());
187292

@@ -229,9 +334,11 @@ size_t LocatedTriplesPerBlock::mergeTriples(size_t blockIndex,
229334
// considered, given the `matchMode`.
230335
auto locatedTripleMatches = [&]() {
231336
return !scanSpec.col0Id().has_value() ||
232-
(locatedTriple->id1 == scanSpec.col0Id().value() && (!scanSpec.col1Id().has_value() ||
233-
(locatedTriple->id2 == scanSpec.col1Id().value() && (!scanSpec.col2Id().has_value() ||
234-
locatedTriple->id3 == scanSpec.col2Id().value()))));
337+
(locatedTriple->id1 == scanSpec.col0Id().value() &&
338+
(!scanSpec.col1Id().has_value() ||
339+
(locatedTriple->id2 == scanSpec.col1Id().value() &&
340+
(!scanSpec.col2Id().has_value() ||
341+
locatedTriple->id3 == scanSpec.col2Id().value()))));
235342
};
236343

237344
// Advance to the first located triple in the specified range.

src/index/LocatedTriples.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#include "engine/idTable/IdTable.h"
88
#include "global/IdTriple.h"
9-
#include "util/HashMap.h"
109
#include "index/CompressedRelation.h"
10+
#include "util/HashMap.h"
1111

1212
class Permutation;
1313

@@ -42,8 +42,18 @@ struct LocatedTriple {
4242
static LocatedTriple locateTripleInPermutation(
4343
Id id1, Id id2, Id id3, const Permutation& permutation);
4444

45+
// Added for benchmarking
46+
static std::vector<LocatedTriple> locateTriplesInPermutation(
47+
const IdTable& triples, const Permutation& permutation);
48+
// Added for benchmarking
49+
static std::vector<LocatedTriple> locateTriplesInPermutation(
50+
const std::vector<IdTriple>& triples, const Permutation& permutation);
51+
4552
static std::vector<LocatedTriple> locateSortedTriplesInPermutation(
4653
const IdTable& triples, const Permutation& permutation);
54+
// Added for benchmarking
55+
static std::vector<LocatedTriple> locateSortedTriplesInPermutation(
56+
const std::vector<IdTriple>& triples, const Permutation& permutation);
4757

4858
// Special row index for triples that belong to the previous block (see the
4959
// definition for the location of a triple at the end of this file).
@@ -86,13 +96,12 @@ class LocatedTriplesPerBlock {
8696

8797
public:
8898
using ScanSpecification = CompressedRelationReader::ScanSpecification;
89-
// Get the number of located triples for the given block that match `id1` (if
90-
// provided) and `id2` (if provided). The return value is a pair of numbers:
9199
// Get the number of located triples for the given block that match the
92100
// ScanSpecification. The return value is a pair of numbers:
93101
// first, the number of existing triples ("to be deleted") and second, the
94102
// number of new triples ("to be inserted").
95-
std::pair<size_t, size_t> numTriples(size_t blockIndex, ScanSpecification scanSpec) const;
103+
std::pair<size_t, size_t> numTriples(size_t blockIndex,
104+
ScanSpecification scanSpec) const;
96105

97106
// Merge located triples for `blockIndex` with the given index `block` and
98107
// write to `result`, starting from position `offsetInResult`. Consider only
@@ -115,11 +124,11 @@ class LocatedTriplesPerBlock {
115124
// larger than all triples there. This requires that the ScanSpecification
116125
// has `col0Id` and `col1Id` set.
117126
//
118-
size_t mergeTriples(size_t blockIndex, std::optional<IdTable> block,
119-
IdTable& result, size_t offsetInResult,
120-
ScanSpecification scanSpec,
121-
size_t rowIndexInBlockBegin = 0,
122-
size_t rowIndexInBlockEnd = LocatedTriple::NO_ROW_INDEX) const;
127+
size_t mergeTriples(
128+
size_t blockIndex, std::optional<IdTable> block, IdTable& result,
129+
size_t offsetInResult, ScanSpecification scanSpec,
130+
size_t rowIndexInBlockBegin = 0,
131+
size_t rowIndexInBlockEnd = LocatedTriple::NO_ROW_INDEX) const;
123132

124133
// Add the given `locatedTriple` to the given `LocatedTriplesPerBlock`.
125134
// Return a handle to where it was added (`LocatedTriples` is a sorted set,

0 commit comments

Comments
 (0)