Re: $r-print and references: Was RE: Slightly OT - Content-length

2000-06-19 Thread Matt Sergeant

On Mon, 19 Jun 2000, John Hughes wrote:

 (Hint - Perl passes all values by reference.

Are you sure thats the case with XS code? I don't personally know XS very
well, but there are some wierd things about it, and this might be one of
them. I know for certain that XML::Parser has a lot of slow-down because
of the Perl - XS interface, and passing strings across it (although IIRC
Ilya did some work on that for perl 5.6).

-- 
Matt/

Fastnet Software Ltd. High Performance Web Specialists
Providing mod_perl, XML, Sybase and Oracle solutions
Email for training and consultancy availability.
http://sergeant.org | AxKit: http://axkit.org




RE: $r-print and references: Was RE: Slightly OT - Content-length

2000-06-19 Thread John Hughes

De : Shane Nay [mailto:[EMAIL PROTECTED]]
 To comment on what John originally said..., arg, here we go.  The
 difference between print \$somevariable, and print $somevariable can
 be very significant.

Nope.  Not "very".

 Everything is passed internally as a "reference", but that
 doesn't me it's done in the same way.  When you say "print $somevar",
 and "print \$somevar" two VERY different things happen. case 1:
 print $somevar  (I'm assuming a type of string)
 SV* somesv=(SV*)malloc(sizeof(SV));
 somesv-sv_any=(void*)malloc(sizeof(char*)*strlen(ourstring));
 //Notice, have to set aside memory
 strcpy((char*)somesv-sv_any,ourstring); //Notice we have to copy our data
 --Call the "print function" and pass it a reference to "somesv"

I'm sorry, but I think you are simply wrong here.  If I call a perl
function or an XS routine with a variable as an argument it is passed by
reference.  There is *no* copy.

A simple proof:

sub hack {
$_[0] = 'a new string';
}

$a = 'an old string';
hack $a;
print $a;

This part of your pseudo code:

 SV* somesv=(SV*)malloc(sizeof(SV));
 somesv-sv_any=(void*)malloc(sizeof(char*)*strlen(ourstring));
 strcpy((char*)somesv-sv_any,ourstring); //Notice we have to copy our data

is *not* part of the call.  It's the code that puts a value in "somesv",
i.e.

$somesv = "ourstring".

It is present in both the ref and the nonref cases.

Passing a refrence to $r-print is *slower*(by an infinitessimal amount).

The point of allowing it is to get around the problems with the normal
Perl coding style:

sub slow {
my $r = shift;
my $arg = shift;   # A copy here!
$r-print ($arg);
}

But note the copy is not in the argument passing, it's in the Perl
code.

sub fast_but_ugly {
$_[0]-print ($_[1]);
}

--
John Hughes [EMAIL PROTECTED],
CalvaEDI SA.Tel: +33-1-4313-3131
66 rue du Moulin de la Pointe,  Fax: +33-1-4313-3139
75013 PARIS.