Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cache-max-... settings for the root operation, also when it's lazy #1709

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/engine/Operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,13 @@ ProtoResult Operation::runComputation(const ad_utility::Timer& timer,
// _____________________________________________________________________________
CacheValue Operation::runComputationAndPrepareForCache(
const ad_utility::Timer& timer, ComputationMode computationMode,
const QueryCacheKey& cacheKey, bool pinned) {
const QueryCacheKey& cacheKey, bool pinned, bool isRoot) {
auto& cache = _executionContext->getQueryTreeCache();
auto result = runComputation(timer, computationMode);
auto maxSize =
std::min(RuntimeParameters().get<"lazy-result-max-cache-size">(),
cache.getMaxSizeSingleEntry());
isRoot ? cache.getMaxSizeSingleEntry()
: std::min(RuntimeParameters().get<"lazy-result-max-cache-size">(),
cache.getMaxSizeSingleEntry());
Comment on lines +233 to +235
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the modified tests, isRoot is always false, so I wonder why CodeCov ist not complaining about incomplete coverage here.

if (canResultBeCached() && !result.isFullyMaterialized() &&
!unlikelyToFitInCache(maxSize)) {
AD_CONTRACT_CHECK(!pinned);
Expand Down Expand Up @@ -306,9 +307,10 @@ std::shared_ptr<const Result> Operation::getResult(
updateRuntimeInformationOnFailure(timer.msecs());
}
});
auto cacheSetup = [this, &timer, computationMode, &cacheKey, pinResult]() {
auto cacheSetup = [this, &timer, computationMode, &cacheKey, pinResult,
isRoot]() {
return runComputationAndPrepareForCache(timer, computationMode, cacheKey,
pinResult);
pinResult, isRoot);
};

auto suitedForCache = [](const CacheValue& cacheValue) {
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ class Operation {
CacheValue runComputationAndPrepareForCache(const ad_utility::Timer& timer,
ComputationMode computationMode,
const QueryCacheKey& cacheKey,
bool pinned);
bool pinned, bool isRoot);

// Create and store the complete runtime information for this operation after
// it has either been successfully computed or read from the cache.
Expand Down Expand Up @@ -454,4 +454,5 @@ class Operation {
FRIEND_TEST(Operation, ensureLazyOperationIsCachedIfSmallEnough);
FRIEND_TEST(Operation, checkLazyOperationIsNotCachedIfTooLarge);
FRIEND_TEST(Operation, checkLazyOperationIsNotCachedIfUnlikelyToFitInCache);
FRIEND_TEST(Operation, checkMaxCacheSizeIsComputedCorrectly);
};
52 changes: 49 additions & 3 deletions test/OperationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "engine/NeutralElementOperation.h"
#include "engine/ValuesForTesting.h"
#include "global/RuntimeParameters.h"
#include "util/GTestHelpers.h"
#include "util/IdTableHelpers.h"
#include "util/IndexTestHelpers.h"
#include "util/OperationTestHelpers.h"
Expand Down Expand Up @@ -556,7 +557,7 @@ TEST(Operation, ensureLazyOperationIsCachedIfSmallEnough) {

auto cacheValue = valuesForTesting.runComputationAndPrepareForCache(
timer, ComputationMode::LAZY_IF_SUPPORTED, makeQueryCacheKey("test"),
false);
false, false);
EXPECT_FALSE(
qec->getQueryTreeCache().cacheContains(makeQueryCacheKey("test")));

Expand Down Expand Up @@ -614,7 +615,7 @@ TEST(Operation, checkLazyOperationIsNotCachedIfTooLarge) {

cacheValue = valuesForTesting.runComputationAndPrepareForCache(
timer, ComputationMode::LAZY_IF_SUPPORTED, makeQueryCacheKey("test"),
false);
false, false);
EXPECT_FALSE(
qec->getQueryTreeCache().cacheContains(makeQueryCacheKey("test")));
}
Expand All @@ -641,7 +642,7 @@ TEST(Operation, checkLazyOperationIsNotCachedIfUnlikelyToFitInCache) {

auto cacheValue = valuesForTesting.runComputationAndPrepareForCache(
timer, ComputationMode::LAZY_IF_SUPPORTED, makeQueryCacheKey("test"),
false);
false, false);
EXPECT_FALSE(
qec->getQueryTreeCache().cacheContains(makeQueryCacheKey("test")));

Expand All @@ -653,6 +654,51 @@ TEST(Operation, checkLazyOperationIsNotCachedIfUnlikelyToFitInCache) {
qec->getQueryTreeCache().cacheContains(makeQueryCacheKey("test")));
}

// _____________________________________________________________________________
TEST(Operation, checkMaxCacheSizeIsComputedCorrectly) {
auto runTest = [](ad_utility::MemorySize cacheLimit,
ad_utility::MemorySize runtimeParameterLimit, bool isRoot,
ad_utility::MemorySize expectedSize,
ad_utility::source_location sourceLocation =
ad_utility::source_location::current()) {
auto loc = generateLocationTrace(sourceLocation);
auto qec = getQec();
qec->getQueryTreeCache().clearAll();
std::vector<IdTable> idTablesVector{};
idTablesVector.push_back(makeIdTableFromVector({{3, 4}}));
ValuesForTesting valuesForTesting{
qec, std::move(idTablesVector), {Variable{"?x"}, Variable{"?y"}}, true};

ad_utility::MemorySize actualCacheSize;
valuesForTesting.setCacheSizeStorage(&actualCacheSize);

absl::Cleanup restoreOriginalSize{
[qec, original = qec->getQueryTreeCache().getMaxSizeSingleEntry()]() {
qec->getQueryTreeCache().setMaxSizeSingleEntry(original);
}};
qec->getQueryTreeCache().setMaxSizeSingleEntry(cacheLimit);

auto cleanup = setRuntimeParameterForTest<"lazy-result-max-cache-size">(
runtimeParameterLimit);

ad_utility::Timer timer{ad_utility::Timer::InitialStatus::Started};

auto cacheValue = valuesForTesting.runComputationAndPrepareForCache(
timer, ComputationMode::LAZY_IF_SUPPORTED, makeQueryCacheKey("test"),
false, isRoot);

EXPECT_EQ(actualCacheSize, expectedSize);
};

runTest(10_B, 10_B, true, 10_B);
runTest(10_B, 10_B, false, 10_B);
runTest(10_B, 1_B, false, 1_B);
runTest(1_B, 10_B, false, 1_B);
runTest(10_B, 1_B, true, 10_B);
runTest(1_B, 10_B, true, 1_B);
}

// _____________________________________________________________________________
TEST(OperationTest, disableCaching) {
auto qec = getQec();
qec->getQueryTreeCache().clearAll();
Expand Down
11 changes: 10 additions & 1 deletion test/engine/ValuesForTesting.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ValuesForTesting : public Operation {
size_t sizeEstimate_;
size_t costEstimate_;
bool unlikelyToFitInCache_ = false;
ad_utility::MemorySize* cacheSizeStorage_ = nullptr;

public:
// Create an operation that has as its result the given `table` and the given
Expand Down Expand Up @@ -115,9 +116,17 @@ class ValuesForTesting : public Operation {
}
return {std::move(table), resultSortedOn(), localVocab_.clone()};
}
bool unlikelyToFitInCache(ad_utility::MemorySize) const override {
bool unlikelyToFitInCache(ad_utility::MemorySize cacheSize) const override {
if (cacheSizeStorage_ != nullptr) {
*cacheSizeStorage_ = cacheSize;
}
return unlikelyToFitInCache_;
}

void setCacheSizeStorage(ad_utility::MemorySize* cacheSizeStorage) {
cacheSizeStorage_ = cacheSizeStorage;
}

bool supportsLimit() const override { return supportsLimit_; }

bool& forceFullyMaterialized() { return forceFullyMaterialized_; }
Expand Down
Loading