Skip to content

Commit

Permalink
flatbuffer builder reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaohailin committed Jul 4, 2024
1 parent 9d9e358 commit 6e3e18e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions worker/include/Channel/ChannelNotifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -48,7 +48,7 @@ namespace Channel

builder.FinishSizePrefixed(message);
this->channel->Send(builder.GetBufferPointer(), builder.GetSize());
builder.Reset();
builder.Clear();
}

private:
Expand Down
8 changes: 4 additions & 4 deletions worker/include/Channel/ChannelRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ namespace Channel

public:
static absl::flat_hash_map<FBS::Request::Method, const char*> method2String;
thread_local static flatbuffers::FlatBufferBuilder bufferBuilder;

public:
ChannelRequest(Channel::ChannelSocket* channel, const FBS::Request::Request* request);
~ChannelRequest() = default;

flatbuffers::FlatBufferBuilder& GetBufferBuilder()
{
return this->bufferBuilder;
return ChannelRequest::bufferBuilder;
}
void Accept();
template<class Body>
Expand All @@ -39,15 +40,15 @@ 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 =
FBS::Message::CreateMessage(builder, FBS::Message::Body::Response, response.Union());

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);
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions worker/src/Channel/ChannelRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

/**
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -163,12 +163,12 @@ namespace Channel

void ChannelRequest::SendResponse(const flatbuffers::Offset<FBS::Response::Response>& 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
2 changes: 1 addition & 1 deletion worker/src/Channel/ChannelSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 6e3e18e

Please sign in to comment.