This is an automated email from the ASF dual-hosted git repository.

wasphin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 0938215d Avoid redundant shared_ptr copies in bvar percentile writes 
(#3700)
0938215d is described below

commit 0938215d77db154cddb5484bc1e7e5a4e750a5e7
Author: Bright Chen <[email protected]>
AuthorDate: Mon Nov 2 22:16:38 2026 +0800

    Avoid redundant shared_ptr copies in bvar percentile writes (#3700)
---
 src/bvar/detail/combiner.h | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/bvar/detail/combiner.h b/src/bvar/detail/combiner.h
index 4fa72542..7f531d94 100644
--- a/src/bvar/detail/combiner.h
+++ b/src/bvar/detail/combiner.h
@@ -205,17 +205,30 @@ friend class GlobalValue<self_type>;
         // 
         // NOTE: Only available to non-atomic types.
         template <typename Op>
-        void merge_global(const Op &op, self_shared_type& c) {
-            const self_shared_type& c_ref = nullptr != c ? c : combiner.lock();
-            if (nullptr != c_ref) {
-                GlobalValue<self_type> g(this, c_ref.get());
-                element.merge_global(op, g);
+        void merge_global(const Op &op, const self_shared_type& c) {
+            // The caller keeps c alive during the merge. A conditional
+            // expression mixing c and combiner.lock() would copy c into a
+            // temporary shared_ptr, even when c is non-null.
+            if (c != nullptr) {
+                merge_global_impl(op, c.get());
+                return;
+            }
+            self_shared_type hold = combiner.lock();
+            if (hold != nullptr) {
+                merge_global_impl(op, hold.get());
             }
         }
 
         ElementContainer<ElementTp> element;
     private:
     friend class AgentCombiner<ResultTp, ElementTp, BinaryOp>;
+
+        template <typename Op>
+        void merge_global_impl(const Op& op, self_type* c) {
+            GlobalValue<self_type> g(this, c);
+            element.merge_global(op, g);
+        }
+
         self_weak_type combiner;
     };
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to