Skip to content

Commit

Permalink
Simple comparison of AsyncGenerator and Generator "hot path" performance
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Alexey Spiridonov authored and facebook-github-bot committed Nov 9, 2023
1 parent 3faf49b commit 7ed8c61
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions folly/experimental/coro/test/AsyncGeneratorBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <folly/experimental/coro/AsyncGenerator.h>
#include <folly/experimental/coro/BlockingWait.h>
#include <folly/experimental/coro/Generator.h>
#include <folly/experimental/coro/Task.h>
#include <folly/experimental/coro/ViaIfAsync.h>

Expand Down Expand Up @@ -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<void> {
auto gen = [](size_t iters) -> folly::coro::AsyncGenerator<size_t> {
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<size_t> {
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) {
Expand Down

0 comments on commit 7ed8c61

Please sign in to comment.