466924f
To: vim_dev@googlegroups.com
466924f
Subject: Patch 7.3.434
466924f
Fcc: outbox
466924f
From: Bram Moolenaar <Bram@moolenaar.net>
466924f
Mime-Version: 1.0
466924f
Content-Type: text/plain; charset=UTF-8
466924f
Content-Transfer-Encoding: 8bit
466924f
------------
466924f
466924f
Patch 7.3.434
466924f
Problem:    Using join() can be slow.
466924f
Solution:   Compute the size of the result before allocation to avoid a lot of
466924f
            allocations and copies. (Taro Muraoka)
466924f
Files:      src/eval.c
466924f
466924f
466924f
*** ../vim-7.3.433/src/eval.c	2012-02-05 00:39:14.000000000 +0100
466924f
--- src/eval.c	2012-02-06 00:05:31.000000000 +0100
466924f
***************
466924f
*** 442,447 ****
466924f
--- 442,448 ----
466924f
  static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
466924f
  static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
466924f
  static char_u *list2string __ARGS((typval_T *tv, int copyID));
466924f
+ static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
466924f
  static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
466924f
  static int free_unref_items __ARGS((int copyID));
466924f
  static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
466924f
***************
466924f
*** 6571,6617 ****
466924f
      return (char_u *)ga.ga_data;
466924f
  }
466924f
  
466924f
! /*
466924f
!  * Join list "l" into a string in "*gap", using separator "sep".
466924f
!  * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
466924f
!  * Return FAIL or OK.
466924f
!  */
466924f
      static int
466924f
! list_join(gap, l, sep, echo_style, copyID)
466924f
!     garray_T	*gap;
466924f
      list_T	*l;
466924f
      char_u	*sep;
466924f
      int		echo_style;
466924f
      int		copyID;
466924f
  {
466924f
      int		first = TRUE;
466924f
      char_u	*tofree;
466924f
      char_u	numbuf[NUMBUFLEN];
466924f
      listitem_T	*item;
466924f
      char_u	*s;
466924f
  
466924f
      for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
466924f
      {
466924f
- 	if (first)
466924f
- 	    first = FALSE;
466924f
- 	else
466924f
- 	    ga_concat(gap, sep);
466924f
- 
466924f
  	if (echo_style)
466924f
  	    s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
466924f
  	else
466924f
  	    s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
466924f
- 	if (s != NULL)
466924f
- 	    ga_concat(gap, s);
466924f
- 	vim_free(tofree);
466924f
  	if (s == NULL)
466924f
  	    return FAIL;
466924f
  	line_breakcheck();
466924f
      }
466924f
      return OK;
466924f
  }
466924f
  
466924f
  /*
466924f
   * Garbage collection for lists and dictionaries.
466924f
   *
466924f
   * We use reference counts to be able to free most items right away when they
466924f
--- 6572,6690 ----
466924f
      return (char_u *)ga.ga_data;
466924f
  }
466924f
  
466924f
! typedef struct join_S {
466924f
!     char_u	*s;
466924f
!     char_u	*tofree;
466924f
! } join_T;
466924f
! 
466924f
      static int
466924f
! list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
466924f
!     garray_T	*gap;		/* to store the result in */
466924f
      list_T	*l;
466924f
      char_u	*sep;
466924f
      int		echo_style;
466924f
      int		copyID;
466924f
+     garray_T	*join_gap;	/* to keep each list item string */
466924f
  {
466924f
+     int		i;
466924f
+     join_T	*p;
466924f
+     int		len;
466924f
+     int		sumlen = 0;
466924f
      int		first = TRUE;
466924f
      char_u	*tofree;
466924f
      char_u	numbuf[NUMBUFLEN];
466924f
      listitem_T	*item;
466924f
      char_u	*s;
466924f
  
466924f
+     /* Stringify each item in the list. */
466924f
      for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
466924f
      {
466924f
  	if (echo_style)
466924f
  	    s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
466924f
  	else
466924f
  	    s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
466924f
  	if (s == NULL)
466924f
  	    return FAIL;
466924f
+ 
466924f
+ 	len = (int)STRLEN(s);
466924f
+ 	sumlen += len;
466924f
+ 
466924f
+ 	ga_grow(join_gap, 1);
466924f
+ 	p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
466924f
+ 	if (tofree != NULL || s != numbuf)
466924f
+ 	{
466924f
+ 	    p->s = s;
466924f
+ 	    p->tofree = tofree;
466924f
+ 	}
466924f
+ 	else
466924f
+ 	{
466924f
+ 	    p->s = vim_strnsave(s, len);
466924f
+ 	    p->tofree = p->s;
466924f
+ 	}
466924f
+ 
466924f
+ 	line_breakcheck();
466924f
+     }
466924f
+ 
466924f
+     /* Allocate result buffer with its total size, avoid re-allocation and
466924f
+      * multiple copy operations.  Add 2 for a tailing ']' and NUL. */
466924f
+     if (join_gap->ga_len >= 2)
466924f
+ 	sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
466924f
+     if (ga_grow(gap, sumlen + 2) == FAIL)
466924f
+ 	return FAIL;
466924f
+ 
466924f
+     for (i = 0; i < join_gap->ga_len && !got_int; ++i)
466924f
+     {
466924f
+ 	if (first)
466924f
+ 	    first = FALSE;
466924f
+ 	else
466924f
+ 	    ga_concat(gap, sep);
466924f
+ 	p = ((join_T *)join_gap->ga_data) + i;
466924f
+ 
466924f
+ 	if (p->s != NULL)
466924f
+ 	    ga_concat(gap, p->s);
466924f
  	line_breakcheck();
466924f
      }
466924f
+ 
466924f
      return OK;
466924f
  }
466924f
  
466924f
  /*
466924f
+  * Join list "l" into a string in "*gap", using separator "sep".
466924f
+  * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
466924f
+  * Return FAIL or OK.
466924f
+  */
466924f
+     static int
466924f
+ list_join(gap, l, sep, echo_style, copyID)
466924f
+     garray_T	*gap;
466924f
+     list_T	*l;
466924f
+     char_u	*sep;
466924f
+     int		echo_style;
466924f
+     int		copyID;
466924f
+ {
466924f
+     garray_T	join_ga;
466924f
+     int		retval;
466924f
+     join_T	*p;
466924f
+     int		i;
466924f
+ 
466924f
+     ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
466924f
+     retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
466924f
+ 
466924f
+     /* Dispose each item in join_ga. */
466924f
+     if (join_ga.ga_data != NULL)
466924f
+     {
466924f
+ 	p = (join_T *)join_ga.ga_data;
466924f
+ 	for (i = 0; i < join_ga.ga_len; ++i)
466924f
+ 	{
466924f
+ 	    vim_free(p->tofree);
466924f
+ 	    ++p;
466924f
+ 	}
466924f
+ 	ga_clear(&join_ga);
466924f
+     }
466924f
+ 
466924f
+     return retval;
466924f
+ }
466924f
+ 
466924f
+ /*
466924f
   * Garbage collection for lists and dictionaries.
466924f
   *
466924f
   * We use reference counts to be able to free most items right away when they
466924f
***************
466924f
*** 13406,13412 ****
466924f
      char_u	*rhs;
466924f
      int		mode;
466924f
      int		abbr = FALSE;
466924f
!     int         get_dict = FALSE;
466924f
      mapblock_T	*mp;
466924f
      int		buffer_local;
466924f
  
466924f
--- 13479,13485 ----
466924f
      char_u	*rhs;
466924f
      int		mode;
466924f
      int		abbr = FALSE;
466924f
!     int		get_dict = FALSE;
466924f
      mapblock_T	*mp;
466924f
      int		buffer_local;
466924f
  
466924f
*** ../vim-7.3.433/src/version.c	2012-02-05 23:10:25.000000000 +0100
466924f
--- src/version.c	2012-02-06 00:10:23.000000000 +0100
466924f
***************
466924f
*** 716,717 ****
466924f
--- 716,719 ----
466924f
  {   /* Add new patch number below this line */
466924f
+ /**/
466924f
+     434,
466924f
  /**/
466924f
466924f
-- 
466924f
hundred-and-one symptoms of being an internet addict:
466924f
30. Even though you died last week, you've managed to retain OPS on your
466924f
    favorite IRC channel.
466924f
466924f
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
466924f
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
466924f
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
466924f
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///