From e47042fe2604625fffdba015c2a9bdaed30e1f0c Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 2 Feb 2011 20:05:41 -0500 Subject: [PATCH] Optimize the case where we reinsert an existing timeout --- event.c | 36 ++++++++++++++++++++++++++++++++++-- minheap-internal.h | 18 ++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/event.c b/event.c index fa8c722d..63927085 100644 --- a/event.c +++ b/event.c @@ -133,6 +133,8 @@ static inline int event_del_internal(struct event *ev); static void event_queue_insert(struct event_base *, struct event *, int); static void event_queue_remove(struct event_base *, struct event *, int); +static void event_queue_reinsert(struct event_base *,struct event *ev,int); + static int event_haveevents(struct event_base *); static int event_process_active(struct event_base *); @@ -146,6 +148,9 @@ static inline void event_persist_closure(struct event_base *, struct event *ev); static int evthread_notify_base(struct event_base *base); +static void insert_common_timeout_inorder(struct common_timeout_list *ctl, + struct event *ev); + #ifndef _EVENT_DISABLE_DEBUG_MODE /* These functions implement a hashtable of which 'struct event *' structures * have been setup or added. We don't want to trust the content of the struct @@ -2030,7 +2035,7 @@ event_add_internal(struct event *ev, const struct timeval *tv, /* XXX I believe this is needless. */ if (min_heap_elt_is_top(ev)) notify = 1; - event_queue_remove(base, ev, EVLIST_TIMEOUT); + /*event_queue_remove(base, ev, EVLIST_TIMEOUT);*/ } /* Check if it is active due to a timeout. Rescheduling @@ -2070,7 +2075,8 @@ event_add_internal(struct event *ev, const struct timeval *tv, "event_add: timeout in %d seconds, call %p", (int)tv->tv_sec, ev->ev_callback)); - event_queue_insert(base, ev, EVLIST_TIMEOUT); + event_queue_reinsert(base, ev, EVLIST_TIMEOUT); + if (common_timeout) { struct common_timeout_list *ctl = get_common_timeout_list(base, &ev->ev_timeout); @@ -2458,6 +2464,32 @@ event_queue_remove(struct event_base *base, struct event *ev, int queue) } } +/* Remove and reinsert 'ev' into the appropriate queue. Only EVLIST_TIMEOUT + * is supported. */ +static void +event_queue_reinsert(struct event_base *base, struct event *ev, int queue) +{ + if (!(ev->ev_flags & queue)) { + event_queue_insert(base, ev, queue); + return; + } + + if (queue != EVLIST_TIMEOUT) { + event_errx(1, "%s: Unsupported queue %x", __func__, queue); + return; /* unreached */ + } + + if (is_common_timeout(&ev->ev_timeout, base)) { + struct common_timeout_list *ctl = + get_common_timeout_list(base, &ev->ev_timeout); + TAILQ_REMOVE(&ctl->events, ev, + ev_timeout_pos.ev_next_with_common_timeout); + insert_common_timeout_inorder(ctl, ev); + } else { + min_heap_adjust(&base->timeheap, ev); + } +} + /* Add 'ev' to the common timeout list in 'ev'. */ static void insert_common_timeout_inorder(struct common_timeout_list *ctl, diff --git a/minheap-internal.h b/minheap-internal.h index 8055e903..39e3946a 100644 --- a/minheap-internal.h +++ b/minheap-internal.h @@ -53,6 +53,7 @@ static inline struct event* min_heap_top(min_heap_t* s); static inline int min_heap_reserve(min_heap_t* s, unsigned n); static inline int min_heap_push(min_heap_t* s, struct event* e); static inline struct event* min_heap_pop(min_heap_t* s); +static inline int min_heap_adjust(min_heap_t *s, struct event* e); static inline int min_heap_erase(min_heap_t* s, struct event* e); static inline void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e); static inline void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e); @@ -115,6 +116,23 @@ int min_heap_erase(min_heap_t* s, struct event* e) return -1; } +int min_heap_adjust(min_heap_t *s, struct event *e) +{ + if (-1 == e->ev_timeout_pos.min_heap_idx) { + return min_heap_push(s, e); + } else { + unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2; + /* The position of e has changed; we shift it up or down + * as needed. We can't need to do both. */ + if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], e)) + min_heap_shift_up_(s, e->ev_timeout_pos.min_heap_idx, e); + else + min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, e); + return 0; + } + return -1; +} + int min_heap_reserve(min_heap_t* s, unsigned n) { if (s->a < n)