Skip to content

Commit

Permalink
Add [UN]LIKELY to if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
alexowens90 committed Apr 16, 2024
1 parent de93314 commit 0248fce
Showing 1 changed file with 23 additions and 32 deletions.
55 changes: 23 additions & 32 deletions cpp/arcticdb/processing/aggregation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,12 @@ enum class SortedAggregationOperator {
COUNT
};

// TODO: Find a way to get rid of the optionals
template<typename T>
class SumBucketAggregator {
public:
void push(T value) {
if constexpr (std::is_floating_point_v<T>) {
if (!std::isnan(value)) {
if (ARCTICDB_LIKELY(!std::isnan(value))) {
sum_ = sum_.value_or(0) + value;
}
} else {
Expand All @@ -237,12 +236,12 @@ class SumBucketAggregator {
}

T finalize() {
auto res = sum_.value_or(static_cast<T>(0));
sum_ = std::nullopt;
auto res = sum_.value_or(0);
sum_.reset();
return res;
}

bool has_value() const {
[[nodiscard]] bool has_value() const {
return sum_.has_value();
}
private:
Expand All @@ -254,7 +253,7 @@ class MinBucketAggregator {
public:
void push(T value) {
if constexpr (std::is_floating_point_v<T>) {
if (!std::isnan(value)) {
if (ARCTICDB_LIKELY(!std::isnan(value))) {
min_ = std::min(min_.value_or(std::numeric_limits<T>::max()), value);
}
} else {
Expand All @@ -270,11 +269,11 @@ class MinBucketAggregator {
debug::check<ErrorCode::E_ASSERTION_FAILURE>(min_.has_value(), "finalize called on non-float MinBucketAggregator with no values");
res = *min_;
}
min_ = std::nullopt;
min_.reset();
return res;
}

bool has_value() const {
[[nodiscard]] bool has_value() const {
return min_.has_value();
}
private:
Expand All @@ -286,7 +285,7 @@ class MaxBucketAggregator {
public:
void push(T value) {
if constexpr (std::is_floating_point_v<T>) {
if (!std::isnan(value)) {
if (ARCTICDB_LIKELY(!std::isnan(value))) {
max_ = std::max(max_.value_or(std::numeric_limits<T>::lowest()), value);
}
} else {
Expand All @@ -302,11 +301,11 @@ class MaxBucketAggregator {
debug::check<ErrorCode::E_ASSERTION_FAILURE>(max_.has_value(), "finalize called on non-float MaxBucketAggregator with no values");
res = *max_;
}
max_ = std::nullopt;
max_.reset();
return res;
}

bool has_value() const {
[[nodiscard]] bool has_value() const {
return max_.has_value();
}
private:
Expand All @@ -318,7 +317,7 @@ class MeanBucketAggregator {
public:
void push(T value) {
if constexpr (std::is_floating_point_v<T>) {
if (!std::isnan(value)) {
if (ARCTICDB_LIKELY(!std::isnan(value))) {
sum_ = sum_.value_or(0) + value;
++count_;
}
Expand All @@ -330,12 +329,12 @@ class MeanBucketAggregator {

double finalize() {
auto res = static_cast<double>(sum_.value_or(std::numeric_limits<double>::quiet_NaN())) / static_cast<double>(count_);
sum_ = std::nullopt;
sum_.reset();
count_ = 0;
return res;
}

bool has_value() const {
[[nodiscard]] bool has_value() const {
return sum_.has_value();
}
private:
Expand All @@ -348,15 +347,11 @@ class FirstBucketAggregator {
public:
void push(T value) {
if constexpr (std::is_floating_point_v<T>) {
if (first_.has_value()) {
if (std::isnan(*first_)) {
first_ = value;
}
} else {
if (ARCTICDB_UNLIKELY(!first_.has_value() || std::isnan(*first_))) {
first_ = value;
}
} else {
if (!first_.has_value()) {
if (ARCTICDB_UNLIKELY(!first_.has_value())) {
first_ = value;
}
}
Expand All @@ -370,11 +365,11 @@ class FirstBucketAggregator {
debug::check<ErrorCode::E_ASSERTION_FAILURE>(first_.has_value(), "finalize called on non-float FirstBucketAggregator with no values");
res = *first_;
}
first_ = std::nullopt;
first_.reset();
return res;
}

bool has_value() const {
[[nodiscard]] bool has_value() const {
return first_.has_value();
}
private:
Expand All @@ -386,11 +381,7 @@ class LastBucketAggregator {
public:
void push(T value) {
if constexpr (std::is_floating_point_v<T>) {
if (last_.has_value()) {
if (!std::isnan(value)) {
last_ = value;
}
} else {
if (ARCTICDB_LIKELY(!last_.has_value() || !std::isnan(value))) {
last_ = value;
}
} else {
Expand All @@ -406,11 +397,11 @@ class LastBucketAggregator {
debug::check<ErrorCode::E_ASSERTION_FAILURE>(last_.has_value(), "finalize called on non-float LastBucketAggregator with no values");
res = *last_;
}
last_ = std::nullopt;
last_.reset();
return res;
}

bool has_value() const {
[[nodiscard]] bool has_value() const {
return last_.has_value();
}
private:
Expand All @@ -422,7 +413,7 @@ class CountBucketAggregator {
template<typename T>
void push(T value) {
if constexpr (std::is_floating_point_v<T>) {
if (!std::isnan(value)) {
if (ARCTICDB_LIKELY(!std::isnan(value))) {
count_ = count_.value_or(0) + 1;
}
} else {
Expand All @@ -432,11 +423,11 @@ class CountBucketAggregator {

uint64_t finalize() {
auto res = count_.value_or(0);
count_ = std::nullopt;
count_.reset();
return res;
}

bool has_value() const {
[[nodiscard]] bool has_value() const {
return count_.has_value();
}
private:
Expand Down

0 comments on commit 0248fce

Please sign in to comment.