summaryrefslogtreecommitdiff
path: root/absl/container/inlined_vector_benchmark.cc
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2018-06-20 06:25:23 -0700
committerGravatar Shaindel Schwartz <shaindel@google.com>2018-06-20 10:15:31 -0400
commit6c7de165d1c82684359ccb630bb5f83263fa5ebc (patch)
treef271bc0005ad42773cc0140100699b8bfbaea124 /absl/container/inlined_vector_benchmark.cc
parente5be80532b5d998813f9db952d2cc5401b1532df (diff)
Project import generated by Copybara.20180600
GitOrigin-RevId: d89dba27e35462d7457121b978fd79214205e686 Change-Id: I0eae80578a93a580820bc90d42e6b42faf7fde0a
Diffstat (limited to 'absl/container/inlined_vector_benchmark.cc')
-rw-r--r--absl/container/inlined_vector_benchmark.cc37
1 files changed, 24 insertions, 13 deletions
diff --git a/absl/container/inlined_vector_benchmark.cc b/absl/container/inlined_vector_benchmark.cc
index 5977bc93..24f21749 100644
--- a/absl/container/inlined_vector_benchmark.cc
+++ b/absl/container/inlined_vector_benchmark.cc
@@ -63,18 +63,34 @@ void BM_StdVectorFill(benchmark::State& state) {
}
BENCHMARK(BM_StdVectorFill)->Range(0, 1024);
+// The purpose of the next two benchmarks is to verify that
+// absl::InlinedVector is efficient when moving is more efficent than
+// copying. To do so, we use strings that are larger than the short
+// std::string optimization.
bool StringRepresentedInline(std::string s) {
const char* chars = s.data();
std::string s1 = std::move(s);
return s1.data() != chars;
}
+int GetNonShortStringOptimizationSize() {
+ for (int i = 24; i <= 192; i *= 2) {
+ if (!StringRepresentedInline(std::string(i, 'A'))) {
+ return i;
+ }
+ }
+ ABSL_RAW_LOG(
+ FATAL,
+ "Failed to find a std::string larger than the short std::string optimization");
+ return -1;
+}
+
void BM_InlinedVectorFillString(benchmark::State& state) {
const int len = state.range(0);
- std::string strings[4] = {"a quite long string",
- "another long string",
- "012345678901234567",
- "to cause allocation"};
+ const int no_sso = GetNonShortStringOptimizationSize();
+ std::string strings[4] = {std::string(no_sso, 'A'), std::string(no_sso, 'B'),
+ std::string(no_sso, 'C'), std::string(no_sso, 'D')};
+
for (auto _ : state) {
absl::InlinedVector<std::string, 8> v;
for (int i = 0; i < len; i++) {
@@ -87,10 +103,10 @@ BENCHMARK(BM_InlinedVectorFillString)->Range(0, 1024);
void BM_StdVectorFillString(benchmark::State& state) {
const int len = state.range(0);
- std::string strings[4] = {"a quite long string",
- "another long string",
- "012345678901234567",
- "to cause allocation"};
+ const int no_sso = GetNonShortStringOptimizationSize();
+ std::string strings[4] = {std::string(no_sso, 'A'), std::string(no_sso, 'B'),
+ std::string(no_sso, 'C'), std::string(no_sso, 'D')};
+
for (auto _ : state) {
std::vector<std::string> v;
for (int i = 0; i < len; i++) {
@@ -98,11 +114,6 @@ void BM_StdVectorFillString(benchmark::State& state) {
}
}
state.SetItemsProcessed(static_cast<int64_t>(state.iterations()) * len);
- // The purpose of the benchmark is to verify that inlined vector is
- // efficient when moving is more efficent than copying. To do so, we
- // use strings that are larger than the small std::string optimization.
- ABSL_RAW_CHECK(!StringRepresentedInline(strings[0]),
- "benchmarked with strings that are too small");
}
BENCHMARK(BM_StdVectorFillString)->Range(0, 1024);