• Andi Kleen's avatar
    [PATCH] Optimize select/poll by putting small data sets on the stack · 70674f95
    Andi Kleen authored
    Optimize select and poll by a using stack space for small fd sets
    
    This brings back an old optimization from Linux 2.0.  Using the stack is
    faster than kmalloc.  On a Intel P4 system it speeds up a select of a
    single pty fd by about 13% (~4000 cycles -> ~3500)
    
    It also saves memory because a daemon hanging in select or poll will
    usually save one or two less pages.  This can add up - e.g.  if you have 10
    daemons blocking in poll/select you save 40KB of memory.
    
    I did a patch for this long ago, but it was never applied.  This version is
    a reimplementation of the old patch that tries to be less intrusive.  I
    only did the minimal changes needed for the stack allocation.
    
    The cut off point before external memory is allocated is currently at
    832bytes.  The system calls always allocate this much memory on the stack.
    
    These 832 bytes are divided into 256 bytes frontend data (for the select
    bitmaps of the pollfds) and the rest of the space for the wait queues used
    by the low level drivers.  There are some extreme cases where this won't
    work out for select and it falls back to allocating memory too early -
    especially with very sparse large select bitmaps - but the majority of
    processes who only have a small number of file descriptors should be ok.
    [TBD: 832/256 might not be the best split for select or poll]
    
    I suspect more optimizations might be possible, but they would be more
    complicated.  One way would be to cache the select/poll context over
    multiple system calls because typically the input values should be similar.
     Problem is when to flush the file descriptors out though.
    Signed-off-by: default avatarAndi Kleen <ak@suse.de>
    Cc: Eric Dumazet <dada1@cosmosbay.com>
    Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
    70674f95
select.c 19.6 KB