2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 /* -*- Mode: C++; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
18 #include <folly/experimental/ReadMostlySharedPtr.h>
23 #include <folly/Benchmark.h>
24 #include <folly/Memory.h>
25 #include <folly/experimental/RCURefCount.h>
26 #include <folly/portability/GFlags.h>
28 template <template<typename> class MainPtr,
29 template<typename> class WeakPtr,
31 void benchmark(size_t n) {
32 MainPtr<int> mainPtr(std::make_unique<int>(42));
34 std::vector<std::thread> ts;
36 for (size_t t = 0; t < threadCount; ++t) {
37 ts.emplace_back([&]() {
38 WeakPtr<int> weakPtr(mainPtr);
40 for (size_t i = 0; i < n; ++i) {
52 using RCUMainPtr = folly::ReadMostlyMainPtr<T, folly::RCURefCount>;
54 using RCUWeakPtr = folly::ReadMostlyWeakPtr<T, folly::RCURefCount>;
56 using TLMainPtr = folly::ReadMostlyMainPtr<T, folly::TLRefCount>;
58 using TLWeakPtr = folly::ReadMostlyWeakPtr<T, folly::TLRefCount>;
61 BENCHMARK(WeakPtrOneThread, n) {
62 benchmark<std::shared_ptr, std::weak_ptr, 1>(n);
65 BENCHMARK(WeakPtrFourThreads, n) {
66 benchmark<std::shared_ptr, std::weak_ptr, 4>(n);
69 BENCHMARK(RCUReadMostlyWeakPtrOneThread, n) {
70 benchmark<RCUMainPtr, RCUWeakPtr, 1>(n);
73 BENCHMARK(RCUReadMostlyWeakPtrFourThreads, n) {
74 benchmark<RCUMainPtr, RCUWeakPtr, 4>(n);
77 BENCHMARK(TLReadMostlyWeakPtrOneThread, n) {
78 benchmark<TLMainPtr, TLWeakPtr, 1>(n);
81 BENCHMARK(TLReadMostlyWeakPtrFourThreads, n) {
82 benchmark<TLMainPtr, TLWeakPtr, 4>(n);
85 int main(int argc, char** argv) {
86 gflags::ParseCommandLineFlags(&argc, &argv, true);
87 gflags::SetCommandLineOptionWithMode(
88 "bm_min_usec", "100000", gflags::SET_FLAG_IF_DEFAULT
91 folly::runBenchmarks();