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

[BUILD] Provide LIKELY / UNLIKELY macros #2580

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 25 additions & 11 deletions api/include/opentelemetry/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,36 @@

#pragma once

#if !defined(OPENTELEMETRY_LIKELY_IF) && defined(__cplusplus)
#if !defined(OPENTELEMETRY_LIKELY) && defined(__cplusplus)
// Only use likely with C++20
# if __cplusplus >= 202002L
// GCC 9 has likely attribute but do not support declare it at the beginning of statement
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
# if __has_cpp_attribute(likely)
# define OPENTELEMETRY_LIKELY_IF(...) \
if (__VA_ARGS__) \
[[likely]]

# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
# if __has_cpp_attribute(likely)
# define OPENTELEMETRY_LIKELY [[likely]]
# endif
# endif
# endif
#endif
#if !defined(OPENTELEMETRY_LIKELY_IF) && (defined(__clang__) || defined(__GNUC__))
# define OPENTELEMETRY_LIKELY_IF(...) if (__builtin_expect(!!(__VA_ARGS__), true))

#ifndef OPENTELEMETRY_LIKELY
# define OPENTELEMETRY_LIKELY
#endif
#ifndef OPENTELEMETRY_LIKELY_IF
# define OPENTELEMETRY_LIKELY_IF(...) if (__VA_ARGS__)

#if !defined(OPENTELEMETRY_UNLIKELY) && defined(__cplusplus)
// Only use unlikely with C++20
# if __cplusplus >= 202002L
// GCC 9 has unlikely attribute but do not support declare it at the beginning of statement
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
# if __has_cpp_attribute(unlikely)
# define OPENTELEMETRY_UNLIKELY [[unlikely]]
# endif
# endif
# endif
#endif

#ifndef OPENTELEMETRY_UNLIKELY
# define OPENTELEMETRY_UNLIKELY
#endif

/// \brief Declare variable as maybe unused
Expand Down
6 changes: 4 additions & 2 deletions api/include/opentelemetry/logs/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,15 @@ class Logger

inline bool Enabled(Severity severity, const EventId &event_id) const noexcept
{
OPENTELEMETRY_LIKELY_IF(Enabled(severity) == false) { return false; }
if (!Enabled(severity))
OPENTELEMETRY_LIKELY { return false; }
return EnabledImplementation(severity, event_id);
}

inline bool Enabled(Severity severity, int64_t event_id) const noexcept
{
OPENTELEMETRY_LIKELY_IF(Enabled(severity) == false) { return false; }
if (!Enabled(severity))
OPENTELEMETRY_LIKELY { return false; }
return EnabledImplementation(severity, event_id);
}

Expand Down
6 changes: 4 additions & 2 deletions exporters/otlp/src/otlp_log_recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ namespace otlp

const opentelemetry::sdk::resource::Resource &OtlpLogRecordable::GetResource() const noexcept
{
OPENTELEMETRY_LIKELY_IF(nullptr != resource_) { return *resource_; }
if (nullptr != resource_)
OPENTELEMETRY_LIKELY { return *resource_; }

return opentelemetry::sdk::logs::ReadableLogRecord::GetDefaultResource();
}

const opentelemetry::sdk::instrumentationscope::InstrumentationScope &
OtlpLogRecordable::GetInstrumentationScope() const noexcept
{
OPENTELEMETRY_LIKELY_IF(nullptr != instrumentation_scope_) { return *instrumentation_scope_; }
if (nullptr != instrumentation_scope_)
OPENTELEMETRY_LIKELY { return *instrumentation_scope_; }

return opentelemetry::sdk::logs::ReadableLogRecord::GetDefaultInstrumentationScope();
}
Expand Down
6 changes: 4 additions & 2 deletions sdk/src/logs/read_write_log_record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ const std::unordered_map<std::string, opentelemetry::common::AttributeValue>

const opentelemetry::sdk::resource::Resource &ReadWriteLogRecord::GetResource() const noexcept
{
OPENTELEMETRY_LIKELY_IF(nullptr != resource_) { return *resource_; }
if (nullptr != resource_)
OPENTELEMETRY_LIKELY { return *resource_; }

return GetDefaultResource();
}
Expand All @@ -172,7 +173,8 @@ void ReadWriteLogRecord::SetResource(
const opentelemetry::sdk::instrumentationscope::InstrumentationScope &
ReadWriteLogRecord::GetInstrumentationScope() const noexcept
{
OPENTELEMETRY_LIKELY_IF(nullptr != instrumentation_scope_) { return *instrumentation_scope_; }
if (nullptr != instrumentation_scope_)
OPENTELEMETRY_LIKELY { return *instrumentation_scope_; }

return GetDefaultInstrumentationScope();
}
Expand Down
14 changes: 8 additions & 6 deletions sdk/src/metrics/data/circular_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ struct AdaptingIntegerArrayIncrement
uint64_t operator()(std::vector<T> &backing)
{
const uint64_t result = backing[index] + count;
OPENTELEMETRY_LIKELY_IF(result <= uint64_t(std::numeric_limits<T>::max()))
{
backing[index] = static_cast<T>(result);
return 0;
}
if (result <= uint64_t(std::numeric_limits<T>::max()))
OPENTELEMETRY_LIKELY
{
backing[index] = static_cast<T>(result);
return 0;
}
return result;
}
};
Expand Down Expand Up @@ -76,7 +77,8 @@ struct AdaptingIntegerArrayCopy
void AdaptingIntegerArray::Increment(size_t index, uint64_t count)
{
const uint64_t result = nostd::visit(AdaptingIntegerArrayIncrement{index, count}, backing_);
OPENTELEMETRY_LIKELY_IF(result == 0) { return; }
if (result == 0)
OPENTELEMETRY_LIKELY { return; }
EnlargeToFit(result);
Increment(index, count);
}
Expand Down