From 2fef5f704ab8cb3fb8f5643a23ae36d32969020d Mon Sep 17 00:00:00 2001 From: Tom Jackson Date: Mon, 27 Nov 2017 17:13:37 -0800 Subject: [PATCH] Use "auto" with GENERATOR instead of std::function Summary: Now that we're using C++14 more broadly, we should use it to improve generator performance. This speeds up a microbenchmark >5x by removing type erasure. Reviewed By: philippv Differential Revision: D6398730 fbshipit-source-id: 5809058a3b5ff0e66fd4b1e8954698944e1a7d09 --- folly/gen/Base.h | 5 ++--- folly/gen/test/BaseBenchmark.cpp | 23 +++++++++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/folly/gen/Base.h b/folly/gen/Base.h index a5503935..dd74a092 100644 --- a/folly/gen/Base.h +++ b/folly/gen/Base.h @@ -503,9 +503,8 @@ Yield generator(Source&& source) { * * auto gen = GENERATOR(int) { yield(1); yield(2); }; */ -#define GENERATOR(TYPE) \ - ::folly::gen::detail::GeneratorBuilder() + \ - [=](const std::function& yield) +#define GENERATOR(TYPE) \ + ::folly::gen::detail::GeneratorBuilder() + [=](auto&& yield) /* * empty() - for producing empty sequences. diff --git a/folly/gen/test/BaseBenchmark.cpp b/folly/gen/test/BaseBenchmark.cpp index f11fef0f..51058b71 100644 --- a/folly/gen/test/BaseBenchmark.cpp +++ b/folly/gen/test/BaseBenchmark.cpp @@ -140,11 +140,11 @@ BENCHMARK_DRAW_LINE() BENCHMARK(Fib_Sum_NoGen, iters) { int s = 0; while (iters--) { - auto fib = [](int limit) -> vector { + auto fib = [](size_t limit) -> vector { vector ret; int a = 0; int b = 1; - for (int i = 0; i * 2 < limit; ++i) { + for (size_t i = 0; i < limit; i += 2) { ret.push_back(a += b); ret.push_back(b += a); } @@ -168,11 +168,30 @@ BENCHMARK_RELATIVE(Fib_Sum_Gen, iters) { yield(b += a); } }; + // Early stopping implemented with exceptions. s += fib | take(testSize.load()) | sum; } folly::doNotOptimizeAway(s); } +BENCHMARK_RELATIVE(Fib_Sum_Gen_Limit, iters) { + int s = 0; + while (iters--) { + size_t limit = testSize.load(); + auto fib = GENERATOR(int) { + int a = 0; + int b = 1; + for (size_t i = 0; i < limit; i += 2) { + yield(a += b); + yield(b += a); + } + }; + // No early stopping. + s += fib | sum; + } + folly::doNotOptimizeAway(s); +} + struct FibYielder { template void operator()(Yield&& yield) const { -- 2.34.1