From 0b4fb9bd992c6bb78f3aa86a67aa2879e2b64c3d Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Fri, 23 Sep 2011 15:58:18 +0300 Subject: [PATCH] Code optimization: avoid lot of calls for alloc/free memory in cycle. Signed-off-by: Slava Zanko --- src/editor/editcmd.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index 7e7ffc9ad..a73b3d98e 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -1216,13 +1216,15 @@ edit_collect_completions (WEdit * edit, long word_start, gsize word_len, current_word = edit_collect_completions_get_current_word (edit, srch, word_start); + temp = g_string_new (""); + /* collect max MAX_WORD_COMPLETIONS completions */ while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len)) { + g_string_set_size (temp, 0); start = srch->normal_offset; /* add matched completion if not yet added */ - temp = g_string_new (""); for (i = 0; i < len; i++) { skip = edit_get_byte (edit, start + i); @@ -1240,10 +1242,7 @@ edit_collect_completions (WEdit * edit, long word_start, gsize word_len, continue; if (current_word != NULL && strcmp (current_word, temp->str) == 0) - { - g_string_free (temp, TRUE); continue; - } skip = 0; @@ -1264,10 +1263,7 @@ edit_collect_completions (WEdit * edit, long word_start, gsize word_len, } } if (skip != 0) - { - g_string_free (temp, TRUE); continue; - } if (*num == MAX_WORD_COMPLETIONS) { @@ -1284,26 +1280,25 @@ edit_collect_completions (WEdit * edit, long word_start, gsize word_len, recoded = str_convert_to_display (temp->str); if (recoded && recoded->len) - { - g_string_free (temp, TRUE); - temp = recoded; - } - else - g_string_free (recoded, TRUE); + g_string_assign (temp, recoded->str); + + g_string_free (recoded, TRUE); } #endif - compl[*num].text = temp->str; + compl[*num].text = g_strdup (temp->str); compl[*num].len = temp->len; (*num)++; start += len; - g_string_free (temp, FALSE); /* note the maximal length needed for the completion dialog */ if (len > max_len) max_len = len; } + mc_search_free (srch); + g_string_free (temp, TRUE); g_free (current_word); + return max_len; } -- 2.11.4.GIT