in reply to Re^4: XS/Inline::C concat *any* two SVs.
in thread XS/Inline::C concat *any* two SVs.

I want, and need to accumulate the appended data in the original SV.

That's easy, but in this case you must work not with SV but refernce to SV, and dereference it properly.

Let us start with perl stub of what you're trying to achieve

sub test { my $ref = shift; $$ref .= shift; # concatenate, but do efficiently at C level, workin +g with preallocated space }

If this is not what you need, then try expressing in terms of Perl sub your ideas, and we'll translate those to XS... or C.

When you enter a sub, parameters to it are mostly copied (well, sometimes they are aliased but relying on this knowledge will not help you understanding, so let us assume they are copied), so you will not gain speed unless you work with reference.

Regarding your OP code, when you make an illusion of correctly working code, but with mystery "Why do I need to increment the ref count?" this only means you got a SV leak. I can explain it, if needed.

PS sorry my frustrated reply, but once you'll explain your subroutine better I will reply better :)

Replies are listed 'Best First'.
Re^6: XS/Inline::C concat *any* two SVs.
by BrowserUk (Patriarch) on May 29, 2006 at 04:29 UTC
    well, sometimes they are aliased but relying on this knowledge will not help you understanding,

    Actually, that is exactly the type of understanding I hope to aquire. The very purpose of dropping into XS to code is to achieve better performance. If you throw away half that potential by unnecessarially replicating data, there is no point in going there in the first place.

    The purpose of the exercise is to learn how to correctly use and benefit from using Perl's scalar classes from within XS code.

    Dealing with scalars that might be NVs, IVs, UVs and readonly is only the first part of the exercise. There are also RVs, tied scalars, blessed scalars etc. When I posted, I was hoping for insights into dealing with these also. I never expected to get hung up on dealing with readonly inputs.


    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.
      IMO dealing with NVs, IVs, UVs should be left to perl, its algorithms were tuned by years of everyday use and tons of bugreports. So instead of inserting your own "if numerix then stringify" better just do nothing and perl will DWIM. (let days of its DWIMery and TIMTOWTDI will be countless :)

      I applause to your intentions on deal efficiently with SV. This is very doable. I will gladly share my knowledge, in addition to what I already shared (try pure-XS w/o Inline::C and you need reference to SV to be able to modify SV passed to sub).

        IMO dealing with NVs, IVs, UVs should be left to perl, its algorithms were tuned by years of everyday use and tons of bugreports. So instead of inserting your own "if numerix then stringify" better just do nothing and perl will DWIM.

        I didn't start this for the fun of doing it. Believe me, nothing would please me more if that were the case, but it simply isn't. If I just use the API provided for concatenating 2 SVs, the result is no output--except a warning:

        #! perl -slw use strict; use Inline C => << '__C__', NAME => 'test', CLEAN_AFTER_BUILD => 0; #include <stdio.h> SV* test( SV *a, SV *b ) { sv_catsv( a, b ); return a; } __C__ for ( 1 .. 1e7 ) { print test( 'bill', 'fred' ); my( $p, $q ) = ( 'fred' ); print test( $q, $p ); $q = 'bill'; print test( $q, $p ); $q = 1; print test( $q, $p ); $p = 1; print test( $q, $p ); } __END__ c:\test>test Modification of a read-only value attempted at c:\test\test.pl line 25 +. c:\test>

        Try it. Nothing! No output at all from *any* of the tests. DWIM? Right.


        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.