Skip to content

Commit 5ce87d1

Browse files
committed
Coalesced packets in send_rpc_cmd and send_msg
1 parent dd62dcf commit 5ce87d1

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

ggml/src/ggml-rpc/ggml-rpc.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,20 @@ static bool recv_data(sockfd_t sockfd, void * data, size_t size) {
387387
}
388388

389389
static bool send_msg(sockfd_t sockfd, const void * msg, size_t msg_size) {
390-
if (!send_data(sockfd, &msg_size, sizeof(msg_size))) {
391-
return false;
390+
const size_t header_size = sizeof(msg_size);
391+
std::vector<uint8_t> buf;
392+
buf.resize(header_size + msg_size);
393+
394+
// header
395+
memcpy(buf.data(), &msg_size, sizeof(msg_size));
396+
397+
// payload
398+
if (msg_size > 0) {
399+
memcpy(buf.data() + header_size, msg, msg_size);
392400
}
393-
return send_data(sockfd, msg, msg_size);
401+
402+
// single send
403+
return send_data(sockfd, buf.data(), buf.size());
394404
}
395405

396406
static bool recv_msg(sockfd_t sockfd, void * msg, size_t msg_size) {
@@ -431,17 +441,21 @@ static bool parse_endpoint(const std::string & endpoint, std::string & host, int
431441
// RPC request : | rpc_cmd (1 byte) | request_size (8 bytes) | request_data (request_size bytes) |
432442
// No response
433443
static bool send_rpc_cmd(const std::shared_ptr<socket_t> & sock, enum rpc_cmd cmd, const void * input, size_t input_size) {
434-
uint8_t cmd_byte = cmd;
435-
if (!send_data(sock->fd, &cmd_byte, sizeof(cmd_byte))) {
436-
return false;
437-
}
438-
if (!send_data(sock->fd, &input_size, sizeof(input_size))) {
439-
return false;
440-
}
441-
if (!send_data(sock->fd, input, input_size)) {
442-
return false;
444+
const size_t header_size = 1 + sizeof(input_size);
445+
std::vector<uint8_t> buf;
446+
buf.resize(header_size + input_size);
447+
448+
// header
449+
buf[0] = static_cast<uint8_t>(cmd);
450+
memcpy(buf.data() + 1, &input_size, sizeof(input_size));
451+
452+
// payload
453+
if (input_size > 0) {
454+
memcpy(buf.data() + header_size, input, input_size);
443455
}
444-
return true;
456+
457+
// single send (send_data may still chunk very large buffers, which is fine)
458+
return send_data(sock->fd, buf.data(), buf.size());
445459
}
446460

447461
// RPC request : | rpc_cmd (1 byte) | request_size (8 bytes) | request_data (request_size bytes) |

0 commit comments

Comments
 (0)