On 26 March 2013 08:32, H.Merijn Brand <h.m.brand@xs4all.nl> wrote: > On Mon, 25 Mar 2013 20:47:39 -0400, Eric Brine <ikegami@adaelis.com> > wrote: > >> On Sun, Mar 24, 2013 at 4:00 PM, bulk88 <bulk88@hotmail.com> wrote: >> >> > Konovalov, Vadim (Vadim)** CTR ** wrote: >> > >> >> It was clearly and unambiguously a patch. It wasn't in diff format, but it >> >>> was clearly indicating that to make your code more portable, you could >> >>> change the line: >> >>> >> >>> HE *hes [count]; >> >>> to >> >>> #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__STDC__) >> >>> HE **hes = Perl_malloc (count * sizeof (HE)); >> >>> #else >> >>> HE *hes [count]; >> >>> #endif >> >>> >> >> >> >> 1) >> >> an overlook - it should be HE **hes = Perl_malloc (count * sizeof >> >> (HE*)); >> >> >> >> it appears that HE is larger than HE* and allocating more memory, hence >> >> misbehaviour is not noticed, but anyway, this isn't correct >> >> >> >> 2) when that chunk of memory freed? TIA :) >> >> >> > >> > alloca, it can never leak. A non-const length auto C array calls alloca >> > anyway on C99 under the hood. >> >> I think he's asking about the newly introduced Perl_malloc > > I tested this yesterday with this code: > --8<--- > my $n = 100; > foreach my $f (qw( test1.json test2.json )) { > say "Testing $f"; > my $jsn = do { open my $fh, "<", $f; local $/; <$fh> }; > printf "Length: %9d\n", length $jsn; > my $x = decode_json ($jsn); > printf "Size D: %9d\n", total_size ($x); > printf "Size E: %9d\n", length encode_json ([$x]); > > my $td = 0; > for (1..$n) { > my $t0 = [ gettimeofday ]; > $x = decode_json ($jsn); > $td += tv_interval ($t0); > } > printf "%s: decode %10.6f s/run\n", $f, $td / $n; > > my $te = 0; > for (1..$n) { > my $t0 = [ gettimeofday ]; > my $j = encode_json ([$x]); > $te += tv_interval ($t0); > } > > printf "%s: encode %10.6f s/run\n", $f, $te / $n; > } > -->8--- > > on two relative big json files test1.json is unformatted (no newlines > and whitespace) test2.json is formatted (prettied) > > -rw-rw-rw- 1 merijn users 716642 Mar 25 18:40 test1.json > -rw-rw-rw- 1 merijn users 14776790 Mar 25 18:40 test2.json > > Testing test1.json > Length: 716642 > Size D: 2240594 > Size E: 716589 > test1.json: decode 0.010089 s/run > test1.json: encode 0.003995 s/run > Testing test2.json > Length: 14776790 > Size D: 28672598 > Size E: 8198965 > test2.json: decode 0.157494 s/run > test2.json: encode 0.042437 s/run > > Then changed the code to (note the (HE *) and the dropped _ for > Linux/gcc. Used alloca, not Perl_malloc. I know I should look at > *en*coding timings only. > > #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__STDC__) > HE **hes = alloca (count * sizeof (HE *)); > #else > HE *hes [count]; // if your compiler dies here, you need to enable C99 mode > #endif > > and got > > Testing test1.json > Length: 716642 > Size D: 2240594 > Size E: 716589 > test1.json: decode 0.010471 s/run > test1.json: encode 0.003938 s/run > Testing test2.json > Length: 14776790 > Size D: 28672598 > Size E: 8198965 > test2.json: decode 0.156661 s/run > test2.json: encode 0.041112 s/run > > Then with Perl_malloc > > #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__STDC__) > HE **hes = Perl_malloc (count * sizeof (HE *)); > #else > HE *hes [count]; // if your compiler dies here, you need to enable C99 mode > #endif > > which resulted in > > Testing test1.json > Length: 716642 > Size D: 2240594 > Size E: 716589 > test1.json: decode 0.009939 s/run > test1.json: encode 0.003932 s/run > Testing test2.json > Length: 14776790 > Size D: 28672598 > Size E: 8198965 > test2.json: decode 0.157021 s/run > test2.json: encode 0.041158 s/run > > which is still faster than the original code. > > gcc (SUSE Linux) 4.7.2 20130108 [gcc-4_7-branch revision 195012] > This is perl 5, version 16, subversion 3 (v5.16.3) built for i686-linux-64int > Linux 3.7.10-1.1-desktop [openSUSE 12.3 (Dartmouth)] i386 Core(TM) i7-2620M CPU @ 2.70GHz/800(4) i686 8042 Mb IMO the differences there are in the noise. Yves -- perl -Mre=debug -e "/just|another|perl|hacker/"Thread Previous | Thread Next