Speed up hbuf_add_line()
When loading a history file, *p_hbuf was a pointer to the head of the list of
lines, so a lot of CPU time was lost in g_list_last().
--- a/mcabber/src/hbuf.c Thu Aug 04 09:00:54 2005 +0100
+++ b/mcabber/src/hbuf.c Thu Aug 04 22:55:10 2005 +0100
@@ -67,11 +67,14 @@
hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE;
*p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
} else {
- hbuf_block *hbuf_b_prev = g_list_last(hbuf)->data;
+ // Set p_hbuf to the end of the list, to speed up history loading
+ // (or CPU time will be used by g_list_last() for each line)
+ hbuf = *p_hbuf = g_list_last(*p_hbuf);
+ hbuf_block *hbuf_b_prev = hbuf->data;
hbuf_block_elt->ptr = hbuf_b_prev->ptr_end;
hbuf_block_elt->flags = HBB_FLAG_PERSISTENT;
hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
- *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
+ g_list_append(*p_hbuf, hbuf_block_elt);
}
if (strlen(text) >= HBB_BLOCKSIZE) {
@@ -109,7 +112,7 @@
hbuf_block_elt->ptr_end = end;
hbuf_block_elt->flags = HBB_FLAG_PERSISTENT;
hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
- *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
+ g_list_append(*p_hbuf, hbuf_block_elt);
line = hbuf_block_elt->ptr;
} else {
// We need to break where we can find a space char
@@ -127,7 +130,7 @@
hbuf_block_elt->ptr_end = end;
hbuf_block_elt->flags = 0;
hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
- *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
+ g_list_append(*p_hbuf, hbuf_block_elt);
line = hbuf_block_elt->ptr;
}
cr = strchr(line, '\n');