comparison mcabber/src/hbuf.c @ 392:6329c9601704

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().
author Mikael Berthe <mikael@lilotux.net>
date Thu, 04 Aug 2005 22:55:10 +0100
parents 468c9cac2798
children f8f3c7493457
comparison
equal deleted inserted replaced
391:868a350fefca 392:6329c9601704
65 hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE); 65 hbuf_block_elt->ptr = g_new(char, HBB_BLOCKSIZE);
66 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT; 66 hbuf_block_elt->flags = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
67 hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE; 67 hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + HBB_BLOCKSIZE;
68 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); 68 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
69 } else { 69 } else {
70 hbuf_block *hbuf_b_prev = g_list_last(hbuf)->data; 70 // Set p_hbuf to the end of the list, to speed up history loading
71 // (or CPU time will be used by g_list_last() for each line)
72 hbuf = *p_hbuf = g_list_last(*p_hbuf);
73 hbuf_block *hbuf_b_prev = hbuf->data;
71 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; 74 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end;
72 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT; 75 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT;
73 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc; 76 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
74 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); 77 g_list_append(*p_hbuf, hbuf_block_elt);
75 } 78 }
76 79
77 if (strlen(text) >= HBB_BLOCKSIZE) { 80 if (strlen(text) >= HBB_BLOCKSIZE) {
78 // Too long 81 // Too long
79 text = "[ERR:LINE_TOO_LONG]"; 82 text = "[ERR:LINE_TOO_LONG]";
107 hbuf_block_elt = g_new0(hbuf_block, 1); 110 hbuf_block_elt = g_new0(hbuf_block, 1);
108 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end + 1; // == cr+1 111 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end + 1; // == cr+1
109 hbuf_block_elt->ptr_end = end; 112 hbuf_block_elt->ptr_end = end;
110 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT; 113 hbuf_block_elt->flags = HBB_FLAG_PERSISTENT;
111 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc; 114 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
112 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); 115 g_list_append(*p_hbuf, hbuf_block_elt);
113 line = hbuf_block_elt->ptr; 116 line = hbuf_block_elt->ptr;
114 } else { 117 } else {
115 // We need to break where we can find a space char 118 // We need to break where we can find a space char
116 char *br; // break pointer 119 char *br; // break pointer
117 for (br = line + width; br > line && *br != 32 && *br != 9; br--) 120 for (br = line + width; br > line && *br != 32 && *br != 9; br--)
125 hbuf_block_elt = g_new0(hbuf_block, 1); 128 hbuf_block_elt = g_new0(hbuf_block, 1);
126 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; // == br 129 hbuf_block_elt->ptr = hbuf_b_prev->ptr_end; // == br
127 hbuf_block_elt->ptr_end = end; 130 hbuf_block_elt->ptr_end = end;
128 hbuf_block_elt->flags = 0; 131 hbuf_block_elt->flags = 0;
129 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc; 132 hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
130 *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt); 133 g_list_append(*p_hbuf, hbuf_block_elt);
131 line = hbuf_block_elt->ptr; 134 line = hbuf_block_elt->ptr;
132 } 135 }
133 cr = strchr(line, '\n'); 136 cr = strchr(line, '\n');
134 } 137 }
135 } 138 }