This question is half Perl, half perl, and half C. That's too many halves, but I was never very good at math.

I have some accessors that, after profiling, seem to be good candidates for some efficiency gains. Even shaving a few fractions of a second off could make a big difference, as they tend to get called a lot.

Here's the Perl implementation of the one I'm starting with:

sub world_location { my ($self) = @_; return ($self->{x}, $self->{y}); }

Which works just fine according to my test scripts. Now in xs:

#include "EXTERN.h" #include "perl.h" #include "XSUB.h" AV* world_location( SV* self ) { HV *hash_self = (HV*) SvRV( self ); SV **x = hv_fetch( hash_self, "x", 1, FALSE ); SV **y = hv_fetch( hash_self, "y", 1, FALSE ); AV *to_return = newAV(); av_push( to_return, *x ); av_push( to_return, *y ); return to_return; } MODULE = My::Games::Azure::Unit PACKAGE = My::Games::Azure::Unit PROTOTYPES: DISABLE AV* world_location(self) SV* self

However, my test script gives me the following failures:

NOK 1# Failed test (t/025_unit.t at line 128) # got: 140725396 # expected: 84 t/025_unit...........NOK 2# Failed test (t/025_unit.t at line 129) + # got: undef # expected: 84

(The first test is the X coord, and the second is the Y coord.)

The values returned seem to be identical in each run. I haven't done much xs and my C is rusty, so I'm not sure what I'm doing wrong here.

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.


In reply to Accessors in xs by hardburn

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.