antirez/redis — pull request #1482 by shenfeng (2013-12-25 13:10:12+00:00)
Title: Optimize networking (demo of concept)

Hi, this is a demo of concept, use code to discuss the concept.

When we have content need to be written to TCP buffer, instead of

1. register AE_WRITABLE event
2. get notified that the TCP buffer has space
3. write the buffer
4. deregister‎ AE_WRITABLE event

We can first try 3, if all the content get copied to TCP buffer, then we save at least 2 system calls. If not, follow the original path.
Benchmark shows, even under heavy load of redis-benchmark, the latter happens rarely. So, most of the time, we can save two system call(two `epoll_ctl` on linux, which is O(n)), and less latency, since we do not need to wait until the next round, `aeProcessEvents` get called.

Only commands handing modified, we may need to modify replication logic too.

With this patch, `/src/redis-benchmark -t get -n 500000` can get 2%-3% faster.
You can see from a profile without the patch, `epoll_ctl` cost ~8% of the CPU time.
[![](http://shenfeng.me/imgs/redis-server-profile.svg)](http://shenfeng.me/imgs/redis-server-profile.svg)