From d62914af15d577d3d622aed494a473e3102868d2 Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Thu, 3 Sep 2015 01:04:12 +1000 Subject: [PATCH] Optimise add_last_type() and change it to accept const char * pointers The optimisation reduces the number of malloc_strcpy() calls, from 50 to 5 for most last types. --- include/misc.h | 2 +- source/misc.c | 36 ++++++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/include/misc.h b/include/misc.h index 62332e1..021632f 100644 --- a/include/misc.h +++ b/include/misc.h @@ -113,7 +113,7 @@ char *get_signoffreason(const char *nick); #ifdef GUI char *convert_output_format2 (const char *); #endif - void add_last_type (LastMsg *, int, char *, char *, char *, char *); +void add_last_type(LastMsg *array, int size, const char *from, const char *uh, const char *to, const char *msg); int check_last_type (LastMsg *, int, char *, char *); int matchmcommand (char *, int); char *convert_time (time_t); diff --git a/source/misc.c b/source/misc.c index 3de799f..e39aea1 100644 --- a/source/misc.c +++ b/source/misc.c @@ -4764,23 +4764,31 @@ char *convert_output_format2(const char *str) } #endif /* GUI */ -void add_last_type (LastMsg *array, int size, char *from, char *uh, char *to, char *str) +void add_last_type(LastMsg *array, int size, const char *from, const char *uh, const char *to, const char *msg) { -int i; - for (i = size - 1; i > 0; i--) + int i = size - 1; + LastMsg new_msg = array[i]; /* Re-use allocations from last entry */ + + malloc_strcpy(&new_msg.last_msg, msg); + malloc_strcpy(&new_msg.from, from); + malloc_strcpy(&new_msg.to, to); + malloc_strcpy(&new_msg.uh, uh); + malloc_strcpy(&new_msg.time, update_clock(GET_TIME)); + + for (; i > 0; i--) { - - malloc_strcpy(&array[i].last_msg, array[i - 1].last_msg); - malloc_strcpy(&array[i].from, array[i - 1].from); - malloc_strcpy(&array[i].uh, array[i - 1].uh); - malloc_strcpy(&array[i].to, array[i - 1].to); - malloc_strcpy(&array[i].time, array[i - 1].time); + array[i].last_msg = array[i - 1].last_msg; + array[i].from = array[i - 1].from; + array[i].uh = array[i - 1].uh; + array[i].to = array[i - 1].to; + array[i].time = array[i - 1].time; } - malloc_strcpy(&array->last_msg, str); - malloc_strcpy(&array->from, from); - malloc_strcpy(&array->to, to); - malloc_strcpy(&array->uh, uh); - malloc_strcpy(&array->time, update_clock(GET_TIME)); + + array[0].last_msg = new_msg.last_msg; + array[0].from = new_msg.from; + array[0].uh = new_msg.uh; + array[0].to = new_msg.to; + array[0].time = new_msg.time; } int check_last_type(LastMsg *array, int size, char *from, char *uh)