Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

timers: Avoid linear scan in _unrefActive. #8225

Closed
wants to merge 1 commit into from

Commits on Aug 22, 2014

  1. timers: Avoid linear scan in _unrefActive.

    Before this change, _unrefActive would keep the unrefList sorted when
    adding a new timer.
    
    Because _unrefActive is called extremely frequently, this linear scan
    (O(n) at worse) would make _unrefActive show high in the list of
    contributors when profiling CPU usage.
    
    This commit changes _unrefActive so that it doesn't try to keep the
    unrefList sorted. The insertion thus happens in constant time.
    
    However, when a timer expires, unrefTimeout has to go through the whole
    unrefList because it's not ordered anymore.
    
    It is usually not large enough to have a significant impact on
    performance because:
    - Most of the time, the timers will be removed before unrefTimeout is
      called because their users (sockets mainly) cancel them when an I/O
      operation takes place.
    - If they're not, it means that some I/O took a long time to happen, and
      the initiator of subsequents I/O operations that would add more timers
      has to wait for them to complete.
    
    With this change, _unrefActive does not show as a significant
    contributor in CPU profiling reports anymore.
    
    Fixes nodejs#8160.
    Julien Gilli committed Aug 22, 2014
    Configuration menu
    Copy the full SHA
    aefb864 View commit details
    Browse the repository at this point in the history