From ecd0d5ee2b2b058662d0e773094d0f9f3a3e08f9 Mon Sep 17 00:00:00 2001 From: Botond Baranyi <botond.baranyi@ericsson.com> Date: Wed, 22 May 2019 14:43:05 +0200 Subject: [PATCH] Enhanced performance of TIMER::get_min_expiration (bug 547552) Change-Id: I9eb9a55a13d4edba47d39f3974c9279a7a5d493d Signed-off-by: Botond Baranyi <botond.baranyi@ericsson.com> --- core/Timer.cc | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/core/Timer.cc b/core/Timer.cc index fb13bcb0c..54751f0b7 100644 --- a/core/Timer.cc +++ b/core/Timer.cc @@ -29,11 +29,41 @@ void TIMER::add_to_list() { // do nothing if already a member of list if (this == list_head || list_prev != NULL) return; - if (list_head == NULL) list_head = this; - else if (list_tail != NULL) list_tail->list_next = this; - list_prev = list_tail; - list_next = NULL; - list_tail = this; + if (list_head == NULL) { + // the list is empty, this will be its only element + list_head = this; + list_tail = this; + list_next = NULL; + list_prev = NULL; + } + else { + // insert this timer into the list based on its expiration (in an ascending order) + TIMER* prev = NULL; + TIMER* next = list_head; + while (next != NULL) { + if (next->t_expires > t_expires) { + break; + } + prev = next; + next = next->list_next; + } + if (prev != NULL) { + prev->list_next = this; + } + else { + // this will be the first element + list_head = this; + } + list_prev = prev; + list_next = next; + if (next != NULL) { + next->list_prev = this; + } + else { + // this will be the last element + list_tail = this; + } + } } void TIMER::remove_from_list() @@ -137,6 +167,8 @@ void TIMER::start(double start_val) is_started = TRUE; } TTCN_Logger::log_timer_start(timer_name, start_val); + t_started = TTCN_Snapshot::time_now(); + t_expires = t_started + start_val; add_to_list(); } else { if (start_val < 0.0) @@ -148,9 +180,9 @@ void TIMER::start(double start_val) } is_started = TRUE; TTCN_Logger::log_timer_guard(start_val); + t_started = TTCN_Snapshot::time_now(); + t_expires = t_started + start_val; } - t_started = TTCN_Snapshot::time_now(); - t_expires = t_started + start_val; } void TIMER::start(const FLOAT& start_val) @@ -283,6 +315,7 @@ boolean TIMER::get_min_expiration(double& min_val) else if (!min_flag || list_iter->t_expires < min_val) { min_val = list_iter->t_expires; min_flag = TRUE; + break; } } return min_flag; -- GitLab