Skip to content

Use send/recv instead of write/read when the reactor_stream is a socket #5

Description

@talawahtech

Hi,

I have been benchmarking and profiling libreactor to try and find any potential optimizations for what is already a very, very fast framework.

In my tests using the Techempower JSON benchmark running on an AWS EC2 instance, I was able to achieve a performance improvement of a little over 10% by using the send/recv functions (with the flags param set to 0) in place of write/read.

From the attached flamegraphs (see below) of the syscalls made during the test, you can see that sys_read/sys_write call several intermediate functions before finally calling inet_recvmsg and sock_sendmsg. So even though the behavior is functionally identical, there is a performance gain to be had that shows up tests like this.

I would like to create a PR for libreactor where reactor_stream_flush and reactor_stream_input conditionally use send and recv if the steam in question is known to be a socket. However I wanted to find out what your preferred approach would be.

One approach could be to add a new member to the reactor_stream struct called is_socket. That value could be initialized when reactor_stream_open is called, either by passing and additional parameter or checking it automatically using fstat/S_ISSOCK. I just wanted to get you feedback before I created a PR.

FYI I also tried using sendto and sendmsg. The performance of sendto was the same and sendmsg was actually worse.

write/read
image

send/recv
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions