From 05fd64dd1611610e2efe596718c657343db839e1 Mon Sep 17 00:00:00 2001 From: ailin-nemui Date: Thu, 4 Oct 2018 09:21:46 +0200 Subject: [PATCH] improve rawlog perf --- src/core/rawlog.c | 18 +++++++----------- src/core/rawlog.h | 3 +-- src/perl/perl-common.c | 2 +- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/core/rawlog.c b/src/core/rawlog.c index e6bb7017..8f2d7142 100644 --- a/src/core/rawlog.c +++ b/src/core/rawlog.c @@ -48,8 +48,8 @@ void rawlog_destroy(RAWLOG_REC *rawlog) { g_return_if_fail(rawlog != NULL); - g_slist_foreach(rawlog->lines, (GFunc) g_free, NULL); - g_slist_free(rawlog->lines); + g_queue_foreach(rawlog->lines, (GFunc) g_free, NULL); + g_queue_free(rawlog->lines); if (rawlog->logging) { write_buffer_flush(); @@ -61,12 +61,8 @@ void rawlog_destroy(RAWLOG_REC *rawlog) /* NOTE! str must be dynamically allocated and must not be freed after! */ static void rawlog_add(RAWLOG_REC *rawlog, char *str) { - if (rawlog->nlines < rawlog_lines || rawlog_lines <= 2) - rawlog->nlines++; - else { - void *tmp = rawlog->lines->data; - rawlog->lines = g_slist_remove(rawlog->lines, - rawlog->lines->data); + if (rawlog->lines->length >= rawlog_lines && rawlog_lines > 0) { + void *tmp = g_queue_pop_tail(rawlog->lines); g_free(tmp); } @@ -75,7 +71,7 @@ static void rawlog_add(RAWLOG_REC *rawlog, char *str) write_buffer(rawlog->handle, "\n", 1); } - rawlog->lines = g_slist_append(rawlog->lines, str); + g_queue_push_head(rawlog->lines, str); signal_emit_id(signal_rawlog, 2, rawlog, str); } @@ -105,10 +101,10 @@ void rawlog_redirect(RAWLOG_REC *rawlog, const char *str) static void rawlog_dump(RAWLOG_REC *rawlog, int f) { - GSList *tmp; + GList *tmp; ssize_t ret = 0; - for (tmp = rawlog->lines; ret != -1 && tmp != NULL; tmp = tmp->next) { + for (tmp = rawlog->lines->tail; ret != -1 && tmp != NULL; tmp = tmp->prev) { ret = write(f, tmp->data, strlen((char *) tmp->data)); if (ret != -1) ret = write(f, "\n", 1); diff --git a/src/core/rawlog.h b/src/core/rawlog.h index 9e460a83..01175e67 100644 --- a/src/core/rawlog.h +++ b/src/core/rawlog.h @@ -5,8 +5,7 @@ struct _RAWLOG_REC { int logging; int handle; - int nlines; - GSList *lines; + GQueue *lines; }; RAWLOG_REC *rawlog_create(void); diff --git a/src/perl/perl-common.c b/src/perl/perl-common.c index 1d08319f..802a771e 100644 --- a/src/perl/perl-common.c +++ b/src/perl/perl-common.c @@ -501,7 +501,7 @@ static void perl_log_item_fill_hash(HV *hv, LOG_ITEM_REC *item) static void perl_rawlog_fill_hash(HV *hv, RAWLOG_REC *rawlog) { (void) hv_store(hv, "logging", 7, newSViv(rawlog->logging), 0); - (void) hv_store(hv, "nlines", 6, newSViv(rawlog->nlines), 0); + (void) hv_store(hv, "nlines", 6, newSViv(rawlog->lines->length), 0); } static void perl_reconnect_fill_hash(HV *hv, RECONNECT_REC *reconnect)