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!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.