Update: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!
OK, I'm replying to myself here...
After I posted the original query, I tried a different tack. I streamlined the C function make_aoa_c, as follows:
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 < n_rows; ++i ) {
row = ( AV * ) sv_2mortal( ( SV * ) newAV() );
for ( j = 0; j < n_cols; ++j ) {
av_push( row, newSVpv( foo, 0 ) );
}
av_push( table, sv_2mortal( newRV( ( SV * ) row ) ) );
}
return newRV( ( SV * ) table );
}
This took care of the size problem for the most part, and greatly improved the speed (though it's still slower than Perl).
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:
% 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)
With every iteration, the memory grows by at least 8kb.
I have tried adding sv_2mortal around various items in the code (e.g. AV *table=(AV *)sv_2mortal((SV *)newAV())), but I get errors like:
Attempt to free unreferenced scalar: SV 0x5b2c620, Perl interpreter: 0
+x603010 at test_aoa.pl line 22.
If anyone can spot the memory leak here, I'd much appreciate it!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.