Yves said that the XS version of Data::Dumper was inefficient because
Nicholas Clark [Sat, 7 Oct 2006 23:42:56 +0000 (23:42 +0000)]
it keeps triggering realloc() due to sv_cat(). Here's a rather brute
force approach to pre-stretching the buffer - if there are less than
40 bytes free, grow it by 50%.

Surprisingly effective for my test program
./perl -Ilib -MData::Dumper -MStorable=retrieve -we \
    'print Dumper(retrieve(shift))' ~/.cpan/Metadata >/dev/null

Before
real    2m42.921s
user    1m43.321s
sys     0m55.611s

After
real    0m5.205s
user    0m4.885s
sys     0m0.255s

Same 25M of output, byte for byte. :-)

p4raw-id: //depot/perl@28963

ext/Data/Dumper/Dumper.xs

index 36383dc..dbdc2d6 100644 (file)
@@ -274,6 +274,9 @@ DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
     if (!val)
        return 0;
 
+    if (SvTYPE(retval) >= SVt_PV && (SvLEN(retval) - SvCUR(retval)) < 40) {
+       sv_grow(retval, SvCUR(retval) * 1.5);
+    }
     realtype = SvTYPE(val);
 
     if (SvGMAGICAL(val))