1 To: vim_dev@googlegroups.com
4 From: Bram Moolenaar <Bram@moolenaar.net>
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
11 Problem: Using join() can be slow.
12 Solution: Compute the size of the result before allocation to avoid a lot of
13 allocations and copies. (Taro Muraoka)
17 *** ../vim-7.3.433/src/eval.c 2012-02-05 00:39:14.000000000 +0100
18 --- src/eval.c 2012-02-06 00:05:31.000000000 +0100
22 static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
23 static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
24 static char_u *list2string __ARGS((typval_T *tv, int copyID));
25 + static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
26 static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
27 static int free_unref_items __ARGS((int copyID));
28 static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
31 return (char_u *)ga.ga_data;
35 ! * Join list "l" into a string in "*gap", using separator "sep".
36 ! * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
37 ! * Return FAIL or OK.
40 ! list_join(gap, l, sep, echo_style, copyID)
49 char_u numbuf[NUMBUFLEN];
53 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
58 - ga_concat(gap, sep);
61 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
63 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
75 * Garbage collection for lists and dictionaries.
77 * We use reference counts to be able to free most items right away when they
79 return (char_u *)ga.ga_data;
82 ! typedef struct join_S {
88 ! list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
89 ! garray_T *gap; /* to store the result in */
94 + garray_T *join_gap; /* to keep each list item string */
102 char_u numbuf[NUMBUFLEN];
106 + /* Stringify each item in the list. */
107 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
110 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
112 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
116 + len = (int)STRLEN(s);
119 + ga_grow(join_gap, 1);
120 + p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
121 + if (tofree != NULL || s != numbuf)
124 + p->tofree = tofree;
128 + p->s = vim_strnsave(s, len);
135 + /* Allocate result buffer with its total size, avoid re-allocation and
136 + * multiple copy operations. Add 2 for a tailing ']' and NUL. */
137 + if (join_gap->ga_len >= 2)
138 + sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
139 + if (ga_grow(gap, sumlen + 2) == FAIL)
142 + for (i = 0; i < join_gap->ga_len && !got_int; ++i)
147 + ga_concat(gap, sep);
148 + p = ((join_T *)join_gap->ga_data) + i;
151 + ga_concat(gap, p->s);
159 + * Join list "l" into a string in "*gap", using separator "sep".
160 + * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
161 + * Return FAIL or OK.
164 + list_join(gap, l, sep, echo_style, copyID)
176 + ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
177 + retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
179 + /* Dispose each item in join_ga. */
180 + if (join_ga.ga_data != NULL)
182 + p = (join_T *)join_ga.ga_data;
183 + for (i = 0; i < join_ga.ga_len; ++i)
185 + vim_free(p->tofree);
188 + ga_clear(&join_ga);
195 * Garbage collection for lists and dictionaries.
197 * We use reference counts to be able to free most items right away when they
203 ! int get_dict = FALSE;
211 ! int get_dict = FALSE;
215 *** ../vim-7.3.433/src/version.c 2012-02-05 23:10:25.000000000 +0100
216 --- src/version.c 2012-02-06 00:10:23.000000000 +0100
220 { /* Add new patch number below this line */
226 hundred-and-one symptoms of being an internet addict:
227 30. Even though you died last week, you've managed to retain OPS on your
228 favorite IRC channel.
230 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
231 /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
232 \\\ an exciting new programming language -- http://www.Zimbu.org ///
233 \\\ help me help AIDS victims -- http://ICCF-Holland.org ///