<commit_before>/*
 * Copyright 2018-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <folly/Benchmark.h>
#include <folly/Function.h>
#include <folly/Random.h>
#include <folly/synchronization/detail/InlineFunctionRef.h>

#include <cstdint>
#include <functional>
#include <memory>
#include <tuple>
#include <type_traits>

/**
 * ============================================================================
 * folly/test/FunctionRefBenchmark.cpp             relative  time/iter  iters/s
 * ============================================================================
 * SmallFunctionFunctionPointerInvoke                           3.02ns  331.34M
 * SmallFunctionStdFunctionInvoke                               3.02ns  331.34M
 * SmallFunctionStdFunctionWithReferenceWrapperInv              3.02ns  331.33M
 * SmallFunctionFollyFunctionInvoke                             3.02ns  331.30M
 * SmallFunctionFollyFunctionRefInvoke                          3.02ns  331.34M
 * ----------------------------------------------------------------------------
 * SmallFunctionFunctionPointerCreateInvoke                     3.02ns  331.34M
 * SmallFunctionStdFunctionCreateInvoke                         3.76ns  266.24M
 * SmallFunctionStdFunctionReferenceWrapperCreateI              4.77ns  209.52M
 * SmallFunctionFollyFunctionCreateInvoke                       3.24ns  308.40M
 * SmallFunctionFollyFunctionRefCreateInvoke                    3.02ns  331.34M
 * ----------------------------------------------------------------------------
 * BigFunctionStdFunctionInvoke                                 3.02ns  330.74M
 * BigFunctionStdFunctionReferenceWrapperInvoke                 3.02ns  330.74M
 * BigFunctionFollyFunctionInvoke                               3.02ns  331.31M
 * BigFunctionFollyFunctionRefInvoke                            3.02ns  331.31M
 * ----------------------------------------------------------------------------
 * BigFunctionStdFunctionCreateInvoke                          43.87ns   22.79M
 * BigFunctionStdFunctionReferenceWrapperCreateInv              4.20ns  238.22M
 * BigFunctionFollyFunctionCreateInvoke                        43.01ns   23.25M
 * BigFunctionFollyFunctionRefCreateInvoke                      3.02ns  331.31M
 * ============================================================================
 */

namespace folly {
namespace {
template <typename MakeFunction>
void runSmallInvokeBenchmark(std::size_t iters, MakeFunction make) {
  auto lambda = [](auto& i) {
    folly::makeUnpredictable(i);
    return i;
  };
  folly::makeUnpredictable(lambda);
  auto func = make(lambda);
  folly::makeUnpredictable(func);

  for (auto i = std::size_t{iters}; --i;) {
    folly::doNotOptimizeAway(func(i));
  }
}

template <typename MakeFunction>
void runSmallCreateAndInvokeBenchmark(std::size_t iters, MakeFunction make) {
  auto lambda = [](auto& i) {
    folly::makeUnpredictable(i);
    return i;
  };
  folly::makeUnpredictable(lambda);

  for (auto i = std::size_t{iters}; --i;) {
    auto func = make(lambda);
    folly::makeUnpredictable(func);
    folly::doNotOptimizeAway(func(i));
  }
}

template <typename MakeFunction>
void runBigAndInvokeBenchmark(std::size_t iters, MakeFunction make) {
  auto suspender = BenchmarkSuspender{};
  auto array = std::array<std::uint8_t, 4096>{};

  auto lambda = [=](auto& i) {
    // we use std::ignore to ensure ODR-usage of array
    std::ignore = array;
    folly::makeUnpredictable(i);
    return i;
  };
  folly::makeUnpredictable(lambda);
  auto func = make(lambda);
  folly::makeUnpredictable(func);

  suspender.dismissing([&] {
    for (auto i = std::size_t{iters}; --i;) {
      folly::doNotOptimizeAway(func(i));
    }
  });
}

template <typename MakeFunction>
void runBigCreateAndInvokeBenchmark(std::size_t iters, MakeFunction make) {
  auto suspender = BenchmarkSuspender{};
  auto array = std::array<std::uint8_t, 1024>{};
  folly::makeUnpredictable(array);

  auto lambda = [=](auto& i) {
    folly::doNotOptimizeAway(array);
    folly::makeUnpredictable(i);
    return i;
  };
  folly::makeUnpredictable(lambda);

  suspender.dismissing([&] {
    for (auto i = std::size_t{iters}; --i;) {
      auto func = make(lambda);
      folly::makeUnpredictable(func);
      folly::doNotOptimizeAway(func(i));
    }
  });
}
} // namespace

BENCHMARK(SmallFunctionFunctionPointerInvoke, iters) {
  using FPtr = size_t (*)(size_t&);
  runSmallInvokeBenchmark(iters, [](auto& f) { return FPtr{f}; });
}
BENCHMARK(SmallFunctionStdFunctionInvoke, iters) {
  runSmallInvokeBenchmark(
      iters, [](auto& f) { return std::function<size_t(size_t&)>{f}; });
}
BENCHMARK(SmallFunctionStdFunctionWithReferenceWrapperInvoke, iters) {
  runSmallInvokeBenchmark(iters, [](auto& f) {
    return std::function<size_t(size_t&)>{std::ref(f)};
  });
}
BENCHMARK(SmallFunctionFollyFunctionInvoke, iters) {
  runSmallInvokeBenchmark(
      iters, [](auto& f) { return folly::Function<size_t(size_t&)>{f}; });
}
BENCHMARK(SmallFunctionFollyFunctionRefInvoke, iters) {<commit_msg>Cut no-op same-type casts in Function benchmark<commit_after>/*
 * Copyright 2018-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <folly/Benchmark.h>
#include <folly/Function.h>
#include <folly/Random.h>
#include <folly/synchronization/detail/InlineFunctionRef.h>

#include <cstdint>
#include <functional>
#include <memory>
#include <tuple>
#include <type_traits>

/**
 * ============================================================================
 * folly/test/FunctionRefBenchmark.cpp             relative  time/iter  iters/s
 * ============================================================================
 * SmallFunctionFunctionPointerInvoke                           3.02ns  331.34M
 * SmallFunctionStdFunctionInvoke                               3.02ns  331.34M
 * SmallFunctionStdFunctionWithReferenceWrapperInv              3.02ns  331.33M
 * SmallFunctionFollyFunctionInvoke                             3.02ns  331.30M
 * SmallFunctionFollyFunctionRefInvoke                          3.02ns  331.34M
 * ----------------------------------------------------------------------------
 * SmallFunctionFunctionPointerCreateInvoke                     3.02ns  331.34M
 * SmallFunctionStdFunctionCreateInvoke                         3.76ns  266.24M
 * SmallFunctionStdFunctionReferenceWrapperCreateI              4.77ns  209.52M
 * SmallFunctionFollyFunctionCreateInvoke                       3.24ns  308.40M
 * SmallFunctionFollyFunctionRefCreateInvoke                    3.02ns  331.34M
 * ----------------------------------------------------------------------------
 * BigFunctionStdFunctionInvoke                                 3.02ns  330.74M
 * BigFunctionStdFunctionReferenceWrapperInvoke                 3.02ns  330.74M
 * BigFunctionFollyFunctionInvoke                               3.02ns  331.31M
 * BigFunctionFollyFunctionRefInvoke                            3.02ns  331.31M
 * ----------------------------------------------------------------------------
 * BigFunctionStdFunctionCreateInvoke                          43.87ns   22.79M
 * BigFunctionStdFunctionReferenceWrapperCreateInv              4.20ns  238.22M
 * BigFunctionFollyFunctionCreateInvoke                        43.01ns   23.25M
 * BigFunctionFollyFunctionRefCreateInvoke                      3.02ns  331.31M
 * ============================================================================
 */

namespace folly {
namespace {
template <typename MakeFunction>
void runSmallInvokeBenchmark(std::size_t iters, MakeFunction make) {
  auto lambda = [](auto& i) {
    folly::makeUnpredictable(i);
    return i;
  };
  folly::makeUnpredictable(lambda);
  auto func = make(lambda);
  folly::makeUnpredictable(func);

  for (auto i = iters; --i;) {
    folly::doNotOptimizeAway(func(i));
  }
}

template <typename MakeFunction>
void runSmallCreateAndInvokeBenchmark(std::size_t iters, MakeFunction make) {
  auto lambda = [](auto& i) {
    folly::makeUnpredictable(i);
    return i;
  };
  folly::makeUnpredictable(lambda);

  for (auto i = iters; --i;) {
    auto func = make(lambda);
    folly::makeUnpredictable(func);
    folly::doNotOptimizeAway(func(i));
  }
}

template <typename MakeFunction>
void runBigAndInvokeBenchmark(std::size_t iters, MakeFunction make) {
  auto suspender = BenchmarkSuspender{};
  auto array = std::array<std::uint8_t, 4096>{};

  auto lambda = [=](auto& i) {
    // we use std::ignore to ensure ODR-usage of array
    std::ignore = array;
    folly::makeUnpredictable(i);
    return i;
  };
  folly::makeUnpredictable(lambda);
  auto func = make(lambda);
  folly::makeUnpredictable(func);

  suspender.dismissing([&] {
    for (auto i = iters; --i;) {
      folly::doNotOptimizeAway(func(i));
    }
  });
}

template <typename MakeFunction>
void runBigCreateAndInvokeBenchmark(std::size_t iters, MakeFunction make) {
  auto suspender = BenchmarkSuspender{};
  auto array = std::array<std::uint8_t, 1024>{};
  folly::makeUnpredictable(array);

  auto lambda = [=](auto& i) {
    folly::doNotOptimizeAway(array);
    folly::makeUnpredictable(i);
    return i;
  };
  folly::makeUnpredictable(lambda);

  suspender.dismissing([&] {
    for (auto i = iters; --i;) {
      auto func = make(lambda);
      folly::makeUnpredictable(func);
      folly::doNotOptimizeAway(func(i));
    }
  });
}
} // namespace

BENCHMARK(SmallFunctionFunctionPointerInvoke, iters) {
  using FPtr = size_t (*)(size_t&);
  runSmallInvokeBenchmark(iters, [](auto& f) { return FPtr{f}; });
}
BENCHMARK(SmallFunctionStdFunctionInvoke, iters) {
  runSmallInvokeBenchmark(
      iters, [](auto& f) { return std::function<size_t(size_t&)>{f}; });
}
BENCHMARK(SmallFunctionStdFunctionWithReferenceWrapperInvoke, iters) {
  runSmallInvokeBenchmark(iters, [](auto& f) {
    return std::function<size_t(size_t&)>{std::ref(f)};
  });
}
BENCHMARK(SmallFunctionFollyFunctionInvoke, iters) {
  runSmallInvokeBenchmark(
      iters, [](auto& f) { return folly::Function<size_t(size_t&)>{f}; });
}
BENCHMARK(SmallFunctionFollyFunctionRefInvoke, iters) {
{'fraction_non_alphanumeric': 0.18379446640316205, 'fraction_numerical': 0.029105282069708947, 'mean_word_length': 2.856252164877035, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6326443', 'n_tokens_mistral': 3457, 'n_tokens_neox': 3200, 'n_words': 668}