From 7ed8c613d1881ade6e1b524920d4b40c84549bf3 Mon Sep 17 00:00:00 2001 From: Alexey Spiridonov Date: Thu, 9 Nov 2023 10:47:53 -0800 Subject: [PATCH] Simple comparison of AsyncGenerator and Generator "hot path" performance Summary: I was curious what the overhead was for using `AsyncGenerator` when producing cheap values. Reviewed By: ibrahimjirdeh Differential Revision: D51139076 fbshipit-source-id: a843a68e09ae0f22fc1d498519ccf7f1d3ab16b1 --- .../coro/test/AsyncGeneratorBenchmark.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/folly/experimental/coro/test/AsyncGeneratorBenchmark.cpp b/folly/experimental/coro/test/AsyncGeneratorBenchmark.cpp index 18844fa3c2e..f415067c189 100644 --- a/folly/experimental/coro/test/AsyncGeneratorBenchmark.cpp +++ b/folly/experimental/coro/test/AsyncGeneratorBenchmark.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -102,6 +103,45 @@ BENCHMARK(asyncGeneratorYieldErrorAwaitTry, iters) { }()); } +/* +Comparing just the "hot paths" of the two generator coroutines: + +11/08/23 16:25$ buck2 run @mode/opt \ + //folly/experimental/coro/test:async_generator_bench -- \ + -bm_regex '.*YieldValue.*' +============================================================================ +[...]coro/test/AsyncGeneratorBenchmark.cpp relative time/iter iters/s +============================================================================ +asyncGeneratorYieldValues 12.00ns 83.32M +compareToSynchronousGeneratorYieldValues 4.43ns 225.72M +*/ + +BENCHMARK(asyncGeneratorYieldValues, iters) { + folly::coro::blockingWait([&]() -> folly::coro::Task { + auto gen = [](size_t iters) -> folly::coro::AsyncGenerator { + for (size_t iter = 0; iter < iters; ++iter) { + co_yield iter; + } + }(iters); + size_t i = 0; + while (auto it = co_await gen.next()) { + CHECK_EQ(i++, *it); + } + }()); +} + +BENCHMARK(compareToSynchronousGeneratorYieldValues, iters) { + auto gen = [](size_t iters) -> folly::coro::Generator { + for (size_t iter = 0; iter < iters; ++iter) { + co_yield iter; + } + }(iters); + size_t i = 0; + for (size_t iter : gen) { + CHECK_EQ(i++, iter); + } +} + #endif int main(int argc, char** argv) {