From 6e3e18e31f7ad2a25b1ae17744408cbf9b7ec2d9 Mon Sep 17 00:00:00 2001 From: zhaohailin Date: Thu, 4 Jul 2024 15:48:56 +0800 Subject: [PATCH 1/3] flatbuffer builder reuse --- worker/include/Channel/ChannelNotifier.hpp | 4 ++-- worker/include/Channel/ChannelRequest.hpp | 8 ++++---- worker/src/Channel/ChannelRequest.cpp | 12 ++++++------ worker/src/Channel/ChannelSocket.cpp | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/worker/include/Channel/ChannelNotifier.hpp b/worker/include/Channel/ChannelNotifier.hpp index c896ca3958..097c1398c0 100644 --- a/worker/include/Channel/ChannelNotifier.hpp +++ b/worker/include/Channel/ChannelNotifier.hpp @@ -34,7 +34,7 @@ namespace Channel builder.FinishSizePrefixed(message); this->channel->Send(builder.GetBufferPointer(), builder.GetSize()); - builder.Reset(); + builder.Clear(); } void Emit(const std::string& targetId, FBS::Notification::Event event) @@ -48,7 +48,7 @@ namespace Channel builder.FinishSizePrefixed(message); this->channel->Send(builder.GetBufferPointer(), builder.GetSize()); - builder.Reset(); + builder.Clear(); } private: diff --git a/worker/include/Channel/ChannelRequest.hpp b/worker/include/Channel/ChannelRequest.hpp index 1f1b48c242..2df2d5dea2 100644 --- a/worker/include/Channel/ChannelRequest.hpp +++ b/worker/include/Channel/ChannelRequest.hpp @@ -22,6 +22,7 @@ namespace Channel public: static absl::flat_hash_map method2String; + thread_local static flatbuffers::FlatBufferBuilder bufferBuilder; public: ChannelRequest(Channel::ChannelSocket* channel, const FBS::Request::Request* request); @@ -29,7 +30,7 @@ namespace Channel flatbuffers::FlatBufferBuilder& GetBufferBuilder() { - return this->bufferBuilder; + return ChannelRequest::bufferBuilder; } void Accept(); template @@ -39,7 +40,7 @@ namespace Channel this->replied = true; - auto& builder = this->bufferBuilder; + auto& builder = ChannelRequest::bufferBuilder; auto response = FBS::Response::CreateResponse(builder, this->id, true, type, body.Union()); auto message = @@ -47,7 +48,7 @@ namespace Channel builder.FinishSizePrefixed(message); this->Send(builder.GetBufferPointer(), builder.GetSize()); - builder.Reset(); + builder.Clear(); } void Error(const char* reason = nullptr); void TypeError(const char* reason = nullptr); @@ -61,7 +62,6 @@ namespace Channel Channel::ChannelSocket* channel{ nullptr }; const FBS::Request::Request* data{ nullptr }; // Others. - flatbuffers::FlatBufferBuilder bufferBuilder{}; uint32_t id{ 0u }; Method method; const char* methodCStr; diff --git a/worker/src/Channel/ChannelRequest.cpp b/worker/src/Channel/ChannelRequest.cpp index f268d94f21..e81c922a9e 100644 --- a/worker/src/Channel/ChannelRequest.cpp +++ b/worker/src/Channel/ChannelRequest.cpp @@ -83,7 +83,7 @@ namespace Channel { FBS::Request::Method::RTPOBSERVER_REMOVE_PRODUCER, "rtpObserver.removeProducer" }, }; // clang-format on - + thread_local flatbuffers::FlatBufferBuilder ChannelRequest::bufferBuilder{}; /* Instance methods. */ /** @@ -119,7 +119,7 @@ namespace Channel this->replied = true; - auto& builder = this->bufferBuilder; + auto& builder = ChannelRequest::bufferBuilder; auto response = FBS::Response::CreateResponse(builder, this->id, true, FBS::Response::Body::NONE, 0); @@ -134,7 +134,7 @@ namespace Channel this->replied = true; - auto& builder = this->bufferBuilder; + auto& builder = ChannelRequest::bufferBuilder; auto response = FBS::Response::CreateResponseDirect( builder, this->id, false /*accepted*/, FBS::Response::Body::NONE, 0, "Error" /*Error*/, reason); @@ -149,7 +149,7 @@ namespace Channel this->replied = true; - auto& builder = this->bufferBuilder; + auto& builder = ChannelRequest::bufferBuilder; auto response = FBS::Response::CreateResponseDirect( builder, this->id, false /*accepted*/, FBS::Response::Body::NONE, 0, "TypeError" /*Error*/, reason); @@ -163,12 +163,12 @@ namespace Channel void ChannelRequest::SendResponse(const flatbuffers::Offset& response) { - auto& builder = this->bufferBuilder; + auto& builder = ChannelRequest::bufferBuilder; auto message = FBS::Message::CreateMessage(builder, FBS::Message::Body::Response, response.Union()); builder.FinishSizePrefixed(message); this->Send(builder.GetBufferPointer(), builder.GetSize()); - builder.Reset(); + builder.Clear(); } } // namespace Channel diff --git a/worker/src/Channel/ChannelSocket.cpp b/worker/src/Channel/ChannelSocket.cpp index cb99e8472b..144712501b 100644 --- a/worker/src/Channel/ChannelSocket.cpp +++ b/worker/src/Channel/ChannelSocket.cpp @@ -162,7 +162,7 @@ namespace Channel this->bufferBuilder.FinishSizePrefixed(message); this->Send(this->bufferBuilder.GetBufferPointer(), this->bufferBuilder.GetSize()); - this->bufferBuilder.Reset(); + this->bufferBuilder.Clear(); } bool ChannelSocket::CallbackRead() From 028bb1b033023551c4da4cdfb4c41f49d06c39b3 Mon Sep 17 00:00:00 2001 From: zhaohailin Date: Thu, 4 Jul 2024 16:33:04 +0800 Subject: [PATCH 2/3] move variable positin --- worker/src/Channel/ChannelRequest.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/worker/src/Channel/ChannelRequest.cpp b/worker/src/Channel/ChannelRequest.cpp index e81c922a9e..b31a2b9b5d 100644 --- a/worker/src/Channel/ChannelRequest.cpp +++ b/worker/src/Channel/ChannelRequest.cpp @@ -7,6 +7,10 @@ namespace Channel { + /* Static variables. */ + + thread_local flatbuffers::FlatBufferBuilder ChannelRequest::bufferBuilder{}; + /* Class variables. */ // clang-format off @@ -83,7 +87,7 @@ namespace Channel { FBS::Request::Method::RTPOBSERVER_REMOVE_PRODUCER, "rtpObserver.removeProducer" }, }; // clang-format on - thread_local flatbuffers::FlatBufferBuilder ChannelRequest::bufferBuilder{}; + /* Instance methods. */ /** From ffe7f9d7a35aa250a5931a3d523f4afa2364e023 Mon Sep 17 00:00:00 2001 From: zhaohailin Date: Thu, 4 Jul 2024 17:20:54 +0800 Subject: [PATCH 3/3] fix clang format --- worker/src/Channel/ChannelRequest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/worker/src/Channel/ChannelRequest.cpp b/worker/src/Channel/ChannelRequest.cpp index b31a2b9b5d..82830bef7c 100644 --- a/worker/src/Channel/ChannelRequest.cpp +++ b/worker/src/Channel/ChannelRequest.cpp @@ -9,7 +9,7 @@ namespace Channel { /* Static variables. */ - thread_local flatbuffers::FlatBufferBuilder ChannelRequest::bufferBuilder{}; + thread_local flatbuffers::FlatBufferBuilder ChannelRequest::bufferBuilder{}; /* Class variables. */ @@ -87,7 +87,7 @@ namespace Channel { FBS::Request::Method::RTPOBSERVER_REMOVE_PRODUCER, "rtpObserver.removeProducer" }, }; // clang-format on - + /* Instance methods. */ /**