From f12005599f648e675af22dfef1047191e260bc48 Mon Sep 17 00:00:00 2001 From: Wolfram Humann Date: Fri, 13 Aug 2010 17:20:26 -0700 Subject: make string-append on win32 100 times faster When a string grows (e.g. gets appended to), perl calls sv_grow. When sv_grow decides that the memory currently allocated to the string is insufficient, it calls saferealloc. Depending on whether or not perl was compiled with 'usemymalloc' this calls realloc in either perls internal version or on the operating system. Perl requests from realloc just the amount of memory required for the current operation. With 'usemymalloc' this is not a problem because it rounds up memory allocation to a certain geometric progression anyway. When the operating system's realloc is called, this may or may not lead to desaster, depending on how it's implemented. On win32 it does lead to desaster: when I loop 1000 times and each time append 1000 chars to an initial string size of 10 million, the memory grows from 10.000e6 to 10.001e6 to 10.002e6 and so on 1000 times till it ends at 11.000e6. --- sv.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'sv.c') diff --git a/sv.c b/sv.c index a2f9867214..1f66e5b393 100644 --- a/sv.c +++ b/sv.c @@ -1536,6 +1536,10 @@ Perl_sv_grow(pTHX_ register SV *const sv, register STRLEN newlen) s = SvPVX_mutable(sv); if (newlen > SvLEN(sv)) { /* need more room? */ + STRLEN minlen = SvCUR(sv); + minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + 10; + if (newlen < minlen) + newlen = minlen; #ifndef Perl_safesysmalloc_size newlen = PERL_STRLEN_ROUNDUP(newlen); #endif -- cgit v1.2.1