in reply to Re^2: XS and overload
in thread XS and overload

if your data structure acts like an array, then you should be using the tie-array interface instead of overloading. The @{} overloading method is available but it is quite inefficient for most practical purposes.

Well, actually, you can combine both overloading and tying. For instance, if you were developing a vector class:

my $v = Vector->new(1..10); my $u = Vector->new(11..20); my $w = $u - $v; # overload my $e5 = $w->[5]; # tie
There is nothing special about tie or overloading methods implemented in XS, you just have to give then the right names in the first case or register them as overload methods with the overload module (well, you will probably need to init the XS part from a BEGIN block, so that the methods are available when you do the use overload ...).

About speed, not too long ago, I did some benchmarks for my Tie::Array::Packed module that implements a tie interface in pure XS for packed arrays, and found that it was around 15 times slower that perl native arrays (a pure perl implementation as provided by Tie::Array::PackedC is around 60 times slower than native arrays). The extra overhead is caused by the method dispatching and because on every call some SVs are created, copied and deallocated which are relatively expensive operations.

On the other hand, comparing a tied array access or an overload operation to a method call, the performance should be quite similar. The only difference is that the tie/overload operation has to perform a magic lookup, something that only requires a few C operations.

Replies are listed 'Best First'.
Re^4: XS and overload
by BrowserUk (Patriarch) on Apr 22, 2006 at 22:05 UTC

    Thanks a lot. Tie::Array::Packed.xs will provide a great reference for what I am doing.

    I agree that the tie interface is the right way for all the array style operations, but there are a couple of extensions that go beyond what an array does, that would benefit from being overloaded.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.