From 14ca776c5324b850b320050ee1bf74ad60602df2 Mon Sep 17 00:00:00 2001 From: gitccl Date: Thu, 5 Jun 2025 10:55:06 +0800 Subject: [PATCH] Optimize shared_ptr assignment in DoublyBufferedData --- src/butil/containers/doubly_buffered_data.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/butil/containers/doubly_buffered_data.h b/src/butil/containers/doubly_buffered_data.h index ff96a90395..8ba54ac051 100644 --- a/src/butil/containers/doubly_buffered_data.h +++ b/src/butil/containers/doubly_buffered_data.h @@ -484,17 +484,15 @@ int DoublyBufferedData::Read( // foreground instance, so during the read process, there is // no need to lock mutex and bthread is allowed to be suspended. w->BeginRead(); - int index = -1; - ptr->_data = UnsafeRead(index); - ptr->_index = index; - w->AddRef(index); - ptr->_w = w; + // UnsafeRead will update ptr->_index + ptr->_data = UnsafeRead(ptr->_index); + w->AddRef(ptr->_index); w->BeginReadRelease(); } else { w->BeginRead(); ptr->_data = UnsafeRead(); - ptr->_w = w; } + ptr->_w.swap(w); return 0; }