Skip to content

Commit 25738f3

Browse files
authored
[BUILD] Provide LIKELY / UNLIKELY macros (#2580)
* Contributes to #2572 * Restored support for __builtin_expect() * Typo
1 parent 3e4b7d3 commit 25738f3

File tree

5 files changed

+129
-18
lines changed

5 files changed

+129
-18
lines changed

api/include/opentelemetry/common/macros.h

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,112 @@
33

44
#pragma once
55

6-
#if !defined(OPENTELEMETRY_LIKELY_IF) && defined(__cplusplus)
6+
/*
7+
Expected usage pattern:
8+
9+
if OPENTELEMETRY_LIKELY_CONDITION (ptr != nullptr)
10+
{
11+
do_something_likely();
12+
} else {
13+
do_something_unlikely();
14+
}
15+
16+
This pattern works with gcc/clang and __builtin_expect(),
17+
as well as with C++20.
18+
It is unclear if __builtin_expect() will be deprecated
19+
in favor of C++20 [[likely]] or not.
20+
21+
OPENTELEMETRY_LIKELY_CONDITION is preferred over OPENTELEMETRY_LIKELY,
22+
to be revisited when C++20 is required.
23+
*/
24+
25+
#if !defined(OPENTELEMETRY_LIKELY_CONDITION) && defined(__cplusplus)
26+
// Only use likely with C++20
27+
# if __cplusplus >= 202002L
728
// GCC 9 has likely attribute but do not support declare it at the beginning of statement
8-
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
9-
# if __has_cpp_attribute(likely)
10-
# define OPENTELEMETRY_LIKELY_IF(...) \
11-
if (__VA_ARGS__) \
12-
[[likely]]
29+
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
30+
# if __has_cpp_attribute(likely)
31+
# define OPENTELEMETRY_LIKELY_CONDITION(C) (C) [[likely]]
32+
# endif
33+
# endif
34+
# endif
35+
#endif
36+
#if !defined(OPENTELEMETRY_LIKELY_CONDITION) && (defined(__clang__) || defined(__GNUC__))
37+
// Only use if supported by the compiler
38+
# define OPENTELEMETRY_LIKELY_CONDITION(C) (__builtin_expect(!!(C), true))
39+
#endif
40+
#ifndef OPENTELEMETRY_LIKELY_CONDITION
41+
// Do not use likely annotations
42+
# define OPENTELEMETRY_LIKELY_CONDITION(C) (C)
43+
#endif
1344

45+
#if !defined(OPENTELEMETRY_UNLIKELY_CONDITION) && defined(__cplusplus)
46+
// Only use unlikely with C++20
47+
# if __cplusplus >= 202002L
48+
// GCC 9 has unlikely attribute but do not support declare it at the beginning of statement
49+
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
50+
# if __has_cpp_attribute(unlikely)
51+
# define OPENTELEMETRY_UNLIKELY_CONDITION(C) (C) [[unlikely]]
52+
# endif
1453
# endif
1554
# endif
1655
#endif
17-
#if !defined(OPENTELEMETRY_LIKELY_IF) && (defined(__clang__) || defined(__GNUC__))
18-
# define OPENTELEMETRY_LIKELY_IF(...) if (__builtin_expect(!!(__VA_ARGS__), true))
56+
#if !defined(OPENTELEMETRY_UNLIKELY_CONDITION) && (defined(__clang__) || defined(__GNUC__))
57+
// Only use if supported by the compiler
58+
# define OPENTELEMETRY_UNLIKELY_CONDITION(C) (__builtin_expect(!!(C), false))
1959
#endif
20-
#ifndef OPENTELEMETRY_LIKELY_IF
21-
# define OPENTELEMETRY_LIKELY_IF(...) if (__VA_ARGS__)
60+
#ifndef OPENTELEMETRY_UNLIKELY_CONDITION
61+
// Do not use unlikely annotations
62+
# define OPENTELEMETRY_UNLIKELY_CONDITION(C) (C)
63+
#endif
64+
65+
/*
66+
Expected usage pattern:
67+
68+
if (ptr != nullptr)
69+
OPENTELEMETRY_LIKELY
70+
{
71+
do_something_likely();
72+
} else {
73+
do_something_unlikely();
74+
}
75+
76+
This pattern works starting with C++20.
77+
See https://en.cppreference.com/w/cpp/language/attributes/likely
78+
79+
Please use OPENTELEMETRY_LIKELY_CONDITION instead for now.
80+
*/
81+
82+
#if !defined(OPENTELEMETRY_LIKELY) && defined(__cplusplus)
83+
// Only use likely with C++20
84+
# if __cplusplus >= 202002L
85+
// GCC 9 has likely attribute but do not support declare it at the beginning of statement
86+
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
87+
# if __has_cpp_attribute(likely)
88+
# define OPENTELEMETRY_LIKELY [[likely]]
89+
# endif
90+
# endif
91+
# endif
92+
#endif
93+
94+
#ifndef OPENTELEMETRY_LIKELY
95+
# define OPENTELEMETRY_LIKELY
96+
#endif
97+
98+
#if !defined(OPENTELEMETRY_UNLIKELY) && defined(__cplusplus)
99+
// Only use unlikely with C++20
100+
# if __cplusplus >= 202002L
101+
// GCC 9 has unlikely attribute but do not support declare it at the beginning of statement
102+
# if defined(__has_cpp_attribute) && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 9)
103+
# if __has_cpp_attribute(unlikely)
104+
# define OPENTELEMETRY_UNLIKELY [[unlikely]]
105+
# endif
106+
# endif
107+
# endif
108+
#endif
109+
110+
#ifndef OPENTELEMETRY_UNLIKELY
111+
# define OPENTELEMETRY_UNLIKELY
22112
#endif
23113

24114
/// \brief Declare variable as maybe unused

api/include/opentelemetry/logs/logger.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,19 @@ class Logger
258258

259259
inline bool Enabled(Severity severity, const EventId &event_id) const noexcept
260260
{
261-
OPENTELEMETRY_LIKELY_IF(Enabled(severity) == false) { return false; }
261+
if OPENTELEMETRY_LIKELY_CONDITION (!Enabled(severity))
262+
{
263+
return false;
264+
}
262265
return EnabledImplementation(severity, event_id);
263266
}
264267

265268
inline bool Enabled(Severity severity, int64_t event_id) const noexcept
266269
{
267-
OPENTELEMETRY_LIKELY_IF(Enabled(severity) == false) { return false; }
270+
if OPENTELEMETRY_LIKELY_CONDITION (!Enabled(severity))
271+
{
272+
return false;
273+
}
268274
return EnabledImplementation(severity, event_id);
269275
}
270276

exporters/otlp/src/otlp_log_recordable.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@ namespace otlp
1818

1919
const opentelemetry::sdk::resource::Resource &OtlpLogRecordable::GetResource() const noexcept
2020
{
21-
OPENTELEMETRY_LIKELY_IF(nullptr != resource_) { return *resource_; }
21+
if OPENTELEMETRY_LIKELY_CONDITION (nullptr != resource_)
22+
{
23+
return *resource_;
24+
}
2225

2326
return opentelemetry::sdk::logs::ReadableLogRecord::GetDefaultResource();
2427
}
2528

2629
const opentelemetry::sdk::instrumentationscope::InstrumentationScope &
2730
OtlpLogRecordable::GetInstrumentationScope() const noexcept
2831
{
29-
OPENTELEMETRY_LIKELY_IF(nullptr != instrumentation_scope_) { return *instrumentation_scope_; }
32+
if OPENTELEMETRY_LIKELY_CONDITION (nullptr != instrumentation_scope_)
33+
{
34+
return *instrumentation_scope_;
35+
}
3036

3137
return opentelemetry::sdk::logs::ReadableLogRecord::GetDefaultInstrumentationScope();
3238
}

sdk/src/logs/read_write_log_record.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ const std::unordered_map<std::string, opentelemetry::common::AttributeValue>
158158

159159
const opentelemetry::sdk::resource::Resource &ReadWriteLogRecord::GetResource() const noexcept
160160
{
161-
OPENTELEMETRY_LIKELY_IF(nullptr != resource_) { return *resource_; }
161+
if OPENTELEMETRY_LIKELY_CONDITION (nullptr != resource_)
162+
{
163+
return *resource_;
164+
}
162165

163166
return GetDefaultResource();
164167
}
@@ -172,7 +175,10 @@ void ReadWriteLogRecord::SetResource(
172175
const opentelemetry::sdk::instrumentationscope::InstrumentationScope &
173176
ReadWriteLogRecord::GetInstrumentationScope() const noexcept
174177
{
175-
OPENTELEMETRY_LIKELY_IF(nullptr != instrumentation_scope_) { return *instrumentation_scope_; }
178+
if OPENTELEMETRY_LIKELY_CONDITION (nullptr != instrumentation_scope_)
179+
{
180+
return *instrumentation_scope_;
181+
}
176182

177183
return GetDefaultInstrumentationScope();
178184
}

sdk/src/metrics/data/circular_buffer.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct AdaptingIntegerArrayIncrement
2121
uint64_t operator()(std::vector<T> &backing)
2222
{
2323
const uint64_t result = backing[index] + count;
24-
OPENTELEMETRY_LIKELY_IF(result <= uint64_t(std::numeric_limits<T>::max()))
24+
if OPENTELEMETRY_LIKELY_CONDITION (result <= uint64_t(std::numeric_limits<T>::max()))
2525
{
2626
backing[index] = static_cast<T>(result);
2727
return 0;
@@ -76,7 +76,10 @@ struct AdaptingIntegerArrayCopy
7676
void AdaptingIntegerArray::Increment(size_t index, uint64_t count)
7777
{
7878
const uint64_t result = nostd::visit(AdaptingIntegerArrayIncrement{index, count}, backing_);
79-
OPENTELEMETRY_LIKELY_IF(result == 0) { return; }
79+
if OPENTELEMETRY_LIKELY_CONDITION (result == 0)
80+
{
81+
return;
82+
}
8083
EnlargeToFit(result);
8184
Increment(index, count);
8285
}

0 commit comments

Comments
 (0)