Hi,

If it's known that inside an AV* array all the elements are IVs, or all the elements are UVs , can I measure the sizeof(IV) and instead of using av_fetch use normal C addressing of arrays like *(array+index) ?

If this is not possible, is there a way to just not use av_fetch, but instead use something else, for example suppose I have a SV* , I would like to make what I want of it, that is, I want to completely ignore Perl's data structure and just write in it as if it were an usual C array.

Will "corrupting" a Perl data structure in this way lead to any problems? (I wasn't able to think of any, the most important would be the refcnt , but we won't tamper with that).

Normally I wouldn't do this because I know some people thought out Perl's data structures and they should be used with the API they have written but I want to write something that will be blazing fast so I need to break some rules here, such as portability(not interested in writing portable code, just extremely fast one).

I'm sure I'm not the first one to think of this as there are so many XS modules on CPAN and there's a big chance one of them is doing this(only problem is I don't know which one).

Another idea would be to use av_fetch to first get all the pointers of the elements of an array(a AV*)and put them in a SV* array[](maybe not even this, maybe just the _SV_HEAD_UNION members of the SV structs, as I understand by reading this, that they are actually the ones containing the data) and use that array because addressing it would be faster than using av_fetch each time. This of course, all depends on many Perl implementation details.

I'm seeking general XS tips for speeding up code. Also, this is not a premature decision/thought as my code is already written, now I need to optimize it.

Thanks


In reply to Need for (XS) speed by spx2

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.