Why is epoll faster than select?
2016-08-24 14:15
1426 查看
A typical server might be dealing with, say, 200 connections. It will service every connection that needs to have data written or read and then it will need to wait until there's more work to do. While it's waiting, it needs to be interrupted if data is received
on any of those 200 connections.
With
the kernel has to add the process to 200 wait lists, one for each connection. To do this, it needs a "thunk" to attach the process to the wait list. When the process finally does wake up, it needs to be removed from all 200 wait lists and all those thunks
need to be freed.
By contrast, with
the
itself has a wait list. The process needs to be put on only that one wait list using only one thunk. When the process wakes up, it needs to be removed from only one wait list and only one thunk needs to be freed.
To be clear, with
the
itself has to be attached to each of those 200 connections. But this is done once, for each connection, when it is accepted in the first place. And this is torn down once, for each connection, when it is removed. By contrast, each call to
blocks must add the process to every wait queue for every socket being monitored.
Ironically, with
the largest cost comes from checking if sockets that have had no activity have had any activity. With
there is no need to check sockets that have had no activity because if they did have activity, they would have informed the
when that activity happened. In a sense,
each socket each time you call
see if there's any activity while
is so that the socket activity itself notifies the process.
on any of those 200 connections.
With
select,
the kernel has to add the process to 200 wait lists, one for each connection. To do this, it needs a "thunk" to attach the process to the wait list. When the process finally does wake up, it needs to be removed from all 200 wait lists and all those thunks
need to be freed.
By contrast, with
epoll,
the
epollsocket
itself has a wait list. The process needs to be put on only that one wait list using only one thunk. When the process wakes up, it needs to be removed from only one wait list and only one thunk needs to be freed.
To be clear, with
epoll,
the
epollsocket
itself has to be attached to each of those 200 connections. But this is done once, for each connection, when it is accepted in the first place. And this is torn down once, for each connection, when it is removed. By contrast, each call to
selectthat
blocks must add the process to every wait queue for every socket being monitored.
Ironically, with
select,
the largest cost comes from checking if sockets that have had no activity have had any activity. With
epoll,
there is no need to check sockets that have had no activity because if they did have activity, they would have informed the
epollsocket
when that activity happened. In a sense,
selectpolls
each socket each time you call
selectto
see if there's any activity while
epollrigs
is so that the socket activity itself notifies the process.
相关文章推荐
- JAVA常见问题其一:Why is it faster to process a sorted array than an unsorted array?
- Why is processing a sorted array faster than an unsorted array(Stackoverflow)
- Why is it faster to process a sorted array than an unsorted array?
- 001:Why is it faster to process a sorted array than an unsorted array?
- Why is processing a sorted array faster than an unsorted array(Stackoverflow)
- Why is processing a sorted array faster than an unsorted array?
- Why is truth() faster than bool()? Part II
- Why is using BufferedInputStream to read a file byte by byte faster than using FileInputStream
- Why is truth() faster than bool()? Part I
- Why our website is faster than yours
- stackoverflow : Why C++ output is too much slower than C?
- 7 Reasons Why BSD Is Better than Linux
- ROS, the Robot Operating System, Is Growing Faster Than Ever, Celebrates 8 Years
- stackoverflow : Why C++ output is too much slower than C?
- Why is Oracle OCI a better API for a scalable, multi-threaded application than Open Database Connectivity (ODBC) ?
- Why attitude is more important than IQ
- Why are some programming languages faster than others?
- Why Git is Better than X
- Why is IMAP better than POP?
- Why Git is better than SVN