Optimize the case where we reinsert an existing timeout

This commit is contained in:
Nick Mathewson 2011-02-02 20:05:41 -05:00
parent 86f02d7533
commit e47042fe26
2 changed files with 52 additions and 2 deletions

36
event.c
View File

@ -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,

View File

@ -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)