Skip to content
  • Sergey Senozhatsky's avatar
    lib/bsearch.c: micro-optimize pivot position calculation · 166a0f78
    Sergey Senozhatsky authored
    There is a slightly faster way (in terms of the number of instructions
    being used) to calculate the position of a middle element, preserving
    integer overflow safeness.
    
    ./scripts/bloat-o-meter lib/bsearch.o.old lib/bsearch.o.new
    add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-24 (-24)
    function                                     old     new   delta
    bsearch                                      122      98     -24
    
    TEST
    
    INT array of size 100001, elements [0..100000]. gcc 7.1, Os, x86_64.
    
    a) bsearch() of existing key "100001 - 2":
    
    BASE
    ====
    
    $ perf stat ./a.out
    
     Performance counter stats for './a.out':
    
            619.445196      task-clock:u (msec)       #    0.999 CPUs utilized
                     0      context-switches:u        #    0.000 K/sec
                     0      cpu-migrations:u          #    0.000 K/sec
                   133      page-faults:u             #    0.215 K/sec
         1,949,517,279      cycles:u                  #    3.147 GHz              ...
    166a0f78