Commit 31563f4c authored by Margot Leibold's avatar Margot Leibold Committed by Facebook GitHub Bot

constructor benchmark

Summary: Construction of ReadMostlyMainPtr is much slower than shared_ptr

Reviewed By: yfeldblum

Differential Revision: D24350176

fbshipit-source-id: 55730192f79288e7ad848b4cec6483bc0c1711b6
parent 50e9d1bb
...@@ -51,6 +51,39 @@ void benchmark(size_t n) { ...@@ -51,6 +51,39 @@ void benchmark(size_t n) {
} }
} }
template <template <typename> class MainPtr>
void constructorBenchmark(size_t n) {
folly::BenchmarkSuspender braces;
using deleter_fn = folly::identity_fn; // noop deleter
using uptr = std::unique_ptr<int, deleter_fn>;
int data = 42;
std::vector<MainPtr<int>> ptrs;
ptrs.reserve(n);
// Only measure the cost of constructing the MainPtr
braces.dismissing([&] {
for (size_t i = 0; i < n; ++i) {
ptrs.push_back(MainPtr<int>(uptr(&data)));
}
});
}
template <template <typename> class MainPtr>
void destructorBenchmark(size_t n) {
folly::BenchmarkSuspender braces;
std::vector<MainPtr<int>> ptrs;
using deleter_fn = folly::identity_fn; // noop deleter
using uptr = std::unique_ptr<int, deleter_fn>;
int data = 42;
ptrs.reserve(n);
for (size_t i = 0; i < n; ++i) {
ptrs.push_back(MainPtr<int>(uptr(&data)));
}
// We only want to measure cost of destructing the MainPtr
braces.dismissing([&] { ptrs.clear(); });
}
template <typename T> template <typename T>
using TLMainPtr = folly::ReadMostlyMainPtr<T, folly::TLRefCount>; using TLMainPtr = folly::ReadMostlyMainPtr<T, folly::TLRefCount>;
template <typename T> template <typename T>
...@@ -60,16 +93,38 @@ BENCHMARK(WeakPtrOneThread, n) { ...@@ -60,16 +93,38 @@ BENCHMARK(WeakPtrOneThread, n) {
benchmark<std::shared_ptr, std::weak_ptr, 1>(n); benchmark<std::shared_ptr, std::weak_ptr, 1>(n);
} }
BENCHMARK_RELATIVE(TLReadMostlyWeakPtrOneThread, n) {
benchmark<TLMainPtr, TLWeakPtr, 1>(n);
}
BENCHMARK(WeakPtrFourThreads, n) { BENCHMARK(WeakPtrFourThreads, n) {
benchmark<std::shared_ptr, std::weak_ptr, 4>(n); benchmark<std::shared_ptr, std::weak_ptr, 4>(n);
} }
BENCHMARK(TLReadMostlyWeakPtrOneThread, n) { BENCHMARK_RELATIVE(TLReadMostlyWeakPtrFourThreads, n) {
benchmark<TLMainPtr, TLWeakPtr, 1>(n); benchmark<TLMainPtr, TLWeakPtr, 4>(n);
} }
BENCHMARK(TLReadMostlyWeakPtrFourThreads, n) { /**
benchmark<TLMainPtr, TLWeakPtr, 4>(n); * ReadMostlyMainPtr construction/destruction is significantly more expensive
* than std::shared_ptr. You should consider using ReadMostly pointers if the
* MainPtr is created infrequently but shared pointers are copied frequently.
*/
BENCHMARK(SharedPtrCtor, n) {
constructorBenchmark<std::shared_ptr>(n);
}
BENCHMARK_RELATIVE(TLReadMostlyMainPtrCtor, n) {
constructorBenchmark<TLMainPtr>(n);
}
BENCHMARK(SharedPtrDtor, n) {
destructorBenchmark<std::shared_ptr>(n);
}
BENCHMARK_RELATIVE(TLReadMostlyMainPtrDtor, n) {
destructorBenchmark<TLMainPtr>(n);
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
......
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