summaryrefslogtreecommitdiffstats
path: root/source/ap/vim/patches/7.3.427
blob: 61df881228e978832c049a076915f9386388503e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
To: vim_dev@googlegroups.com
Subject: Patch 7.3.427
Fcc: outbox
From: Bram Moolenaar <Bram@moolenaar.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
------------

Patch 7.3.427
Problem:    readfile() can be slow with long lines.
Solution:   Use realloc() instead of alloc(). (John Little)
Files:      src/eval.c


*** ../vim-7.3.426/src/eval.c	2012-01-26 14:32:26.000000000 +0100
--- src/eval.c	2012-02-05 00:25:39.000000000 +0100
***************
*** 14325,14346 ****
      typval_T	*rettv;
  {
      int		binary = FALSE;
      char_u	*fname;
      FILE	*fd;
!     listitem_T	*li;
! #define FREAD_SIZE 200	    /* optimized for text lines */
!     char_u	buf[FREAD_SIZE];
!     int		readlen;    /* size of last fread() */
!     int		buflen;	    /* nr of valid chars in buf[] */
!     int		filtd;	    /* how much in buf[] was NUL -> '\n' filtered */
!     int		tolist;	    /* first byte in buf[] still to be put in list */
!     int		chop;	    /* how many CR to chop off */
!     char_u	*prev = NULL;	/* previously read bytes, if any */
!     int		prevlen = 0;    /* length of "prev" if not NULL */
!     char_u	*s;
!     int		len;
!     long	maxline = MAXLNUM;
!     long	cnt = 0;
  
      if (argvars[1].v_type != VAR_UNKNOWN)
      {
--- 14325,14343 ----
      typval_T	*rettv;
  {
      int		binary = FALSE;
+     int		failed = FALSE;
      char_u	*fname;
      FILE	*fd;
!     char_u	buf[(IOSIZE/256)*256];	/* rounded to avoid odd + 1 */
!     int		io_size = sizeof(buf);
!     int		readlen;		/* size of last fread() */
!     char_u	*prev	 = NULL;	/* previously read bytes, if any */
!     long	prevlen  = 0;		/* length of data in prev */
!     long	prevsize = 0;		/* size of prev buffer */
!     long	maxline  = MAXLNUM;
!     long	cnt	 = 0;
!     char_u	*p;			/* position in buf */
!     char_u	*start;			/* start of current line */
  
      if (argvars[1].v_type != VAR_UNKNOWN)
      {
***************
*** 14362,14410 ****
  	return;
      }
  
-     filtd = 0;
      while (cnt < maxline || maxline < 0)
      {
! 	readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
! 	buflen = filtd + readlen;
! 	tolist = 0;
! 	for ( ; filtd < buflen || readlen <= 0; ++filtd)
! 	{
! 	    if (readlen <= 0 || buf[filtd] == '\n')
! 	    {
! 		/* In binary mode add an empty list item when the last
! 		 * non-empty line ends in a '\n'. */
! 		if (!binary && readlen == 0 && filtd == 0 && prev == NULL)
! 		    break;
  
! 		/* Found end-of-line or end-of-file: add a text line to the
! 		 * list. */
! 		chop = 0;
! 		if (!binary)
! 		    while (filtd - chop - 1 >= tolist
! 					  && buf[filtd - chop - 1] == '\r')
! 			++chop;
! 		len = filtd - tolist - chop;
! 		if (prev == NULL)
! 		    s = vim_strnsave(buf + tolist, len);
  		else
  		{
! 		    s = alloc((unsigned)(prevlen + len + 1));
! 		    if (s != NULL)
  		    {
! 			mch_memmove(s, prev, prevlen);
! 			vim_free(prev);
! 			prev = NULL;
! 			mch_memmove(s + prevlen, buf + tolist, len);
  			s[prevlen + len] = NUL;
  		    }
  		}
! 		tolist = filtd + 1;
  
! 		li = listitem_alloc();
! 		if (li == NULL)
  		{
  		    vim_free(s);
  		    break;
  		}
  		li->li_tv.v_type = VAR_STRING;
--- 14359,14419 ----
  	return;
      }
  
      while (cnt < maxline || maxline < 0)
      {
! 	readlen = (int)fread(buf, 1, io_size, fd);
! 
! 	/* This for loop processes what was read, but is also entered at end
! 	 * of file so that either:
! 	 * - an incomplete line gets written
! 	 * - a "binary" file gets an empty line at the end if it ends in a
! 	 *   newline.  */
! 	for (p = buf, start = buf;
! 		p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
! 		++p)
! 	{
! 	    if (*p == '\n' || readlen <= 0)
! 	    {
! 		listitem_T  *li;
! 		char_u	    *s	= NULL;
! 		long_u	    len = p - start;
  
! 		/* Finished a line.  Remove CRs before NL. */
! 		if (readlen > 0 && !binary)
! 		{
! 		    while (len > 0 && start[len - 1] == '\r')
! 			--len;
! 		    /* removal may cross back to the "prev" string */
! 		    if (len == 0)
! 			while (prevlen > 0 && prev[prevlen - 1] == '\r')
! 			    --prevlen;
! 		}
! 		if (prevlen == 0)
! 		    s = vim_strnsave(start, len);
  		else
  		{
! 		    /* Change "prev" buffer to be the right size.  This way
! 		     * the bytes are only copied once, and very long lines are
! 		     * allocated only once.  */
! 		    if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
  		    {
! 			mch_memmove(s + prevlen, start, len);
  			s[prevlen + len] = NUL;
+ 			prev = NULL; /* the list will own the string */
+ 			prevlen = prevsize = 0;
  		    }
  		}
! 		if (s == NULL)
! 		{
! 		    do_outofmem_msg((long_u) prevlen + len + 1);
! 		    failed = TRUE;
! 		    break;
! 		}
  
! 		if ((li = listitem_alloc()) == NULL)
  		{
  		    vim_free(s);
+ 		    failed = TRUE;
  		    break;
  		}
  		li->li_tv.v_type = VAR_STRING;
***************
*** 14412,14485 ****
  		li->li_tv.vval.v_string = s;
  		list_append(rettv->vval.v_list, li);
  
! 		if (++cnt >= maxline && maxline >= 0)
! 		    break;
! 		if (readlen <= 0)
  		    break;
  	    }
! 	    else if (buf[filtd] == NUL)
! 		buf[filtd] = '\n';
  #ifdef FEAT_MBYTE
! 	    else if (buf[filtd] == 0xef
! 		    && enc_utf8
! 		    && filtd + 2 < buflen
! 		    && !binary
! 		    && buf[filtd + 1] == 0xbb
! 		    && buf[filtd + 2] == 0xbf)
! 	    {
! 		/* remove utf-8 byte order mark */
! 		mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
! 		--filtd;
! 		buflen -= 3;
  	    }
  #endif
! 	}
! 	if (readlen <= 0)
! 	    break;
  
! 	if (tolist == 0)
  	{
! 	    if (buflen >= FREAD_SIZE / 2)
  	    {
! 		/* "buf" is full, need to move text to an allocated buffer */
! 		if (prev == NULL)
  		{
! 		    prev = vim_strnsave(buf, buflen);
! 		    prevlen = buflen;
  		}
! 		else
  		{
! 		    s = alloc((unsigned)(prevlen + buflen));
! 		    if (s != NULL)
! 		    {
! 			mch_memmove(s, prev, prevlen);
! 			mch_memmove(s + prevlen, buf, buflen);
! 			vim_free(prev);
! 			prev = s;
! 			prevlen += buflen;
! 		    }
  		}
! 		filtd = 0;
  	    }
  	}
! 	else
! 	{
! 	    mch_memmove(buf, buf + tolist, buflen - tolist);
! 	    filtd -= tolist;
! 	}
!     }
  
      /*
       * For a negative line count use only the lines at the end of the file,
       * free the rest.
       */
!     if (maxline < 0)
  	while (cnt > -maxline)
  	{
  	    listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
  	    --cnt;
  	}
  
      vim_free(prev);
      fclose(fd);
  }
--- 14421,14529 ----
  		li->li_tv.vval.v_string = s;
  		list_append(rettv->vval.v_list, li);
  
! 		start = p + 1; /* step over newline */
! 		if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
  		    break;
  	    }
! 	    else if (*p == NUL)
! 		*p = '\n';
  #ifdef FEAT_MBYTE
! 	    /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF.  Do this
! 	     * when finding the BF and check the previous two bytes. */
! 	    else if (*p == 0xbf && enc_utf8 && !binary)
! 	    {
! 		/* Find the two bytes before the 0xbf.	If p is at buf, or buf
! 		 * + 1, these may be in the "prev" string. */
! 		char_u back1 = p >= buf + 1 ? p[-1]
! 				     : prevlen >= 1 ? prev[prevlen - 1] : NUL;
! 		char_u back2 = p >= buf + 2 ? p[-2]
! 			  : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
! 			  : prevlen >= 2 ? prev[prevlen - 2] : NUL;
! 
! 		if (back2 == 0xef && back1 == 0xbb)
! 		{
! 		    char_u *dest = p - 2;
! 
! 		    /* Usually a BOM is at the beginning of a file, and so at
! 		     * the beginning of a line; then we can just step over it.
! 		     */
! 		    if (start == dest)
! 			start = p + 1;
! 		    else
! 		    {
! 			/* have to shuffle buf to close gap */
! 			int adjust_prevlen = 0;
! 
! 			if (dest < buf)
! 			{
! 			    adjust_prevlen = buf - dest; /* must be 1 or 2 */
! 			    dest = buf;
! 			}
! 			if (readlen > p - buf + 1)
! 			    mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
! 			readlen -= 3 - adjust_prevlen;
! 			prevlen -= adjust_prevlen;
! 			p = dest - 1;
! 		    }
! 		}
  	    }
  #endif
! 	} /* for */
  
! 	if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
! 	    break;
! 	if (start < p)
  	{
! 	    /* There's part of a line in buf, store it in "prev". */
! 	    if (p - start + prevlen >= prevsize)
  	    {
! 		/* need bigger "prev" buffer */
! 		char_u *newprev;
! 
! 		/* A common use case is ordinary text files and "prev" gets a
! 		 * fragment of a line, so the first allocation is made
! 		 * small, to avoid repeatedly 'allocing' large and
! 		 * 'reallocing' small. */
! 		if (prevsize == 0)
! 		    prevsize = p - start;
! 		else
  		{
! 		    long grow50pc = (prevsize * 3) / 2;
! 		    long growmin  = (p - start) * 2 + prevlen;
! 		    prevsize = grow50pc > growmin ? grow50pc : growmin;
  		}
! 		if ((newprev = vim_realloc(prev, prevsize)) == NULL)
  		{
! 		    do_outofmem_msg((long_u)prevsize);
! 		    failed = TRUE;
! 		    break;
  		}
! 		prev = newprev;
  	    }
+ 	    /* Add the line part to end of "prev". */
+ 	    mch_memmove(prev + prevlen, start, p - start);
+ 	    prevlen += p - start;
  	}
!     } /* while */
  
      /*
       * For a negative line count use only the lines at the end of the file,
       * free the rest.
       */
!     if (!failed && maxline < 0)
  	while (cnt > -maxline)
  	{
  	    listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
  	    --cnt;
  	}
  
+     if (failed)
+     {
+ 	list_free(rettv->vval.v_list, TRUE);
+ 	/* readfile doc says an empty list is returned on error */
+ 	rettv->vval.v_list = list_alloc();
+     }
+ 
      vim_free(prev);
      fclose(fd);
  }
*** ../vim-7.3.426/src/version.c	2012-02-04 23:34:57.000000000 +0100
--- src/version.c	2012-02-05 00:38:34.000000000 +0100
***************
*** 716,717 ****
--- 716,719 ----
  {   /* Add new patch number below this line */
+ /**/
+     427,
  /**/

-- 
One difference between a man and a machine is that a machine is quiet
when well oiled.

 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///