Skip to content

Commit 732edd8

Browse files
authored
feat: optimize runtime performance by reducing copy input attachment. (#298)
Signed-off-by: Tao Peng <[email protected]>
1 parent f8807a8 commit 732edd8

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

xllm/api_service/api_service.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ void APIService::CompletionsHttp(::google::protobuf::RpcController* controller,
9595
google::protobuf::Arena::CreateMessage<proto::CompletionResponse>(arena);
9696

9797
auto ctrl = reinterpret_cast<brpc::Controller*>(controller);
98-
std::string attachment = std::move(ctrl->request_attachment().to_string());
9998
std::string error;
100-
auto st = json2pb::JsonToProtoMessage(attachment, req_pb, &error);
99+
json2pb::Json2PbOptions options;
100+
butil::IOBuf& buf = ctrl->request_attachment();
101+
butil::IOBufAsZeroCopyInputStream iobuf_stream(buf);
102+
auto st = json2pb::JsonToProtoMessage(&iobuf_stream, req_pb, options, &error);
101103
if (!st) {
102104
ctrl->SetFailed(error);
103105
LOG(ERROR) << "parse json to proto failed: " << error;
@@ -127,16 +129,14 @@ void ChatCompletionsImpl(std::unique_ptr<Service>& service,
127129
auto resp_pb =
128130
google::protobuf::Arena::CreateMessage<typename ChatCall::ResType>(arena);
129131

130-
std::string attachment = std::move(ctrl->request_attachment().to_string());
131132
std::string error;
132-
133-
google::protobuf::util::JsonParseOptions options;
134-
options.ignore_unknown_fields = true;
135-
auto json_status =
136-
google::protobuf::util::JsonStringToMessage(attachment, req_pb, options);
137-
if (!json_status.ok()) {
138-
ctrl->SetFailed(json_status.ToString());
139-
LOG(ERROR) << "parse json to proto failed: " << json_status.ToString();
133+
json2pb::Json2PbOptions options;
134+
butil::IOBuf& buf = ctrl->request_attachment();
135+
butil::IOBufAsZeroCopyInputStream iobuf_stream(buf);
136+
auto st = json2pb::JsonToProtoMessage(&iobuf_stream, req_pb, options, &error);
137+
if (!st) {
138+
ctrl->SetFailed(error);
139+
LOG(ERROR) << "parse json to proto failed: " << buf.to_string();
140140
return;
141141
}
142142

@@ -201,9 +201,11 @@ void APIService::EmbeddingsHttp(::google::protobuf::RpcController* controller,
201201
google::protobuf::Arena::CreateMessage<proto::EmbeddingResponse>(arena);
202202

203203
auto ctrl = reinterpret_cast<brpc::Controller*>(controller);
204-
std::string attachment = std::move(ctrl->request_attachment().to_string());
205204
std::string error;
206-
auto st = json2pb::JsonToProtoMessage(attachment, req_pb, &error);
205+
json2pb::Json2PbOptions options;
206+
butil::IOBuf& buf = ctrl->request_attachment();
207+
butil::IOBufAsZeroCopyInputStream iobuf_stream(buf);
208+
auto st = json2pb::JsonToProtoMessage(&iobuf_stream, req_pb, options, &error);
207209
if (!st) {
208210
ctrl->SetFailed(error);
209211
LOG(ERROR) << "parse json to proto failed: " << error;
@@ -248,10 +250,13 @@ void APIService::ImageGenerationHttp(
248250
auto resp_pb =
249251
google::protobuf::Arena::CreateMessage<proto::ImageGenerationResponse>(
250252
arena);
253+
251254
auto ctrl = reinterpret_cast<brpc::Controller*>(controller);
252-
std::string attachment = std::move(ctrl->request_attachment().to_string());
253255
std::string error;
254-
auto st = json2pb::JsonToProtoMessage(attachment, req_pb, &error);
256+
json2pb::Json2PbOptions options;
257+
butil::IOBuf& buf = ctrl->request_attachment();
258+
butil::IOBufAsZeroCopyInputStream iobuf_stream(buf);
259+
auto st = json2pb::JsonToProtoMessage(&iobuf_stream, req_pb, options, &error);
255260
if (!st) {
256261
ctrl->SetFailed(error);
257262
LOG(ERROR) << "parse json to proto failed: " << error;
@@ -290,9 +295,11 @@ void APIService::RerankHttp(::google::protobuf::RpcController* controller,
290295
google::protobuf::Arena::CreateMessage<proto::RerankResponse>(arena);
291296

292297
auto ctrl = reinterpret_cast<brpc::Controller*>(controller);
293-
std::string attachment = std::move(ctrl->request_attachment().to_string());
294298
std::string error;
295-
auto st = json2pb::JsonToProtoMessage(attachment, req_pb, &error);
299+
json2pb::Json2PbOptions options;
300+
butil::IOBuf& buf = ctrl->request_attachment();
301+
butil::IOBufAsZeroCopyInputStream iobuf_stream(buf);
302+
auto st = json2pb::JsonToProtoMessage(&iobuf_stream, req_pb, options, &error);
296303
if (!st) {
297304
ctrl->SetFailed(error);
298305
LOG(ERROR) << "parse json to proto failed: " << error;
@@ -398,9 +405,11 @@ void APIService::LinkCluster(::google::protobuf::RpcController* controller,
398405
google::protobuf::Arena::CreateMessage<proto::RpcStatus>(arena);
399406

400407
auto ctrl = reinterpret_cast<brpc::Controller*>(controller);
401-
std::string attachment = std::move(ctrl->request_attachment().to_string());
402408
std::string error;
403-
auto st = json2pb::JsonToProtoMessage(attachment, req_pb, &error);
409+
json2pb::Json2PbOptions options;
410+
butil::IOBuf& buf = ctrl->request_attachment();
411+
butil::IOBufAsZeroCopyInputStream iobuf_stream(buf);
412+
auto st = json2pb::JsonToProtoMessage(&iobuf_stream, req_pb, options, &error);
404413
if (!st) {
405414
ctrl->SetFailed(error);
406415
LOG(ERROR) << "parse json to proto failed: " << error;
@@ -452,9 +461,11 @@ void APIService::UnlinkCluster(::google::protobuf::RpcController* controller,
452461
google::protobuf::Arena::CreateMessage<proto::RpcStatus>(arena);
453462

454463
auto ctrl = reinterpret_cast<brpc::Controller*>(controller);
455-
std::string attachment = std::move(ctrl->request_attachment().to_string());
456464
std::string error;
457-
auto st = json2pb::JsonToProtoMessage(attachment, req_pb, &error);
465+
json2pb::Json2PbOptions options;
466+
butil::IOBuf& buf = ctrl->request_attachment();
467+
butil::IOBufAsZeroCopyInputStream iobuf_stream(buf);
468+
auto st = json2pb::JsonToProtoMessage(&iobuf_stream, req_pb, options, &error);
458469
if (!st) {
459470
ctrl->SetFailed(error);
460471
LOG(ERROR) << "parse json to proto failed: " << error;

0 commit comments

Comments
 (0)