You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Drew Taylor <dt...@vialogix.com> on 2000/07/10 19:33:37 UTC

Is variable initialization necessary?

Here's another topic I've had on my mind lately.

I currently try to initialize all vaiables in the definition (my $var =
();) I've read where several I respect, Doug being one :-), initialize
variables. Then I've read posts of people comparing the op count from
initializtion vs. non-initialization where initialization creates a
sizable more percentage of calls.

So my question is: Is variable initialization necessary? Is being a
lexical enough? To date, I've played it safe. But if I don't have to...
then I won't.

Does anyone have good evidence either way?

-- 
Drew Taylor
Vialogix Communications, Inc.
501 N. College Street
Charlotte, NC 28202
704 370 0550
http://www.vialogix.com/

Re: Is variable initialization necessary?

Posted by Drew Taylor <dt...@vialogix.com>.
Rodney Broom wrote:
>  
> A couple of points:
> 1. I couldn't tell you for sure about performance, but I wouldn't worry too
> much. If you are working on a perfessional system, then I'd bet that you have
> enough hardware to handle any slight performance hit that might be seen.
I know. But I'll soon be to the point where I can start optimizing code.
The hardware can take it, but why not eek out a few more RPS? :-)

> 2. Lear to use strict (see perldoc strict). Once you have this down, you'll have
> everything you need on scoping. It's more difficult to program under strict, but
> much safer.
>
> 3. Further, there are some issues on scoping vars (or rather failing to) when
> running under mod_perl. My experience sais to always lexically scope vars,
> preferably with my() where applicable.
Been there, done that. :-) I no longer consider myself a novice. I was
merely curious as to the general concensus. It sounds like all the major
CPAN modules initialize to avoid warnings under -w, so I'll keep on my
previous course. All in all, it was an interesting discussion - it
mostly satisfied my curiosity.

-- 
Drew Taylor
Vialogix Communications, Inc.
501 N. College Street
Charlotte, NC 28202
704 370 0550
http://www.vialogix.com/

Re: Is variable initialization necessary?

Posted by Rodney Broom <rb...@home.com>.
"Drew Taylor wrote:

> So my question is: Is variable initialization necessary? Is being a
> lexical enough? To date, I've played it safe. But if I don't have to...
> then I won't.

Hi Drew,

A couple of points:
1. I couldn't tell you for sure about performance, but I wouldn't worry too
much. If you are working on a perfessional system, then I'd bet that you have
enough hardware to handle any slight performance hit that might be seen.

2. Lear to use strict (see perldoc strict). Once you have this down, you'll have
everything you need on scoping. It's more difficult to program under strict, but
much safer.

3. Further, there are some issues on scoping vars (or rather failing to) when
running under mod_perl. My experience sais to always lexically scope vars,
preferably with my() where applicable.

----
Rodney Broom




Re: Is variable initialization necessary?

Posted by Drew Taylor <dt...@vialogix.com>.
Barrie,

Thanks for the interesting benchmarks. In my case, I am only
initializing to '' or 0 to avoid warnings (I have PerlWarn On). What was
most interesting is the hit when you initialize an empty array. I'll
probably just continue on my current course, but the numbers are very
intriguing. 

Thanks again.

Drew

Barrie Slaymaker wrote:
> 
> Drew Taylor wrote:
> >
> > Does anyone have good evidence either way?
> 
> I don't see how C<sub { my $foo ; ...> could ever fail to undef $foo, modulo
> bugs in perl.  A hell of a lot of code wouldn't work, then.
> 
> My practice is to never init lexicals to undef/(), and only to '' or 0 if
> they might be used before being set to a defined value.  Can't see any
> reason to = () in perl, where it's set to undef.  C's a different question,
> where you often get randomish values instead of perl's nice clean undef.
> 
> Side note: stay away from the dreaded C<my $f = blah if blurgh> idiom
> unless you really understand it and really need it.
> 
> Below are some useless benchmarks.  The first focuses on the performance of
> C<my $foo=()>, the second is an attempt to put it in perspective.  Both
> on perl 5.6.0.  It's a lot slower to initialize (66%).  My take is that
> it's a minor hit compared to the other things you're usually doing, unless
> you do it in a tight inner loop.

-- 
Drew Taylor
Vialogix Communications, Inc.
501 N. College Street
Charlotte, NC 28202
704 370 0550
http://www.vialogix.com/

Re: Is variable initialization necessary?

Posted by Barrie Slaymaker <ba...@slaysys.com>.
Drew Taylor wrote:
> 
> Does anyone have good evidence either way?

I don't see how C<sub { my $foo ; ...> could ever fail to undef $foo, modulo
bugs in perl.  A hell of a lot of code wouldn't work, then.

My practice is to never init lexicals to undef/(), and only to '' or 0 if
they might be used before being set to a defined value.  Can't see any
reason to = () in perl, where it's set to undef.  C's a different question,
where you often get randomish values instead of perl's nice clean undef.

Side note: stay away from the dreaded C<my $f = blah if blurgh> idiom
unless you really understand it and really need it.

Below are some useless benchmarks.  The first focuses on the performance of
C<my $foo=()>, the second is an attempt to put it in perspective.  Both
on perl 5.6.0.  It's a lot slower to initialize (66%).  My take is that
it's a minor hit compared to the other things you're usually doing, unless
you do it in a tight inner loop.

- Barrie

                     Rate  my $foo = 7 my $foo = () my $foo = undef      my $foo
my $foo = 7     1724138/s           --         -14%            -14%         -71%
my $foo = ()    2000000/s          16%           --              0%         -66%
my $foo = undef 2000000/s          16%           0%              --         -66%
my $foo         5882353/s         241%         194%            194%           --

##########################
#!/usr/local/bin/perl -w

use Benchmark qw( cmpthese timethese ) ;

cmpthese(
   timethese(
      1000000,
      {
         'my $foo = 7'         => sub { my $foo = 7        },
         'my $foo = undef'     => sub { my $foo = undef    },
         'my $foo = ()'        => sub { my $foo = ()       },
         'my $foo'             => sub { my $foo            },
      },
      'none'
   )
) ;

open ME, $0 ; print "\n##########################\n", <ME> ;

-----------------------------------------------------------------------
                     Rate my $foo = undef my $foo = ()  my $foo = 7      my $foo
my $foo = undef  934579/s              --          -2%          -7%         -36%
my $foo = ()     952381/s              2%           --          -6%         -34%
my $foo = 7     1010101/s              8%           6%           --         -30%
my $foo         1449275/s             55%          52%          43%           --

##########################
#!/usr/local/bin/perl -w

use Benchmark qw( cmpthese timethese ) ;

my $bar ;

cmpthese(
   timethese(
      1000000,
      {
         'my $foo = 7'         => sub { my $foo = 7       ; $bar = 12 },
         'my $foo = undef'     => sub { my $foo = undef   ; $bar = 12 },
         'my $foo = ()'        => sub { my $foo = ()      ; $bar = 12 },
         'my $foo'             => sub { my $foo           ; $bar = 12 },
      },
      'none'
   )
) ;

open ME, $0 ; print "\n##########################\n", <ME> ;

[OT] Re: Is variable initialization necessary?

Posted by Drew Taylor <dt...@vialogix.com>.
Vivek Khera wrote:
> 
> >>>>> "DT" == Drew Taylor <dt...@vialogix.com> writes:
> 
> DT> My underlying concern is that each time the code is run, I get "clean"
> DT> variables. Every variable is a lexical. If that is enough to guarantee
> DT> emptiness on each run, then initialization is unnecessary (and in fact a
> DT> performance decrease).
> 
> If you initialize a variable at its declaration, will that
> initialization happen every time thru or will it be done similarly to
> a BEGIN block?  I think that's your real question.
I believe what you said is my question. Can I safely do this?

my @list;
foreach qw(field field2 field3) {
	push @list, $_;
}

I'm 99.99% sure snippet this will give me a clean @list each time it is
run. 

My next question is: do most of the modules submitted to CPAN initialize
variables? I read Chris' response, and I'm inclined to keep initializing
variables. One day I hope to submit CPAN modules, so is this a good
habit to get into? Sorry for getting so off-topic. 


-- 
Drew Taylor
Vialogix Communications, Inc.
501 N. College Street
Charlotte, NC 28202
704 370 0550
http://www.vialogix.com/

Re: Is variable initialization necessary?

Posted by Vivek Khera <kh...@kciLink.com>.
>>>>> "DT" == Drew Taylor <dt...@vialogix.com> writes:

DT> My underlying concern is that each time the code is run, I get "clean"
DT> variables. Every variable is a lexical. If that is enough to guarantee
DT> emptiness on each run, then initialization is unnecessary (and in fact a
DT> performance decrease).

If you initialize a variable at its declaration, will that
initialization happen every time thru or will it be done similarly to
a BEGIN block?  I think that's your real question.

Re: Is variable initialization necessary?

Posted by Matt Sergeant <ma...@sergeant.org>.
On Mon, 10 Jul 2000, Drew Taylor wrote:

> My underlying concern is that each time the code is run, I get "clean"
> variables. Every variable is a lexical. If that is enough to guarantee
> emptiness on each run, then initialization is unnecessary (and in fact a
> performance decrease).

Provided your lexicals aren't closures, then yes, they get initialized on
each hit. CPAN modules only tend to initialise variables that cause that
damnable "use of uninitialized variable", well, that's what mine do,
anyway. Or alternatively, create your variables where you initially set
their value, rather than the old C method of a list of variable
declarations (which is only really a win for typed variables).

-- 
<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: Is variable initialization necessary?

Posted by Drew Taylor <dt...@vialogix.com>.
Vivek Khera wrote:
> 
> >>>>> "MS" == Matt Sergeant <ma...@sergeant.org> writes:
> 
> MS> I doubt you'll find evidence, just some hand waving. If you know what
> MS> you're doing, use lexicals and don't worry about initialization. If you
> MS> don't know what you're doing, initialize.
> 
> Not to imply that initializing means you don't know what you're doing,
> of course. ;-)
But of course. ;-)

My underlying concern is that each time the code is run, I get "clean"
variables. Every variable is a lexical. If that is enough to guarantee
emptiness on each run, then initialization is unnecessary (and in fact a
performance decrease).

-- 
Drew Taylor
Vialogix Communications, Inc.
501 N. College Street
Charlotte, NC 28202
704 370 0550
http://www.vialogix.com/

Re: Is variable initialization necessary?

Posted by Vivek Khera <kh...@kciLink.com>.
>>>>> "MS" == Matt Sergeant <ma...@sergeant.org> writes:

MS> I doubt you'll find evidence, just some hand waving. If you know what
MS> you're doing, use lexicals and don't worry about initialization. If you
MS> don't know what you're doing, initialize.

Not to imply that initializing means you don't know what you're doing,
of course. ;-)

-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Vivek Khera, Ph.D.                Khera Communications, Inc.
Internet: khera@kciLink.com       Rockville, MD       +1-301-545-6996
GPG & MIME spoken here            http://www.khera.org/~vivek/

Re: Is variable initialization necessary?

Posted by Matt Sergeant <ma...@sergeant.org>.
On Mon, 10 Jul 2000, Chris Thorman wrote:

> There's another factor, here, unfortunately: -w warnings.
> 
> I personally never use -w, unfortunately, because it warns about not
> only variables being used before they are first set (usually a good
> thing to warn about) but also the use of variables whose value is
> undef, which is a perfectly legitimate and time-honored tradition in
> perl.

A lot of people create a $SIG{__WARN__} block to catch those warnings. In
fact that's pretty much the only use of $SIG{__WARN__} I've ever seen ;-)

-- 
<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: Is variable initialization necessary?

Posted by Chris Thorman <ct...@innx.com>.
There's another factor, here, unfortunately: -w warnings.

I personally never use -w, unfortunately, because it warns about not only variables being used before they are first set (usually a good thing to warn about) but also the use of variables whose value is undef, which is a perfectly legitimate and time-honored tradition in perl.

However, anyone writing for modules that must compile and work well under -w, as most public modules should, has to worry about this problem since many people really want everything they write or use to run clean under -w.

Certainly this would apply to Doug's code and most public high-profile modules on CPAN, so that's the biggest reason why you see people doing "unnecessary" initializations.

We all hope, I think, that the new, more flexible approach to warnings in 5.6 will mitigate this situation in the coming years.

-chris

At 7:19 PM +0100 7/10/00, Matt Sergeant wrote:
>On Mon, 10 Jul 2000, Drew Taylor wrote:
>
>> Here's another topic I've had on my mind lately.
>> 
>> I currently try to initialize all vaiables in the definition (my $var =
>> ();) I've read where several I respect, Doug being one :-), initialize
>> variables. Then I've read posts of people comparing the op count from
>> initializtion vs. non-initialization where initialization creates a
>> sizable more percentage of calls.
>> 
>> So my question is: Is variable initialization necessary? Is being a
>> lexical enough? To date, I've played it safe. But if I don't have to...
>> then I won't.
>> 
>> Does anyone have good evidence either way?
>
>I doubt you'll find evidence, just some hand waving. If you know what
>you're doing, use lexicals and don't worry about initialization. If you
>don't know what you're doing, initialize.
>
>-- 
><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


------------------------------------------------------------------------
Chris Thorman                                       (413) 473-0853 e-fax
------------------------------------------------------------------------

Re: Is variable initialization necessary?

Posted by Matt Sergeant <ma...@sergeant.org>.
On Mon, 10 Jul 2000, Drew Taylor wrote:

> Here's another topic I've had on my mind lately.
> 
> I currently try to initialize all vaiables in the definition (my $var =
> ();) I've read where several I respect, Doug being one :-), initialize
> variables. Then I've read posts of people comparing the op count from
> initializtion vs. non-initialization where initialization creates a
> sizable more percentage of calls.
> 
> So my question is: Is variable initialization necessary? Is being a
> lexical enough? To date, I've played it safe. But if I don't have to...
> then I won't.
> 
> Does anyone have good evidence either way?

I doubt you'll find evidence, just some hand waving. If you know what
you're doing, use lexicals and don't worry about initialization. If you
don't know what you're doing, initialize.

-- 
<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