<?xml version="1.0" encoding="windows-1252"?>
<node id="751048" title="Re: Inline::C's AoA is much bigger than Perl's" created="2009-03-16 18:24:31" updated="2009-03-16 18:24:31">
<type id="11">
note</type>
<author id="439528">
tlm</author>
<data>
<field name="doctext">
&lt;b&gt;Update:&lt;/b&gt;Actually the problems with the code below, I just discovered, are far more serious than a mere memory leak.  It blows up if I try to accesses the elements in the table...  I guess it's time for me to call it a day!

&lt;strike&gt;
&lt;p&gt;OK, I'm replying to myself here...&lt;/p&gt;

&lt;p&gt;After I posted the original query, I tried a different tack.  I streamlined the C function make_aoa_c, as follows:

&lt;code&gt;
SV *make_aoa_c( int n_rows, int n_cols ) {
  int i, j;
  char *foo = "foo";
  AV *table = newAV();
  AV *row;

  for ( i = 0; i &lt; n_rows; ++i ) {
    row = ( AV * ) sv_2mortal( ( SV * ) newAV() );
    for ( j = 0; j &lt; n_cols; ++j ) {
      av_push( row, newSVpv( foo, 0 ) );
    }

    av_push( table, sv_2mortal( newRV( ( SV * ) row ) ) );
  }

  return newRV( ( SV * ) table );
}
&lt;/code&gt;

This took care of the size problem for the most part, and greatly improved the speed (though it's still slower than Perl).&lt;/p&gt;

&lt;p&gt;Unfortunately, the code has sprung a small memory leak that I can't identify!  Changing the number of repetitions to 10, now the output for the C case looks like this:

&lt;code&gt;
% perl test_aoa.pl 1
1: 78844 (280041 us)
2: 78884 (247865 us)
3: 78892 (237725 us)
4: 78900 (245755 us)
5: 78908 (235251 us)
6: 78916 (235712 us)
7: 78924 (246926 us)
8: 78932 (237128 us)
9: 78940 (237369 us)
10: 78948 (238528 us)
&lt;/code&gt;
With every iteration, the memory grows by at least 8kb.&lt;/p&gt;

&lt;p&gt;I have tried adding sv_2mortal around various items in the code (e.g. &lt;c&gt;AV *table=(AV *)sv_2mortal((SV *)newAV())&lt;/c&gt;), but I get errors like:

&lt;code&gt;
Attempt to free unreferenced scalar: SV 0x5b2c620, Perl interpreter: 0x603010 at test_aoa.pl line 22.
&lt;/code&gt;

If anyone can spot the memory leak here, I'd much appreciate it!&lt;/p&gt;
&lt;/strike&gt;


&lt;div class="pmsig"&gt;&lt;div class="pmsig-439528"&gt;
&lt;p&gt;&lt;small&gt;the lowliest monk&lt;/small&gt;&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
751041</field>
<field name="parent_node">
751041</field>
</data>
</node>
