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.
This commit is contained in:
Kevin Easton
2015-09-03 01:04:12 +10:00
parent c881dd7a9a
commit d62914af15
2 changed files with 23 additions and 15 deletions

View File

@@ -113,7 +113,7 @@ char *get_signoffreason(const char *nick);
#ifdef GUI #ifdef GUI
char *convert_output_format2 (const char *); char *convert_output_format2 (const char *);
#endif #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 check_last_type (LastMsg *, int, char *, char *);
int matchmcommand (char *, int); int matchmcommand (char *, int);
char *convert_time (time_t); char *convert_time (time_t);

View File

@@ -4764,23 +4764,31 @@ char *convert_output_format2(const char *str)
} }
#endif /* GUI */ #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; int i = size - 1;
for (i = size - 1; i > 0; i--) 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--)
{ {
array[i].last_msg = array[i - 1].last_msg;
malloc_strcpy(&array[i].last_msg, array[i - 1].last_msg); array[i].from = array[i - 1].from;
malloc_strcpy(&array[i].from, array[i - 1].from); array[i].uh = array[i - 1].uh;
malloc_strcpy(&array[i].uh, array[i - 1].uh); array[i].to = array[i - 1].to;
malloc_strcpy(&array[i].to, array[i - 1].to); array[i].time = array[i - 1].time;
malloc_strcpy(&array[i].time, array[i - 1].time);
} }
malloc_strcpy(&array->last_msg, str);
malloc_strcpy(&array->from, from); array[0].last_msg = new_msg.last_msg;
malloc_strcpy(&array->to, to); array[0].from = new_msg.from;
malloc_strcpy(&array->uh, uh); array[0].uh = new_msg.uh;
malloc_strcpy(&array->time, update_clock(GET_TIME)); array[0].to = new_msg.to;
array[0].time = new_msg.time;
} }
int check_last_type(LastMsg *array, int size, char *from, char *uh) int check_last_type(LastMsg *array, int size, char *from, char *uh)