From 307e7f7ce8552d7e7c17e198f0f87b0d83174aac Mon Sep 17 00:00:00 2001 From: Thad Fleming Date: Tue, 16 Apr 2024 16:23:57 +0000 Subject: [PATCH 1/6] Speed up Trick::ScheduledJobQueue::push --- .../ScheduledJobQueue/ScheduledJobQueue.cpp | 98 +++++++------------ 1 file changed, 37 insertions(+), 61 deletions(-) diff --git a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp index c05e86914..b7d11a551 100644 --- a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp +++ b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp @@ -1,8 +1,9 @@ - +#include #include #include #include #include +#include #include "trick/ScheduledJobQueue.hh" #include "trick/ScheduledJobQueueInstrument.hh" @@ -34,6 +35,30 @@ Trick::ScheduledJobQueue::~ScheduledJobQueue( ) { } } +struct JobDataCompare { + bool operator()(const Trick::JobData *a, const Trick::JobData *b) { + { + auto ajc = a->job_class; + auto bjc = b->job_class; + if (ajc < bjc) return true; + if (ajc > bjc) return false; + } + { + auto ap = a->phase; + auto bp = b->phase; + if (ap < bp) return true; + if (ap > bp) return false; + } + { + auto asoi = a->sim_object_id; + auto bsoi = b->sim_object_id; + if (asoi < bsoi) return true; + if (asoi > bsoi) return false; + } + return a->id < b->id; + } +}; + /** @design -# Allocate additional memory for the incoming job @@ -46,72 +71,24 @@ Trick::ScheduledJobQueue::~ScheduledJobQueue( ) { */ int Trick::ScheduledJobQueue::push( JobData * new_job ) { - unsigned int ii , jj ; - /* Allocate additional memory for the additional job in the queue */ - JobData ** new_list = (JobData **)calloc( list_size + 1 , sizeof(JobData *)) ; - - new_job->set_handled(true) ; - - /* Find the correct insertion spot in the queue by comparing - the job_class, the phase, the sim_object id, and the job_id in that order. */ - /* While searching for the correct insertion spot, copy all jobs that precede - the incoming job to the newly allocated queue space. */ - for ( ii = jj = 0 ; ii < list_size ; ii++ ) { - if ( list[ii]->job_class == new_job->job_class ) { - if ( list[ii]->phase == new_job->phase ) { - if ( list[ii]->sim_object_id == new_job->sim_object_id ) { - if ( list[ii]->id <= new_job->id ) { - new_list[jj++] = list[ii] ; - } else { - new_list[jj++] = new_job ; - break ; - } - } else if ( list[ii]->sim_object_id < new_job->sim_object_id ) { - new_list[jj++] = list[ii] ; - } else { - new_list[jj++] = new_job ; - break ; - } - } else if ( list[ii]->phase < new_job->phase ) { - new_list[jj++] = list[ii] ; - } else { - new_list[jj++] = new_job ; - break ; - } - } else if ( list[ii]->job_class < new_job->job_class ) { - new_list[jj++] = list[ii] ; - } else { - new_list[jj++] = new_job ; - break ; - } + JobData ** new_list = (JobData **)realloc(list, (list_size + 1) * sizeof(JobData *)) ; + if (!new_list) { + abort(); } - - /* Copy remaining jobs that execute after the incoming job to the new queue space. */ - if ( ii == list_size ) { - /* ii == list_size means the incoming job is the last job */ - new_list[list_size] = new_job ; - } else { - /* Inserted new job before the current job. Increment curr_index to point to the correct job */ - if ( ii < curr_index ) { - curr_index++ ; - } - for ( ; ii < list_size ; ii++ ) { - new_list[jj++] = list[ii] ; - } + list = new_list; + JobData** list_end = list + list_size; + JobData** insert_pt = std::lower_bound(list, list_end, new_job, JobDataCompare()); + if (insert_pt != list_end) { + memmove(insert_pt + 1, insert_pt, (list_end - insert_pt) * sizeof(JobData*)); } + *insert_pt = new_job; + + new_job->set_handled(true) ; /* Increment the size of the queue */ list_size++ ; - /* Free the old queue space */ - if ( list ) { - free(list) ; - } - - /* Assign the queue pointer to the new space */ - list = new_list ; - return(0) ; } @@ -534,4 +511,3 @@ int Trick::ScheduledJobQueue::instrument_remove(std::string job_name) { return 0 ; } - From 1e65bef1fd6cf78807838ef32654a50f82b67813 Mon Sep 17 00:00:00 2001 From: Thad Fleming Date: Tue, 16 Apr 2024 16:37:52 +0000 Subject: [PATCH 2/6] Make comparator a static function --- .../ScheduledJobQueue/ScheduledJobQueue.cpp | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp index b7d11a551..ea37fe03d 100644 --- a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp +++ b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp @@ -35,29 +35,33 @@ Trick::ScheduledJobQueue::~ScheduledJobQueue( ) { } } -struct JobDataCompare { - bool operator()(const Trick::JobData *a, const Trick::JobData *b) { - { - auto ajc = a->job_class; - auto bjc = b->job_class; - if (ajc < bjc) return true; - if (ajc > bjc) return false; - } - { - auto ap = a->phase; - auto bp = b->phase; - if (ap < bp) return true; - if (ap > bp) return false; - } - { - auto asoi = a->sim_object_id; - auto bsoi = b->sim_object_id; - if (asoi < bsoi) return true; - if (asoi > bsoi) return false; - } - return a->id < b->id; +static bool compare_job_data(const Trick::JobData *a, const Trick::JobData *b) { + { + auto ajc = a->job_class; + auto bjc = b->job_class; + if (ajc < bjc) + return true; + if (ajc > bjc) + return false; } -}; + { + auto ap = a->phase; + auto bp = b->phase; + if (ap < bp) + return true; + if (ap > bp) + return false; + } + { + auto asoi = a->sim_object_id; + auto bsoi = b->sim_object_id; + if (asoi < bsoi) + return true; + if (asoi > bsoi) + return false; + } + return a->id < b->id; +} /** @design @@ -78,7 +82,7 @@ int Trick::ScheduledJobQueue::push( JobData * new_job ) { } list = new_list; JobData** list_end = list + list_size; - JobData** insert_pt = std::lower_bound(list, list_end, new_job, JobDataCompare()); + JobData** insert_pt = std::lower_bound(list, list_end, new_job, compare_job_data); if (insert_pt != list_end) { memmove(insert_pt + 1, insert_pt, (list_end - insert_pt) * sizeof(JobData*)); } From 60e17896aa40483b9790b7038937f703bad60dc6 Mon Sep 17 00:00:00 2001 From: Thad Fleming Date: Tue, 16 Apr 2024 19:24:27 +0000 Subject: [PATCH 3/6] Use upper_bound instead --- .../sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp index ea37fe03d..c86008c19 100644 --- a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp +++ b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp @@ -82,7 +82,7 @@ int Trick::ScheduledJobQueue::push( JobData * new_job ) { } list = new_list; JobData** list_end = list + list_size; - JobData** insert_pt = std::lower_bound(list, list_end, new_job, compare_job_data); + JobData** insert_pt = std::upper_bound(list, list_end, new_job, compare_job_data); if (insert_pt != list_end) { memmove(insert_pt + 1, insert_pt, (list_end - insert_pt) * sizeof(JobData*)); } From dfb82b770d9eaa83d7e0eecd781fcc839864e466 Mon Sep 17 00:00:00 2001 From: Thad Fleming Date: Tue, 16 Apr 2024 19:37:57 +0000 Subject: [PATCH 4/6] Use explicit types --- .../ScheduledJobQueue/ScheduledJobQueue.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp index c86008c19..de4e5c2d4 100644 --- a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp +++ b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp @@ -37,24 +37,24 @@ Trick::ScheduledJobQueue::~ScheduledJobQueue( ) { static bool compare_job_data(const Trick::JobData *a, const Trick::JobData *b) { { - auto ajc = a->job_class; - auto bjc = b->job_class; + int ajc = a->job_class; + int bjc = b->job_class; if (ajc < bjc) return true; if (ajc > bjc) return false; } { - auto ap = a->phase; - auto bp = b->phase; + unsigned short ap = a->phase; + unsigned short bp = b->phase; if (ap < bp) return true; if (ap > bp) return false; } { - auto asoi = a->sim_object_id; - auto bsoi = b->sim_object_id; + int asoi = a->sim_object_id; + int bsoi = b->sim_object_id; if (asoi < bsoi) return true; if (asoi > bsoi) From a0e87a2a3f89312c9ef6d177d2cdf4391287d88e Mon Sep 17 00:00:00 2001 From: Thad Fleming Date: Fri, 19 Apr 2024 17:38:15 +0000 Subject: [PATCH 5/6] Update comment --- .../sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp index de4e5c2d4..2972e19f9 100644 --- a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp +++ b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp @@ -68,9 +68,8 @@ static bool compare_job_data(const Trick::JobData *a, const Trick::JobData *b) { -# Allocate additional memory for the incoming job -# Find the insertion point in the queue based on the job_class, the phase, the sim_object id, and the job_id --# While searching for the correct insertion spot, copy all jobs that precede - the incoming job to the newly allocated queue space followed by the new job. --# Copy jobs that are ordered after the incoming job to the new queue +-# Move the jobs after the insertion point to the right by one. +-# Insert the new job at the insertion point. -# Increment the size of the queue. */ int Trick::ScheduledJobQueue::push( JobData * new_job ) { From 998545802d2b6f9e13fa8e37a7639d64c9c17001 Mon Sep 17 00:00:00 2001 From: Thad Fleming Date: Fri, 19 Apr 2024 17:38:22 +0000 Subject: [PATCH 6/6] Fix formatting --- .../sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp index 2972e19f9..85824cf89 100644 --- a/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp +++ b/trick_source/sim_services/ScheduledJobQueue/ScheduledJobQueue.cpp @@ -80,10 +80,10 @@ int Trick::ScheduledJobQueue::push( JobData * new_job ) { abort(); } list = new_list; - JobData** list_end = list + list_size; - JobData** insert_pt = std::upper_bound(list, list_end, new_job, compare_job_data); + JobData ** list_end = list + list_size; + JobData ** insert_pt = std::upper_bound(list, list_end, new_job, compare_job_data); if (insert_pt != list_end) { - memmove(insert_pt + 1, insert_pt, (list_end - insert_pt) * sizeof(JobData*)); + memmove(insert_pt + 1, insert_pt, (list_end - insert_pt) * sizeof(JobData *)); } *insert_pt = new_job;