http://qs1969.pair.com?node_id=502523

hardburn has asked for the wisdom of the Perl Monks concerning the following question:

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.