reduce syscalls needed to send a response #59

toffaletti opened this Issue Jan 7, 2013 · 3 comments


None yet

2 participants


Starman::Server::_finalize_response contains this code:

    # Buffer the headers so they are sent with the first write() call 
    # This reduces the number of TCP packets we are sending
    syswrite $conn, join( $CRLF, @headers, '' ) . $CRLF;

and later on the body is written:

syswrite $conn, $buffer;

This is doing two syscalls when only one is needed. I suggest saving the headers and prepend it to the first $buffer so for small responses only one call to write() is needed.


Well, that's true but is effective only when body is returned immediately (no streaming) and when the actual body is small that fits within a single TCP packet. And I argue that is an unnecessary optimization.


I have an app that does a lot of small fast requests essentially returning cached data from memcache and transforming it to JSON. I've profiled it with perf and besides some internal perl methods I can't easily speed up, this is the low hanging fruit. It also ends up hurting the client when the client gets two packets instead of one which causes it to go to epoll and do an extra epoll_wait(), read() cycle. This app probably shouldn't be written in perl, but for reasons out of my control it is and until I have time to rewrite it I'm stuck making optimizations like this. I can just do it in a private fork if you're not interested.


Why not doing in a public fork so that others might find it interesting :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment