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