Skip to content

Commit d882c6b

Browse files
authored
[SDK] Misc cleanup in attribute_utils.h (#3716)
- prevent superfluous copy in construction of OwnedAttributeValue with std::string or span - combined identical lambdas of both lamdas - use const-ref - removed unneeded (stateless) members
1 parent 3efc14b commit d882c6b

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

sdk/include/opentelemetry/sdk/common/attribute_utils.h

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct AttributeConverter
8383
{
8484
return OwnedAttributeValue(std::string(v));
8585
}
86-
OwnedAttributeValue operator()(std::string v) { return OwnedAttributeValue(v); }
86+
OwnedAttributeValue operator()(std::string v) { return OwnedAttributeValue(std::move(v)); }
8787
OwnedAttributeValue operator()(const char *v) { return OwnedAttributeValue(std::string(v)); }
8888
OwnedAttributeValue operator()(nostd::span<const uint8_t> v) { return convertSpan<uint8_t>(v); }
8989
OwnedAttributeValue operator()(nostd::span<const bool> v) { return convertSpan<bool>(v); }
@@ -100,8 +100,7 @@ struct AttributeConverter
100100
template <typename T, typename U = T>
101101
OwnedAttributeValue convertSpan(nostd::span<const U> vals)
102102
{
103-
const std::vector<T> copy(vals.begin(), vals.end());
104-
return OwnedAttributeValue(std::move(copy));
103+
return OwnedAttributeValue(std::vector<T>(vals.begin(), vals.end()));
105104
}
106105
};
107106

@@ -171,23 +170,15 @@ class AttributeMap : public std::unordered_map<std::string, OwnedAttributeValue>
171170
// Construct attribute map and populate with attributes
172171
AttributeMap(const opentelemetry::common::KeyValueIterable &attributes) : AttributeMap()
173172
{
174-
attributes.ForEachKeyValue(
175-
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {
176-
SetAttribute(key, value);
177-
return true;
178-
});
173+
ConstructFrom(attributes);
179174
}
180175

181176
// Construct attribute map and populate with optional attributes
182177
AttributeMap(const opentelemetry::common::KeyValueIterable *attributes) : AttributeMap()
183178
{
184179
if (attributes != nullptr)
185180
{
186-
attributes->ForEachKeyValue(
187-
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {
188-
SetAttribute(key, value);
189-
return true;
190-
});
181+
ConstructFrom(*attributes);
191182
}
192183
}
193184

@@ -203,6 +194,15 @@ class AttributeMap : public std::unordered_map<std::string, OwnedAttributeValue>
203194
}
204195
}
205196

197+
void ConstructFrom(const opentelemetry::common::KeyValueIterable &attributes)
198+
{
199+
attributes.ForEachKeyValue(
200+
[&](nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept {
201+
SetAttribute(key, value);
202+
return true;
203+
});
204+
}
205+
206206
// Returns a reference to this map
207207
const std::unordered_map<std::string, OwnedAttributeValue> &GetAttributes() const noexcept
208208
{
@@ -213,7 +213,7 @@ class AttributeMap : public std::unordered_map<std::string, OwnedAttributeValue>
213213
void SetAttribute(nostd::string_view key,
214214
const opentelemetry::common::AttributeValue &value) noexcept
215215
{
216-
(*this)[std::string(key)] = nostd::visit(converter_, value);
216+
(*this)[std::string(key)] = nostd::visit(AttributeConverter(), value);
217217
}
218218

219219
// Compare the attributes of this map with another KeyValueIterable
@@ -235,18 +235,14 @@ class AttributeMap : public std::unordered_map<std::string, OwnedAttributeValue>
235235
{
236236
// Order of arguments is important here. OwnedAttributeValue is first then
237237
// AttributeValue AttributeEqualToVisitor does not support the reverse order
238-
return nostd::visit(equal_to_visitor_, kv.second, value);
238+
return nostd::visit(AttributeEqualToVisitor(), kv.second, value);
239239
}
240240
}
241241
return false;
242242
});
243243

244244
return is_equal;
245245
}
246-
247-
private:
248-
AttributeConverter converter_;
249-
AttributeEqualToVisitor equal_to_visitor_;
250246
};
251247

252248
/**
@@ -291,11 +287,8 @@ class OrderedAttributeMap : public std::map<std::string, OwnedAttributeValue>
291287
void SetAttribute(nostd::string_view key,
292288
const opentelemetry::common::AttributeValue &value) noexcept
293289
{
294-
(*this)[std::string(key)] = nostd::visit(converter_, value);
290+
(*this)[std::string(key)] = nostd::visit(AttributeConverter(), value);
295291
}
296-
297-
private:
298-
AttributeConverter converter_;
299292
};
300293

301294
} // namespace common

0 commit comments

Comments
 (0)