Redis须要你来作的算法优化

         阅读一个优秀的Server内核实现,早期的代码比后期的代码要好得多。由于在早期的代码里,你能够学习到一个***级别的程序猿到底在思考什么。同时,你能看到他哪里写得差劲,以及后来是怎么优化的。html

         若是你一心追求最新的技术,可是,不关心它是怎么成长起来的,方向都走错了。走错了方向,跑得越快,离目标越远。ide

/* 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;
}


Redis在查找最近的时间事件的时候,采用了暴力遍历,时间复杂度是O(N)。优化的方法做者想到了2种,可是他并无实现,或许他以为这个不是最紧迫的事情。oop

须要尝试一下吗?学习

相关文章
相关标签/搜索