src/ae.cc
/* Search the first timer to fire.
 * This operation is useful to know how many time the select can be
 * put in sleep without to delay any event.
 * If there are no timers NULL is returned.
 *
 * Note that's O(N) since time events are unsorted.
 * Possible optimizations (not needed by Redis so far, but...):
 * 1) Insert the event in order, so that the nearest is just the head.
 *    Much better but still insertion or deletion of timers is O(N).
 * 2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)).
 */
Search the first timer to fire.
This operation is useful to know how many time the select can be
put in sleep without to delay any event.
If there are no timers NULL is returned.

Note that's O(N) since time events are unsorted.
Possible optimizations (not needed by Redis so far, but...):
1) Insert the event in order, so that the nearest is just the head.
Much better but still insertion or deletion of timers is O(N).
2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)).
Search the first timer to fire . This operation is useful to know how many time the select can be put in sleep without to delay any event . If there are no timers NULL is returned . Note that ' s O ( N ) since time events are unsorted . Possible optimizations ( not needed by Redis so far but ... ) : 1 ) Insert the event in order so that the nearest is just the head . Much better but still insertion or deletion of timers is O ( N ) . 2 ) Use a skiplist to have this operation as O ( 1 ) and insertion as O ( log ( N )) .
static aeTimeEvent *aeSearchNearestTimer(aeEventLoop *eventLoop) {
  aeTimeEvent *te = eventLoop->timeEventHead;
  aeTimeEvent *nearest = NULL;
  while (te) {
    if (!nearest || te->when_sec < nearest->when_sec
        || (te->when_sec == nearest->when_sec && te->when_ms < nearest->when_ms))
      nearest = te;
    te = te->next;
  }
  return nearest;
}
static aeTimeEvent * aeSearchNearestTimer ( aeEventLoop * eventLoop ) { aeTimeEvent * te = eventLoop -> timeEventHead ; aeTimeEvent * nearest = NULL ; while ( te ) { if ( ! nearest || te -> when_sec < nearest -> when_sec || ( te -> when_sec == nearest -> when_sec && te -> when_ms < nearest -> when_ms ) ) nearest = te ; te = te -> next ; } return nearest ; }
Search the first timer to fire.
Search the first timer to fire .