I recently received a poorly documented, poorly written DLL that I'm attempting to interface with perl. (Activestate, win32, 5.8.7). Obviously, there's a problem, the first one being that I don't understand XS very well yet... but I'm working on that. The gist of my question is if there's a good way to avoid an extra copy because there's a LOT of data. If there's no way to do so, I'll just use the straight-forward way.

Basically, one of the C routines will be looped over, and will easily return several thousand different 83-byte char buffers (IE, I pass in an 83-byte buffer, and it pushes data into it... several thousand times)

If I understand newSVpv correctly, that copies the message buffer into the new SV, which is an extra copy that I shouldn't need. What I'd like to do is something like
AV* GetTable() PREINIT: AV *array; int max_rows, max_cols, row, col; char *message; SV *message_sv; CODE: max_rows = 10; max_cols = 5; array = (AV*) sv_2mortal((SV*) newAV()); for( row = 0; row < max_rows; row++) { for( col = 0; col < max_cols; col++) { message_sv = newSV(83); message = SvPV_nolen(message_sv); ExpensiveGetString(row, col, message, 83); av_push(array, message_sv); } } RETVAL = array; OUTPUT: RETVAL
Where ExpensiveGetString is approximately equal to
strcpy(message, "12345678901234567890123456789012345678901234567890123 +456789012345678901234567890123456789012345678901234567890123456789012 +\0");
which is exactly what I'm using to test it. I get an array of undefs back, which tells me that I'm missing something.

Why doesn't this work? Is there a better way to avoid copying the data twice (some of the datasets are in the 30k rows and 70 columns range)?

In reply to XS efficient copy by TheDauthi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.