From 0e44ea891645044f47bf3754e141045ee3b24324 Mon Sep 17 00:00:00 2001
From: dequis <dx@dxzone.com.ar>
Date: Mon, 5 Jun 2017 16:05:00 -0300
Subject: [PATCH 1/2] Performance improvements for /lastlog with big result
 sets

This applies to "/lastlog" with no filters (or with filters that don't
filter a lot) and with large amounts of text in the scrollback.

Test case:

    /exec seq 1 500000
    /lastlog -file log.txt

Thanks to morning for reporting this.
---
 src/fe-text/textbuffer.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/fe-text/textbuffer.c b/src/fe-text/textbuffer.c
index 3668f4c7..fdb95451 100644
--- a/src/fe-text/textbuffer.c
+++ b/src/fe-text/textbuffer.c
@@ -616,21 +616,23 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
 			}
 
 			for (; pre_line != line; pre_line = pre_line->next)
-				matches = g_list_append(matches, pre_line);
+				matches = g_list_prepend(matches, pre_line);
 
 			match_after = after;
 		}
 
 		if (line_matched || match_after > 0) {
 			/* matched */
-			matches = g_list_append(matches, line);
+			matches = g_list_prepend(matches, line);
 
 			if ((!line_matched && --match_after == 0) ||
 			    (line_matched && match_after == 0 && before > 0))
-				matches = g_list_append(matches, NULL);
+				matches = g_list_prepend(matches, NULL);
 		}
 	}
 
+	matches = g_list_reverse(matches);
+
 #ifdef USE_GREGEX
 	if (preg != NULL)
 		g_regex_unref(preg);

From e498265328bd619b231ea4c985734ea43bf89696 Mon Sep 17 00:00:00 2001
From: dequis <dx@dxzone.com.ar>
Date: Mon, 5 Jun 2017 18:04:20 -0300
Subject: [PATCH 2/2] Performance improvements for /lastlog -before

This avoids the use of g_list_find() to find if a match was already
added to the list of results, by checking the last two added matches
instead.

Checking just the last match isn't enough because a NULL match is added
as a separator (shown as -- in the UI)
---
 src/fe-text/textbuffer.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/fe-text/textbuffer.c b/src/fe-text/textbuffer.c
index fdb95451..1f587f97 100644
--- a/src/fe-text/textbuffer.c
+++ b/src/fe-text/textbuffer.c
@@ -610,7 +610,8 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
 			pre_line = line;
 			for (i = 0; i < before; i++) {
 				if (pre_line->prev == NULL ||
-				    g_list_find(matches, pre_line->prev) != NULL)
+				    g_list_nth_data(matches, 0) == pre_line->prev ||
+				    g_list_nth_data(matches, 1) == pre_line->prev)
 					break;
                                 pre_line = pre_line->prev;
 			}