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.


In reply to Re^3: XS and overload by salva
in thread XS and overload by BrowserUk

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.