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..82830bef7c 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 @@ -119,7 +123,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 +138,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 +153,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 +167,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()