Skip to content

Commit

Permalink
Reverted change to caching - false cache hits causing incorrect lookups.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfellows committed Aug 1, 2024
1 parent 646f7ff commit d26a51c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,6 @@ namespace moja {
namespace modules {
namespace cbm {

struct GCCacheKey {
public:
void add(const std::string& classifierValue) {
_classifierValues.push_back(classifierValue);
}

bool operator<(const GCCacheKey& other) const {
if (_classifierValues.size() < other._classifierValues.size()) {
return true;
}

for (int i = 0; i < _classifierValues.size(); i++) {
if (_classifierValues[i] < other._classifierValues[i]) {
return true;
}
}

return false;
}

private:
std::vector<std::string> _classifierValues;
};

class GrowthCurveTransform : public flint::ITransform {
public:
void configure(DynamicObject config,
Expand All @@ -51,7 +27,7 @@ class GrowthCurveTransform : public flint::ITransform {
std::shared_ptr<datarepository::IProviderRelationalInterface> _provider;
mutable const flint::IVariable* _csetVar;
mutable DynamicVar _value;
mutable Poco::ThreadLocal<Poco::LRUCache<GCCacheKey, DynamicVar>> _cache;
mutable Poco::ThreadLocal<Poco::LRUCache<std::string, DynamicVar>> _cache;

const std::string buildSql(const DynamicObject& classifierSet) const;
const std::string buildDebuggingInfo(const DynamicObject& classifierSet) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,6 @@ namespace moja {
namespace modules {
namespace cbm {

struct TRCacheKey {
public:
void add(const std::string& classifierValue) {
_classifierValues.push_back(classifierValue);
}

bool operator<(const TRCacheKey& other) const {
if (_classifierValues.size() < other._classifierValues.size()) {
return true;
}

for (int i = 0; i < _classifierValues.size(); i++) {
if (_classifierValues[i] < other._classifierValues[i]) {
return true;
}
}

return false;
}

private:
std::vector<std::string> _classifierValues;
};

class TransitionRuleTransform : public flint::ITransform {
public:
void configure(DynamicObject config,
Expand All @@ -51,7 +27,7 @@ class TransitionRuleTransform : public flint::ITransform {
std::shared_ptr<datarepository::IProviderRelationalInterface> _provider;
mutable const flint::IVariable* _csetVar;
mutable DynamicVar _value;
mutable Poco::ThreadLocal<Poco::LRUCache<TRCacheKey, DynamicVar>> _cache;
mutable Poco::ThreadLocal<Poco::LRUCache<std::string, DynamicVar>> _cache;

const std::string buildSql(const DynamicObject& classifierSet) const;

Expand Down
16 changes: 3 additions & 13 deletions Source/moja.modules.cbm/src/growthcurvetransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,15 @@ namespace cbm {
}

const auto& cset = csetVariableValue.extract<DynamicObject>();

GCCacheKey key;
for (const auto& classifier : cset) {
if (!classifier.second.isEmpty()) {
const std::string& classifierValue = classifier.second;
key.add(classifierValue);
} else {
key.add("null");
}
}
const auto sql = buildSql(cset);

auto cachedValue = _cache->get(key);
auto cachedValue = _cache->get(sql);
if (!cachedValue.isNull()) {
_value = *cachedValue;
return _value;
}

DynamicVar value;
const auto sql = buildSql(cset);
const auto& gc = _provider->GetDataSet(sql);
if (gc.size() > 0) {
auto& gcRows = gc.extract<const std::vector<DynamicObject>>();
Expand All @@ -156,7 +146,7 @@ namespace cbm {
<< buildDebuggingInfo(cset);
}

_cache->add(key, value);
_cache->add(sql, value);
_value = value;

return _value;
Expand Down
16 changes: 3 additions & 13 deletions Source/moja.modules.cbm/src/transitionruletransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,15 @@ namespace cbm {
}

const auto& cset = csetVariableValue.extract<DynamicObject>();
const auto sql = buildSql(cset);

TRCacheKey key;
for (const auto& classifier : cset) {
if (!classifier.second.isEmpty()) {
const std::string& classifierValue = classifier.second;
key.add(classifierValue);
} else {
key.add("null");
}
}

auto cachedValue = _cache->get(key);
auto cachedValue = _cache->get(sql);
if (!cachedValue.isNull()) {
_value = *cachedValue;
return _value;
}

DynamicObject disturbanceTypeTransitions;
const auto sql = buildSql(cset);
const auto& result = _provider->GetDataSet(sql);
if (result.isVector()) {
for (auto row : result.extract<std::vector<DynamicObject>>()) {
Expand All @@ -142,7 +132,7 @@ namespace cbm {
disturbanceTypeTransitions[disturbanceType] = transitionId;
}

_cache->add(key, disturbanceTypeTransitions);
_cache->add(sql, disturbanceTypeTransitions);
_value = disturbanceTypeTransitions;

return _value;
Expand Down

0 comments on commit d26a51c

Please sign in to comment.