change IndexSet::addMetrics return type to pair<metrics, iterator> to reduce another find call

This commit is contained in:
Xiaoxi Wang 2023-03-06 22:23:55 -08:00
parent 272fa49638
commit 41d01629f1
4 changed files with 27 additions and 21 deletions

View File

@ -549,9 +549,9 @@ void StorageServerMetrics::add(KeyRangeMap<int>& map, KeyRangeRef const& keys, i
// Returns the sampled metric value (possibly 0, possibly increased by the sampling factor)
int64_t TransientStorageMetricSample::addAndExpire(const Key& key, int64_t metric, double expiration) {
int64_t x = add(key, metric);
auto x = add(key, metric);
if (x)
queue.emplace_back(expiration, std::make_pair(*sample.find(key), -x));
queue.emplace_back(expiration, std::make_pair(key, -x));
return x;
}
@ -575,17 +575,18 @@ bool TransientStorageMetricSample::roll(int64_t metric) const {
}
void TransientStorageMetricSample::poll(KeyRangeMap<std::vector<PromiseStream<StorageMetrics>>>& waitMap,
StorageMetrics m) {
StorageMetrics metrics) {
double now = ::now();
while (queue.size() && queue.front().first <= now) {
KeyRef key = queue.front().second.first;
int64_t delta = queue.front().second.second;
ASSERT(delta != 0);
if (sample.addMetric(key, delta) == 0)
sample.erase(key);
auto [m, it] = sample.addMetric(key, delta);
if (m == 0)
sample.erase(it);
StorageMetrics deltaM = m * delta;
StorageMetrics deltaM = metrics * delta;
auto v = waitMap[key];
for (int i = 0; i < v.size(); i++) {
CODE_PROBE(true, "TransientStorageMetricSample poll update");
@ -603,8 +604,9 @@ void TransientStorageMetricSample::poll() {
int64_t delta = queue.front().second.second;
ASSERT(delta != 0);
if (sample.addMetric(key, delta) == 0)
sample.erase(key);
auto [m, it] = sample.addMetric(key, delta);
if (m == 0)
sample.erase(it);
queue.pop_front();
}
@ -621,8 +623,9 @@ int64_t TransientStorageMetricSample::add(const Key& key, int64_t metric) {
metric = metric < 0 ? -metricUnitsPerSample : metricUnitsPerSample;
}
if (sample.addMetric(key, metric) == 0)
sample.erase(key);
auto [m, it] = sample.addMetric(key, metric);
if (m == 0)
sample.erase(it);
return metric;
}

View File

@ -74,6 +74,8 @@ struct TransientStorageMetricSample : StorageMetricSample {
private:
bool roll(int64_t metric) const;
// return the sampled metric delta
int64_t add(const Key& key, int64_t metric);
};

View File

@ -193,9 +193,9 @@ public:
int insert(const std::vector<std::pair<T, Metric>>& data, bool replaceExisting = true);
// Increase the metric for the given item by the given amount. Inserts data into the set if it
// doesn't exist. Returns the new sum.
// doesn't exist. Returns the pair of new sum and inserted iterator.
template <class T_, class Metric_>
Metric addMetric(T_&& data, Metric_&& metric);
std::pair<Metric, iterator> addMetric(T_&& data, Metric_&& metric);
// Remove the data item, if any, which is equal to key
template <class Key>
@ -820,15 +820,16 @@ typename IndexedSet<T, Metric>::template Impl<isConst>::IteratorT IndexedSet<T,
template <class T, class Metric>
template <class T_, class Metric_>
Metric IndexedSet<T, Metric>::addMetric(T_&& data, Metric_&& metric) {
std::pair<Metric, typename IndexedSet<T, Metric>::iterator> IndexedSet<T, Metric>::addMetric(T_&& data,
Metric_&& metric) {
auto i = find(data);
if (i == end()) {
insert(std::forward<T_>(data), std::forward<Metric_>(metric));
return metric;
auto it = insert(std::forward<T_>(data), std::forward<Metric_>(metric));
return { metric, it };
} else {
Metric m = metric + getMetric(i);
insert(std::forward<T_>(data), m);
return m;
auto it = insert(std::forward<T_>(data), m);
return { m, it };
}
}

View File

@ -127,14 +127,14 @@ struct TransientThresholdMetricSample : MetricSample<T> {
int64_t delta = std::get<2>(queue.front());
ASSERT(delta != 0);
int64_t val = this->sample.addMetric(T(key), delta);
auto [val, it] = this->sample.addMetric(T(key), delta);
if (val < thresholdLimit && (val + std::abs(delta)) >= thresholdLimit) {
auto iter = thresholdCrossedSet.find(key);
ASSERT(iter != thresholdCrossedSet.end());
thresholdCrossedSet.erase(iter);
}
if (val == 0)
this->sample.erase(key);
this->sample.erase(it);
queue.pop_front();
}
@ -159,7 +159,7 @@ private:
metric = metric < 0 ? -this->metricUnitsPerSample : this->metricUnitsPerSample;
}
int64_t val = this->sample.addMetric(T(key), metric);
auto [val, it] = this->sample.addMetric(T(key), metric);
if (val >= thresholdLimit) {
ASSERT((val - metric) < thresholdLimit ? thresholdCrossedSet.find(key) == thresholdCrossedSet.end()
: thresholdCrossedSet.find(key) != thresholdCrossedSet.end());
@ -167,7 +167,7 @@ private:
}
if (val == 0)
this->sample.erase(key);
this->sample.erase(it);
return metric;
}