Skip to content

Commit 1e67666

Browse files
authored
Merge pull request #732 from open-telemetry/main
[CODE HEALTH] Fix clang-tidy performance warnings (open-telemetry#3941)
2 parents 7f102b6 + 86389bc commit 1e67666

File tree

7 files changed

+29
-23
lines changed

7 files changed

+29
-23
lines changed

.github/workflows/clang-tidy.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ jobs:
1717
matrix:
1818
include:
1919
- cmake_options: all-options-abiv1-preview
20-
warning_limit: 220
20+
warning_limit: 204
2121
- cmake_options: all-options-abiv2-preview
22-
warning_limit: 222
22+
warning_limit: 206
2323
env:
2424
CC: /usr/bin/clang-18
2525
CXX: /usr/bin/clang++-18

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ Increment the:
7575
* [TEST] CMake component install test for resource_detectors
7676
[#3940](https://github.com/open-telemetry/opentelemetry-cpp/pull/3940)
7777

78+
* [CODE HEALTH] Fix clang-tidy performance warnings
79+
[#3941](https://github.com/open-telemetry/opentelemetry-cpp/pull/3941)
80+
7881
Important changes:
7982

8083
* [BUILD] Revisit EventLogger deprecation

api/include/opentelemetry/context/context.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Context
3535
// Creates a context object from a key and value, this will
3636
// hold a shared_ptr to the head of the DataList linked list
3737
Context(nostd::string_view key, ContextValue value) noexcept
38-
: head_{nostd::shared_ptr<DataList>{new DataList(key, value)}}
38+
: head_{nostd::shared_ptr<DataList>{new DataList(key, std::move(value))}}
3939
{}
4040

4141
// Accepts a new iterable and then returns a new context that
@@ -59,7 +59,7 @@ class Context
5959
// exisiting list to the end of the new list.
6060
Context SetValue(nostd::string_view key, ContextValue value) noexcept
6161
{
62-
Context context = Context(key, value);
62+
Context context = Context(key, std::move(value));
6363
context.head_->next_ = head_;
6464
return context;
6565
}
@@ -125,13 +125,13 @@ class Context
125125

126126
// Builds a data list with just a key and value, so it will just be the head
127127
// and returns that head.
128-
DataList(nostd::string_view key, const ContextValue &value)
128+
DataList(nostd::string_view key, ContextValue value)
129129
{
130130
key_ = new char[key.size()];
131131
key_length_ = key.size();
132132
std::memcpy(key_, key.data(), key.size() * sizeof(char));
133133
next_ = nostd::shared_ptr<DataList>{nullptr};
134-
value_ = value;
134+
value_ = std::move(value);
135135
}
136136

137137
DataList(const DataList &other)

api/include/opentelemetry/trace/span_context_kv_iterable_view.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ namespace trace
2424
namespace detail
2525
{
2626
template <class T>
27-
inline void take_span_context_kv(SpanContext, opentelemetry::common::KeyValueIterableView<T>)
27+
inline void take_span_context_kv(const SpanContext &,
28+
opentelemetry::common::KeyValueIterableView<T>)
2829
{}
2930

3031
template <class T, nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
31-
inline void take_span_context_kv(SpanContext, T &)
32+
inline void take_span_context_kv(const SpanContext &, T &)
3233
{}
3334

3435
inline void take_span_context_kv(
35-
SpanContext,
36+
const SpanContext &,
3637
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>)
3738
{}
3839

@@ -81,7 +82,7 @@ class SpanContextKeyValueIterableView final : public SpanContextKeyValueIterable
8182
private:
8283
const T *container_;
8384

84-
bool do_callback(SpanContext span_context,
85+
bool do_callback(const SpanContext &span_context,
8586
const common::KeyValueIterable &attributes,
8687
nostd::function_ref<bool(SpanContext, const common::KeyValueIterable &)>
8788
callback) const noexcept
@@ -95,7 +96,7 @@ class SpanContextKeyValueIterableView final : public SpanContextKeyValueIterable
9596

9697
template <class U,
9798
nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
98-
bool do_callback(SpanContext span_context,
99+
bool do_callback(const SpanContext &span_context,
99100
const U &attributes,
100101
nostd::function_ref<bool(SpanContext, const common::KeyValueIterable &)>
101102
callback) const noexcept
@@ -104,7 +105,7 @@ class SpanContextKeyValueIterableView final : public SpanContextKeyValueIterable
104105
}
105106

106107
bool do_callback(
107-
SpanContext span_context,
108+
const SpanContext &span_context,
108109
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
109110
nostd::function_ref<bool(SpanContext, const common::KeyValueIterable &)> callback)
110111
const noexcept

examples/environment_carrier/main.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ void RunChild()
107107
opts.parent = parent_ctx;
108108
auto child_span = tracer->StartSpan("child-operation", opts);
109109

110-
std::cout << "[child] trace_id: " << Hex(child_span->GetContext().trace_id()) << std::endl;
111-
std::cout << "[child] span_id: " << Hex(child_span->GetContext().span_id()) << std::endl;
110+
std::cout << "[child] trace_id: " << Hex(child_span->GetContext().trace_id()) << '\n';
111+
std::cout << "[child] span_id: " << Hex(child_span->GetContext().span_id()) << '\n';
112+
std::cout << std::flush;
112113

113114
child_span->End();
114115
CleanupTracer();
@@ -124,8 +125,9 @@ void RunParent()
124125
auto root_span = tracer->StartSpan("parent-operation");
125126
auto scope = trace_api::Scope(root_span);
126127

127-
std::cout << "[parent] trace_id: " << Hex(root_span->GetContext().trace_id()) << std::endl;
128-
std::cout << "[parent] span_id: " << Hex(root_span->GetContext().span_id()) << std::endl;
128+
std::cout << "[parent] trace_id: " << Hex(root_span->GetContext().trace_id()) << '\n';
129+
std::cout << "[parent] span_id: " << Hex(root_span->GetContext().span_id()) << '\n';
130+
std::cout << std::flush;
129131

130132
// Inject context into a map via EnvironmentCarrier
131133
auto env_map = std::make_shared<std::map<std::string, std::string>>();
@@ -155,7 +157,7 @@ void RunParent()
155157
}
156158
else
157159
{
158-
std::cerr << "fork() failed" << std::endl;
160+
std::cerr << "fork() failed\n";
159161
}
160162

161163
root_span->End();

exporters/otlp/test/otlp_grpc_metric_exporter_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ class OtlpGrpcMetricExporterTestPeer : public ::testing::Test
7272
const std::shared_ptr<OtlpGrpcClient> &client)
7373
{
7474
return std::unique_ptr<sdk::metrics::PushMetricExporter>(
75-
new OtlpGrpcMetricExporter(std::move(stub_interface), std::move(client)));
75+
new OtlpGrpcMetricExporter(std::move(stub_interface), client));
7676
}
7777

7878
std::unique_ptr<sdk::metrics::PushMetricExporter> GetExporter(
7979
const OtlpGrpcMetricExporterOptions &options,
8080
const std::shared_ptr<OtlpGrpcClient> &client)
8181
{
8282
return std::unique_ptr<sdk::metrics::PushMetricExporter>(
83-
new OtlpGrpcMetricExporter(options, std::move(client)));
83+
new OtlpGrpcMetricExporter(options, client));
8484
}
8585

8686
// Get the options associated with the given exporter.

sdk/include/opentelemetry/sdk/instrumentationscope/scope_configurator.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ScopeConfigurator
4949
Builder &AddCondition(std::function<bool(const InstrumentationScope &)> scope_matcher,
5050
T scope_config)
5151
{
52-
conditions_.emplace_back(std::move(scope_matcher), scope_config);
52+
conditions_.emplace_back(std::move(scope_matcher), std::move(scope_config));
5353
return *this;
5454
}
5555

@@ -67,7 +67,7 @@ class ScopeConfigurator
6767
[scope_name = std::string(scope_name)](const InstrumentationScope &scope_info) {
6868
return scope_info.GetName() == scope_name;
6969
};
70-
conditions_.emplace_back(name_equals_matcher, scope_config);
70+
conditions_.emplace_back(std::move(name_equals_matcher), std::move(scope_config));
7171
return *this;
7272
}
7373

@@ -112,8 +112,8 @@ class ScopeConfigurator
112112
std::function<bool(const InstrumentationScope &)> scope_matcher;
113113
T scope_config;
114114

115-
Condition(const std::function<bool(const InstrumentationScope &)> &matcher, const T &config)
116-
: scope_matcher(matcher), scope_config(config)
115+
Condition(std::function<bool(const InstrumentationScope &)> matcher, T config)
116+
: scope_matcher(std::move(matcher)), scope_config(std::move(config))
117117
{}
118118
};
119119

0 commit comments

Comments
 (0)