From a34b8b9ef3c49547949f26be3e97193a117ce19c Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Mon, 24 Jun 2024 10:19:05 -0700 Subject: [PATCH] [party] Short circuit expensive loop at the end of every party (#37004) Before: ``` ---------------------------------------------------------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------------------------------------------------------- grpc_core::BM_UnaryWithSpawnPerEnd 1171 ns 1171 ns 2373821 grpc_core::BM_UnaryWithSpawnPerOp 1326 ns 1326 ns 2124026 grpc_core::BM_ClientToServerStreaming 256 ns 256 ns 10512990 grpc_core::BM_UnaryWithSpawnPerEnd 2957 ns 2957 ns 949121 grpc_core::BM_UnaryWithSpawnPerOp 3172 ns 3172 ns 882463 grpc_core::BM_ClientToServerStreaming 500 ns 500 ns 5765914 ``` After: ``` ---------------------------------------------------------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------------------------------------------------------- grpc_core::BM_UnaryWithSpawnPerEnd 1147 ns 1147 ns 2451909 grpc_core::BM_UnaryWithSpawnPerOp 1325 ns 1325 ns 2107818 grpc_core::BM_ClientToServerStreaming 256 ns 256 ns 10896638 grpc_core::BM_UnaryWithSpawnPerEnd 2876 ns 2876 ns 954360 grpc_core::BM_UnaryWithSpawnPerOp 3148 ns 3148 ns 891892 grpc_core::BM_ClientToServerStreaming 503 ns 503 ns 5518039 ``` Closes #37004 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/37004 from ctiller:cancel-fast 6b6b2b449cba9518daf1a02d1a0a5ed7e31491b3 PiperOrigin-RevId: 646138952 --- src/core/lib/promise/party.cc | 1 + src/core/lib/promise/party.h | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/core/lib/promise/party.cc b/src/core/lib/promise/party.cc index 5a14f235cf4..6d04e251ee8 100644 --- a/src/core/lib/promise/party.cc +++ b/src/core/lib/promise/party.cc @@ -178,6 +178,7 @@ Party::Participant::~Participant() { Party::~Party() {} void Party::CancelRemainingParticipants() { + if (!sync_.has_participants()) return; ScopedActivity activity(this); for (size_t i = 0; i < party_detail::kMaxParticipants; i++) { if (auto* p = diff --git a/src/core/lib/promise/party.h b/src/core/lib/promise/party.h index 6a1894ff2bf..b8f11cfd9bd 100644 --- a/src/core/lib/promise/party.h +++ b/src/core/lib/promise/party.h @@ -224,6 +224,10 @@ class PartySyncUsingAtomics { return iteration_.load(std::memory_order_relaxed); } + bool has_participants() const { + return (state_.load(std::memory_order_relaxed) & kAllocatedMask) != 0; + } + private: bool UnreffedLast();