-
Notifications
You must be signed in to change notification settings - Fork 75
Use sendmsg instead of send
PAN, Myautsai edited this page Feb 4, 2016
·
3 revisions
When dispatching differnent keys to different Connections, BufferWritters in each Connection are used to storage fragments before send them to corresponding memcached servers.
Suppose we want to retrieve key "foo" from a memcached server:
-
use
sendwithout memory copying:send("GET ") send("foo") send("\r\n") -
use
sendwith memory copying:alloc a buffer memcpy(buffer, "GET ") memcpy(buffer, "foo") memcpy(buffer, "\r\n") // now buffer = "GET foo\r\n" send(buffer)
As you can see, we have to
A. call send 3 times(system call is time consuming)
or
B. call memcpy 3 times(dynamic memory allocation and
copying memory are time consuming) and send once.
Use sendmsg is the third choice:
char kGET_[] = "GET "
char* key = char pointer to foo
char kCRLF[] = "\r\n"
struct iovec iovec_array[3]
iovec_array[0].iov_base = kGET_
iovec_array[0].iov_base = 4
iovec_array[1].iov_base = key
iovec_array[1].iov_base = 3
iovec_array[2].iov_base = kCRLF
iovec_array[2].iov_base = 2
struct msghdr msg
msg.msg_iov = &iovec_array[0]
msg.msg_iovlen = 3
sendmsg(msg)