Skip to content

Commit

Permalink
Add benchmarking code
Browse files Browse the repository at this point in the history
  • Loading branch information
Qup42 committed May 20, 2024
1 parent f738b40 commit cfc581e
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 31 deletions.
38 changes: 32 additions & 6 deletions src/engine/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,28 @@ void Server::executeUpdateQuery(const QueryExecutionTree& qet,
AD_FAIL();
}
};

// #define QL_UPDATE_VECTOR
// #define QL_UPDATE_PRESORT

#ifdef QL_UPDATE_VECTOR
LOG(INFO) << "Executing update using std::vector" << std::endl;
#else
LOG(INFO) << "Executing update using IdTable" << std::endl;
#endif
#ifdef QL_UPDATE_PRESORT
LOG(INFO) << "Sorting triples before location" << std::endl;
#else
LOG(INFO) << "Locating triples without sorting before" << std::endl;
#endif

#ifdef QL_UPDATE_VECTOR
std::vector<std::array<Id, 3>> toInsert;
std::vector<std::array<Id, 3>> toDelete;
#else
IdTable toInsert(3, qet.getRootOperation()->allocator());
// std::vector<std::array<Id, 3>> toInsert;
toInsert.reserve(res->size() * toInsertTemplates.size());
IdTable toDelete(3, qet.getRootOperation()->allocator());
// std::vector<std::array<Id, 3>> toDelete;
#endif
toInsert.reserve(res->size() * toInsertTemplates.size());
toDelete.reserve(res->size() * toDeleteTemplates.size());
// Result size is size(query result) x num template rows
// TODO: ideas only process template rows with variables here, do ones with
Expand Down Expand Up @@ -701,19 +717,29 @@ void Server::executeUpdateQuery(const QueryExecutionTree& qet,
step.start();
auto locatePermutation = [&toDelete, &toInsert, &step,
&qet](Permutation::Enum permutation) {
#ifdef QL_UPDATE_PRESORT
auto sortOrderTmp = Permutation::toKeyOrder(permutation);
const std::vector<ColumnIndex> sortOrder{sortOrderTmp.begin(),
sortOrderTmp.end()};
#ifdef QL_UPDATE_VECTOR
std::sort(toInsert.begin(), toInsert.end());
std::sort(toDelete.begin(), toDelete.end());
#else
Engine::sort(toInsert, sortOrder);
Engine::sort(toDelete, sortOrder);
// std::sort(toInsert.begin(), toInsert.end());
// std::sort(toDelete.begin(), toDelete.end());
#endif
LOG(INFO) << "Sorting triples took " << step.msecs() << std::endl;
step.start();
#endif
const Permutation& pos =
qet.getQec()->getIndex().getImpl().getPermutation(permutation);
#ifdef QL_UPDATE_PRESORT
LocatedTriple::locateSortedTriplesInPermutation(toInsert, pos);
LocatedTriple::locateSortedTriplesInPermutation(toDelete, pos);
#else
LocatedTriple::locateTriplesInPermutation(toInsert, pos);
LocatedTriple::locateTriplesInPermutation(toDelete, pos);
#endif
LOG(INFO) << "Locating triples took " << step.msecs() << std::endl;
step.start();
};
Expand Down
139 changes: 123 additions & 16 deletions src/index/LocatedTriples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,112 @@ std::vector<LocatedTriple> LocatedTriple::locateSortedTriplesInPermutation(
return out;
}

// Added for benchmarking
std::vector<LocatedTriple> LocatedTriple::locateSortedTriplesInPermutation(
const vector<IdTriple>& triples, const Permutation& permutation) {
if (triples.size() == 0) return {};

auto keyOrder = permutation.keyOrder();
const Permutation::MetaData& meta = permutation.metaData();
const vector<CompressedBlockMetadata>& blocks = meta.blockData();

vector<LocatedTriple> out{triples.size()};
size_t currentBlockIndex = 0;

for (auto [id1, id2, id3] : triples) {
CompressedBlockMetadata::PermutedTriple triple = {id1, id2, id3};

while (triple > blocks.at(currentBlockIndex).lastTriple_ &&
currentBlockIndex > blocks.size() - 1) {
currentBlockIndex++;
}

out.push_back(
{currentBlockIndex, LocatedTriple::NO_ROW_INDEX, id1, id2, id3, false});
}

return out;
}

// Added for benchmarking
std::vector<LocatedTriple> LocatedTriple::locateTriplesInPermutation(
const IdTable& triples, const Permutation& permutation) {
AD_CONTRACT_CHECK(triples.numColumns() == 3);

if (triples.numRows() == 0) return {};

auto keyOrder = permutation.keyOrder();
const Permutation::MetaData& meta = permutation.metaData();
const vector<CompressedBlockMetadata>& blocks = meta.blockData();

vector<LocatedTriple> out{triples.size()};
size_t currentBlockIndex;
for (size_t i : std::views::iota((size_t)0, triples.size())) {
auto id1 = triples(i, keyOrder[0]);
auto id2 = triples(i, keyOrder[1]);
auto id3 = triples(i, keyOrder[2]);
currentBlockIndex =
std::lower_bound(blocks.begin(), blocks.end(),
std::array<Id, 3>{id1, id2, id3},
[&](const CompressedBlockMetadata& block,
const auto& triple) -> bool {
// std::array<Id, 3> kann auch < aus den komponenten
const auto& lastTriple = block.lastTriple_;
if (lastTriple.col0Id_ < triple[0]) {
return true;
} else if (lastTriple.col0Id_ == triple[0]) {
if (lastTriple.col1Id_ < triple[1]) {
return true;
} else if (lastTriple.col1Id_ == triple[1]) {
return lastTriple.col2Id_ < triple[2];
}
}
return false;
}) -
blocks.begin();
out.push_back(
{currentBlockIndex, LocatedTriple::NO_ROW_INDEX, id1, id2, id3, false});
}

return out;
}

// Added for benchmarking
std::vector<LocatedTriple> LocatedTriple::locateTriplesInPermutation(
const std::vector<IdTriple>& triples, const Permutation& permutation) {
// Triples können auch sortiert sein.
const Permutation::MetaData& meta = permutation.metaData();
const vector<CompressedBlockMetadata>& blocks = meta.blockData();

vector<LocatedTriple> out{triples.size()};
size_t currentBlockIndex;
for (auto& [id1, id2, id3] : triples) {
currentBlockIndex =
std::lower_bound(blocks.begin(), blocks.end(),
std::array<Id, 3>{id1, id2, id3},
[&](const CompressedBlockMetadata& block,
const auto& triple) -> bool {
// std::array<Id, 3> kann auch < aus den komponenten
const auto& lastTriple = block.lastTriple_;
if (lastTriple.col0Id_ < triple[0]) {
return true;
} else if (lastTriple.col0Id_ == triple[0]) {
if (lastTriple.col1Id_ < triple[1]) {
return true;
} else if (lastTriple.col1Id_ == triple[1]) {
return lastTriple.col2Id_ < triple[2];
}
}
return false;
}) -
blocks.begin();
out.push_back(
{currentBlockIndex, LocatedTriple::NO_ROW_INDEX, id1, id2, id3, false});
}

return out;
}

// ____________________________________________________________________________
LocatedTriple LocatedTriple::locateTripleInPermutation(
Id id1, Id id2, Id id3, const Permutation& permutation) {
Expand Down Expand Up @@ -118,14 +224,16 @@ LocatedTriple LocatedTriple::locateTripleInPermutation(
DecompressedBlock blockTuples =
reader.readAndDecompressBlock(*matchingBlock, columnIndices);

if(matchingBlock->firstTriple_.col0Id_ <= id1) {
if (matchingBlock->firstTriple_.col0Id_ <= id1) {
// The triple is actually inside this block.
locatedTriple.rowIndexInBlock =
std::lower_bound(
blockTuples.begin(),
blockTuples.end(), std::array<Id, 3>{id1, id2, id3},
blockTuples.begin(), blockTuples.end(),
std::array<Id, 3>{id1, id2, id3},
[](const auto& a, const auto& b) {
return a[0] < b[0] || (a[0] == b[0] && (a[1] < b[1] || (a[1] == b[1] && a[2] < b[2])));
return a[0] < b[0] ||
(a[0] == b[0] &&
(a[1] < b[1] || (a[1] == b[1] && a[2] < b[2])));
}) -
blockTuples.begin();
// Check if the triple at the found position is equal to `id1 id2 id3` to
Expand Down Expand Up @@ -163,8 +271,8 @@ std::pair<size_t, size_t> LocatedTriplesPerBlock::numTriples(
(scanSpec.col0Id().value() == locatedTriple.id1 &&
(!scanSpec.col1Id() ||
(scanSpec.col1Id().value() == locatedTriple.id2 &&
(!scanSpec.col2Id() || scanSpec.col2Id().value() == locatedTriple.id3)))))
{
(!scanSpec.col2Id() ||
scanSpec.col2Id().value() == locatedTriple.id3))))) {
if (locatedTriple.existsInIndex) {
++countExists;
} else {
Expand All @@ -175,13 +283,10 @@ std::pair<size_t, size_t> LocatedTriplesPerBlock::numTriples(
return {countNew, countExists};
}

size_t LocatedTriplesPerBlock::mergeTriples(size_t blockIndex,
std::optional<IdTable> block,
IdTable& result,
size_t offsetInResult,
ScanSpecification scanSpec,
size_t rowIndexInBlockBegin,
size_t rowIndexInBlockEnd) const {
size_t LocatedTriplesPerBlock::mergeTriples(
size_t blockIndex, std::optional<IdTable> block, IdTable& result,
size_t offsetInResult, ScanSpecification scanSpec,
size_t rowIndexInBlockBegin, size_t rowIndexInBlockEnd) const {
// TODO:
AD_CONTRACT_CHECK(!scanSpec.col2Id().has_value());

Expand Down Expand Up @@ -229,9 +334,11 @@ size_t LocatedTriplesPerBlock::mergeTriples(size_t blockIndex,
// considered, given the `matchMode`.
auto locatedTripleMatches = [&]() {
return !scanSpec.col0Id().has_value() ||
(locatedTriple->id1 == scanSpec.col0Id().value() && (!scanSpec.col1Id().has_value() ||
(locatedTriple->id2 == scanSpec.col1Id().value() && (!scanSpec.col2Id().has_value() ||
locatedTriple->id3 == scanSpec.col2Id().value()))));
(locatedTriple->id1 == scanSpec.col0Id().value() &&
(!scanSpec.col1Id().has_value() ||
(locatedTriple->id2 == scanSpec.col1Id().value() &&
(!scanSpec.col2Id().has_value() ||
locatedTriple->id3 == scanSpec.col2Id().value()))));
};

// Advance to the first located triple in the specified range.
Expand Down
27 changes: 18 additions & 9 deletions src/index/LocatedTriples.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include "engine/idTable/IdTable.h"
#include "global/IdTriple.h"
#include "util/HashMap.h"
#include "index/CompressedRelation.h"
#include "util/HashMap.h"

class Permutation;

Expand Down Expand Up @@ -42,8 +42,18 @@ struct LocatedTriple {
static LocatedTriple locateTripleInPermutation(
Id id1, Id id2, Id id3, const Permutation& permutation);

// Added for benchmarking
static std::vector<LocatedTriple> locateTriplesInPermutation(
const IdTable& triples, const Permutation& permutation);
// Added for benchmarking
static std::vector<LocatedTriple> locateTriplesInPermutation(
const std::vector<IdTriple>& triples, const Permutation& permutation);

static std::vector<LocatedTriple> locateSortedTriplesInPermutation(
const IdTable& triples, const Permutation& permutation);
// Added for benchmarking
static std::vector<LocatedTriple> locateSortedTriplesInPermutation(
const std::vector<IdTriple>& triples, const Permutation& permutation);

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

public:
using ScanSpecification = CompressedRelationReader::ScanSpecification;
// Get the number of located triples for the given block that match `id1` (if
// provided) and `id2` (if provided). The return value is a pair of numbers:
// Get the number of located triples for the given block that match the
// ScanSpecification. The return value is a pair of numbers:
// first, the number of existing triples ("to be deleted") and second, the
// number of new triples ("to be inserted").
std::pair<size_t, size_t> numTriples(size_t blockIndex, ScanSpecification scanSpec) const;
std::pair<size_t, size_t> numTriples(size_t blockIndex,
ScanSpecification scanSpec) const;

// Merge located triples for `blockIndex` with the given index `block` and
// write to `result`, starting from position `offsetInResult`. Consider only
Expand All @@ -115,11 +124,11 @@ class LocatedTriplesPerBlock {
// larger than all triples there. This requires that the ScanSpecification
// has `col0Id` and `col1Id` set.
//
size_t mergeTriples(size_t blockIndex, std::optional<IdTable> block,
IdTable& result, size_t offsetInResult,
ScanSpecification scanSpec,
size_t rowIndexInBlockBegin = 0,
size_t rowIndexInBlockEnd = LocatedTriple::NO_ROW_INDEX) const;
size_t mergeTriples(
size_t blockIndex, std::optional<IdTable> block, IdTable& result,
size_t offsetInResult, ScanSpecification scanSpec,
size_t rowIndexInBlockBegin = 0,
size_t rowIndexInBlockEnd = LocatedTriple::NO_ROW_INDEX) const;

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

0 comments on commit cfc581e

Please sign in to comment.