Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flatbuffer builder reuse #1422

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
ibc marked this conversation as resolved.
Show resolved Hide resolved
}
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{};
jmillan marked this conversation as resolved.
Show resolved Hide resolved
/* 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
Loading