From c866d65966e2fff89f0c6de6c3d2cc6bf9cceed6 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 5 Aug 2022 08:28:33 -0700 Subject: [PATCH] [promises] Eliminate switch.h (#30317) * [promises] Eliminate switch.h Instead build a jump table that we can index into and call the appropriate function. Means that the dispatch mechanism can be done in C++ without a code generator, and so eliminates a bunch of fiddly code. * fix * ensure static initialization * Automated change: Fix sanity tests Co-authored-by: ctiller --- BUILD | 10 - build_autogenerated.yaml | 16 - gRPC-C++.podspec | 2 - gRPC-Core.podspec | 2 - grpc.gemspec | 1 - package.xml | 1 - src/core/lib/promise/detail/basic_seq.h | 35 +- src/core/lib/promise/detail/switch.h | 1455 ----------------------- tools/codegen/core/gen_switch.py | 75 -- tools/doxygen/Doxyfile.c++.internal | 1 - tools/doxygen/Doxyfile.core.internal | 1 - 11 files changed, 27 insertions(+), 1572 deletions(-) delete mode 100644 src/core/lib/promise/detail/switch.h delete mode 100755 tools/codegen/core/gen_switch.py diff --git a/BUILD b/BUILD index ac2f0917d3e..5d82de6b525 100644 --- a/BUILD +++ b/BUILD @@ -1395,15 +1395,6 @@ grpc_cc_library( ], ) -grpc_cc_library( - name = "switch", - language = "c++", - public_hdrs = [ - "src/core/lib/promise/detail/switch.h", - ], - deps = ["gpr_platform"], -) - grpc_cc_library( name = "basic_join", external_deps = [ @@ -1472,7 +1463,6 @@ grpc_cc_library( "poll", "promise_factory", "promise_like", - "switch", ], ) diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index fef1c076d5c..a982dfd0757 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -866,7 +866,6 @@ libs: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/exec_ctx_wakeup_scheduler.h - src/core/lib/promise/intra_activity_waiter.h - src/core/lib/promise/latch.h @@ -2051,7 +2050,6 @@ libs: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/exec_ctx_wakeup_scheduler.h - src/core/lib/promise/intra_activity_waiter.h - src/core/lib/promise/latch.h @@ -3667,7 +3665,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/join.h - src/core/lib/promise/poll.h - src/core/lib/promise/promise.h @@ -4081,7 +4078,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/exec_ctx_wakeup_scheduler.h - src/core/lib/promise/loop.h - src/core/lib/promise/map.h @@ -4929,7 +4925,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/exec_ctx_wakeup_scheduler.h - src/core/lib/promise/loop.h - src/core/lib/promise/map.h @@ -6138,7 +6133,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/exec_ctx_wakeup_scheduler.h - src/core/lib/promise/loop.h - src/core/lib/promise/map.h @@ -6222,7 +6216,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/exec_ctx_wakeup_scheduler.h - src/core/lib/promise/for_each.h - src/core/lib/promise/intra_activity_waiter.h @@ -7558,7 +7551,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/intra_activity_waiter.h - src/core/lib/promise/join.h - src/core/lib/promise/latch.h @@ -7723,7 +7715,6 @@ targets: - src/core/lib/promise/detail/basic_seq.h - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/loop.h - src/core/lib/promise/poll.h - src/core/lib/promise/seq.h @@ -7809,7 +7800,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/exec_ctx_wakeup_scheduler.h - src/core/lib/promise/loop.h - src/core/lib/promise/map.h @@ -7887,7 +7877,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/exec_ctx_wakeup_scheduler.h - src/core/lib/promise/loop.h - src/core/lib/promise/map.h @@ -8238,7 +8227,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/observable.h - src/core/lib/promise/poll.h - src/core/lib/promise/promise.h @@ -8567,7 +8555,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/exec_ctx_wakeup_scheduler.h - src/core/lib/promise/intra_activity_waiter.h - src/core/lib/promise/join.h @@ -9122,7 +9109,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/exec_ctx_wakeup_scheduler.h - src/core/lib/promise/loop.h - src/core/lib/promise/map.h @@ -9313,7 +9299,6 @@ targets: - src/core/lib/promise/detail/basic_seq.h - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/poll.h - src/core/lib/promise/seq.h src: @@ -11175,7 +11160,6 @@ targets: - src/core/lib/promise/detail/promise_factory.h - src/core/lib/promise/detail/promise_like.h - src/core/lib/promise/detail/status.h - - src/core/lib/promise/detail/switch.h - src/core/lib/promise/poll.h - src/core/lib/promise/try_seq.h src: diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index 16bfd11741d..2597838fd64 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -825,7 +825,6 @@ Pod::Spec.new do |s| 'src/core/lib/promise/detail/promise_factory.h', 'src/core/lib/promise/detail/promise_like.h', 'src/core/lib/promise/detail/status.h', - 'src/core/lib/promise/detail/switch.h', 'src/core/lib/promise/exec_ctx_wakeup_scheduler.h', 'src/core/lib/promise/intra_activity_waiter.h', 'src/core/lib/promise/latch.h', @@ -1680,7 +1679,6 @@ Pod::Spec.new do |s| 'src/core/lib/promise/detail/promise_factory.h', 'src/core/lib/promise/detail/promise_like.h', 'src/core/lib/promise/detail/status.h', - 'src/core/lib/promise/detail/switch.h', 'src/core/lib/promise/exec_ctx_wakeup_scheduler.h', 'src/core/lib/promise/intra_activity_waiter.h', 'src/core/lib/promise/latch.h', diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index fcd4ca59e6b..0bf5b88aba9 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec @@ -1340,7 +1340,6 @@ Pod::Spec.new do |s| 'src/core/lib/promise/detail/promise_factory.h', 'src/core/lib/promise/detail/promise_like.h', 'src/core/lib/promise/detail/status.h', - 'src/core/lib/promise/detail/switch.h', 'src/core/lib/promise/exec_ctx_wakeup_scheduler.h', 'src/core/lib/promise/intra_activity_waiter.h', 'src/core/lib/promise/latch.h', @@ -2302,7 +2301,6 @@ Pod::Spec.new do |s| 'src/core/lib/promise/detail/promise_factory.h', 'src/core/lib/promise/detail/promise_like.h', 'src/core/lib/promise/detail/status.h', - 'src/core/lib/promise/detail/switch.h', 'src/core/lib/promise/exec_ctx_wakeup_scheduler.h', 'src/core/lib/promise/intra_activity_waiter.h', 'src/core/lib/promise/latch.h', diff --git a/grpc.gemspec b/grpc.gemspec index 543dc485c53..b45550cd2ec 100644 --- a/grpc.gemspec +++ b/grpc.gemspec @@ -1253,7 +1253,6 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/promise/detail/promise_factory.h ) s.files += %w( src/core/lib/promise/detail/promise_like.h ) s.files += %w( src/core/lib/promise/detail/status.h ) - s.files += %w( src/core/lib/promise/detail/switch.h ) s.files += %w( src/core/lib/promise/exec_ctx_wakeup_scheduler.h ) s.files += %w( src/core/lib/promise/intra_activity_waiter.h ) s.files += %w( src/core/lib/promise/latch.h ) diff --git a/package.xml b/package.xml index 6d1bd92223c..471e31dd4cd 100644 --- a/package.xml +++ b/package.xml @@ -1235,7 +1235,6 @@ - diff --git a/src/core/lib/promise/detail/basic_seq.h b/src/core/lib/promise/detail/basic_seq.h index 2687d1f2568..1e4756d8a37 100644 --- a/src/core/lib/promise/detail/basic_seq.h +++ b/src/core/lib/promise/detail/basic_seq.h @@ -17,6 +17,8 @@ #include +#include + #include #include #include @@ -30,12 +32,29 @@ #include "src/core/lib/gprpp/construct_destruct.h" #include "src/core/lib/promise/detail/promise_factory.h" #include "src/core/lib/promise/detail/promise_like.h" -#include "src/core/lib/promise/detail/switch.h" #include "src/core/lib/promise/poll.h" namespace grpc_core { namespace promise_detail { +// Given f0, ..., fn, call function idx and return the result. +template +class JumpTable { + public: + JumpTable() = delete; + JumpTable(const JumpTable&) = delete; + + static R Run(size_t idx, A* arg) { return fs_[idx](arg); } + + private: + using Fn = R (*)(A* arg); + static const Fn fs_[sizeof...(f)]; +}; + +template +const typename JumpTable::Fn + JumpTable::fs_[sizeof...(f)] = {f...}; + // Helper for SeqState to evaluate some common types to all partial // specializations. template