Commit cbfe51b2 by Valery Mironov Committed by Copybara-Service

PR #1672: Optimize StrJoin with tuple without user defined formatter

Imported from GitHub PR https://github.com/abseil/abseil-cpp/pull/1672

https://github.com/abseil/abseil-cpp/discussions/1671

Merge ddcbb2466b2c9c4048d60be7e58cf47f935c257d into eba8db7b

Merging this change closes #1672

COPYBARA_INTEGRATE_REVIEW=https://github.com/abseil/abseil-cpp/pull/1672 from MBkkt:optimize-str-join ddcbb2466b2c9c4048d60be7e58cf47f935c257d
PiperOrigin-RevId: 633988391
Change-Id: I2b3904211a29de3a768fb90a7fc106d7ff6c03e7
parent 6683a617
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <initializer_list>
#include <iterator> #include <iterator>
#include <limits> #include <limits>
#include <memory> #include <memory>
...@@ -321,6 +322,15 @@ std::string JoinRange(const Range& range, absl::string_view separator) { ...@@ -321,6 +322,15 @@ std::string JoinRange(const Range& range, absl::string_view separator) {
return JoinRange(begin(range), end(range), separator); return JoinRange(begin(range), end(range), separator);
} }
template <typename Tuple, std::size_t... I>
std::string JoinTuple(const Tuple& value, absl::string_view separator,
std::index_sequence<I...>) {
return JoinRange(
std::initializer_list<absl::string_view>{
static_cast<const AlphaNum&>(std::get<I>(value)).Piece()...},
separator);
}
} // namespace strings_internal } // namespace strings_internal
ABSL_NAMESPACE_END ABSL_NAMESPACE_END
} // namespace absl } // namespace absl
......
...@@ -291,7 +291,8 @@ inline std::string StrJoin(std::initializer_list<absl::string_view> il, ...@@ -291,7 +291,8 @@ inline std::string StrJoin(std::initializer_list<absl::string_view> il,
template <typename... T> template <typename... T>
std::string StrJoin(const std::tuple<T...>& value, std::string StrJoin(const std::tuple<T...>& value,
absl::string_view separator) { absl::string_view separator) {
return strings_internal::JoinAlgorithm(value, separator, AlphaNumFormatter()); return strings_internal::JoinTuple(value, separator,
std::index_sequence_for<T...>{});
} }
ABSL_NAMESPACE_END ABSL_NAMESPACE_END
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "absl/strings/str_join.h" #include "absl/strings/str_join.h"
#include <string> #include <string>
#include <tuple>
#include <vector> #include <vector>
#include <utility> #include <utility>
...@@ -94,4 +95,13 @@ BENCHMARK(BM_JoinStreamable) ...@@ -94,4 +95,13 @@ BENCHMARK(BM_JoinStreamable)
->ArgPair(16, 256) ->ArgPair(16, 256)
->ArgPair(256, 256); ->ArgPair(256, 256);
void BM_JoinTuple(benchmark::State& state) {
for (auto _ : state) {
std::string s =
absl::StrJoin(std::make_tuple(123456789, 987654321, 24680, 13579), "/");
benchmark::DoNotOptimize(s);
}
}
BENCHMARK(BM_JoinTuple);
} // namespace } // namespace
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment