modperl_perl_style_vars

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: mod_perl list <modperl@apache.org>
From: Stas Bekman <stas@stason.org>
Subject: vars vs fqdn vs our benchmark
Date: Tue, 26 Dec 2000 22:32:42 +0100 (CET)

here is another one

=head2 Global vs. Fully Qualified Variables

It's always a good idea to avoid using global variables where it's
possible.  Some variables must be either global, such as C<@ISA> or
else fully qualified such as C<@MyModule::ISA>, so that Perl can see
them from different packages.

A combination of C<strict> and C<vars> pragmas keeps modules clean and
reduces a bit of noise.  However, the C<vars> pragma also creates
aliases, as does C<Exporter>, which eat up more memory.  When
possible, try to use fully qualified names instead of C<use vars>.

For example write:

  package MyPackage1;
  use strict;
  @MyPackage1::ISA = qw(CGI);
  $MyPackage1::VERSION = "1.00";
  1;

instead of:

  package MyPackage2;
  use strict;
  use vars qw(@ISA $VERSION);
  @ISA = qw(CGI);
  $VERSION = "1.00";
  1;

Here are the numbers under Perl version 5.6.0

  % perl -MGTop -MMyPackage1 -le 'print GTop->new->proc_mem($$)->size'
  1908736
  % perl -MGTop -MMyPackage2 -le 'print GTop->new->proc_mem($$)->size'
  2031616

We have a difference of 122880 bytes!

Note that Perl 5.6.0 introduced a new our() pragma which works like
my() scope-wise, but declares global variables.

  package MyPackage3;
  use strict;
  our @ISA = qw(CGI);
  our $VERSION = "1.00";
  1;

which uses the same amount of memory as a fully qualified global
variable:

  % perl -MGTop -MMyPackage3 -le 'print GTop->new->proc_mem($$)->size'
  1908736


===

To: Stas Bekman <stas@stason.org>
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: vars vs fqdn vs our benchmark
Date: 26 Dec 2000 15:08:42 -0800

>>>>> "Stas" == Stas Bekman <stas@stason.org> writes:

Stas> Note that Perl 5.6.0 introduced a new our() pragma which works like
Stas> my() scope-wise, but declares global variables.

Stas>   package MyPackage3;
Stas>   use strict;
Stas>   our @ISA = qw(CGI);
Stas>   our $VERSION = "1.00";
Stas>   1;

Stas> which uses the same amount of memory as a fully qualified global
Stas> variable:

Stas>   % perl -MGTop -MMyPackage3 -le 'print GTop->new->proc_mem($$)->size'
Stas>   1908736

You still need a note there that says that 5.6.0 is considered unusable
by conservative sysadmins, and until 5.6.1 has reached gold, you
should avoid using "our".

===

To: "Stas Bekman" <stas@stason.org>
From: "Ken Williams" <ken@forum.swarthmore.edu>
Subject: Re: vars vs fqdn vs our benchmark
Date: Tue, 26 Dec 2000 19:01:17 -0600

stas@stason.org (Stas Bekman) wrote:
>A combination of C<strict> and C<vars> pragmas keeps modules clean and
>reduces a bit of noise.  However, the C<vars> pragma also creates
>aliases, as does C<Exporter>, which eat up more memory.  When
>possible, try to use fully qualified names instead of C<use vars>.

I have to disagree with this benchmark.  The aliases take up only the
tiniest bit of memory, much less than your benchmark seems to indicate. 
What your benchmark shows is the code size of the 'vars.pm' module
itself, not the memory cost of making aliases.  Observe:

-------Code:------------------------------------------------------------
package MyPackage;
use strict;
use vars;
@MyPackage::ISA = qw(CGI);
$MyPackage::VERSION = "1.00";

system "ps -lp $$";
-------Output:---------------
USER    PID %CPU %MEM   VSZ  RSS TTY      S    STARTED         TIME COMMAND
ken   59972  0.0  0.1 3.37M 696K pts/4    S  + 19:50:10     0:00.14 ./vars.pl

-------Code:------------------------------------------------------------
package MyPackage;
use strict;
use vars qw(@ISA $VERSION);
@MyPackage::ISA = qw(CGI);
$MyPackage::VERSION = "1.00";

system "ps u -p $$";
-------Output:---------------
USER    PID %CPU %MEM   VSZ  RSS TTY      S    STARTED         TIME COMMAND
ken   54842  0.0  0.1 3.37M 696K pts/4    S  + 19:50:39     0:00.13 ./vars.pl

-------Code:------------------------------------------------------------
package MyPackage;
use strict;
use vars qw(@ISA $VERSION);
@ISA = qw(CGI);
$VERSION = "1.00";

system "ps u -p $$";
-------Output:---------------
USER    PID %CPU %MEM   VSZ  RSS TTY      S    STARTED         TIME COMMAND
ken   58422  0.0  0.1 3.37M 696K pts/4    S  + 19:52:07     0:00.13 ./vars.pl
 ------------------------------------------------------------------------

(This is using 5.004_04, I didn't have GTop.pm available for fine
resolution of memory.)

So there's no visible difference.  Since some module somewhere in your
processes will be certain to 'use vars', you don't get any real benefit
from leaving it out in one place, and you increase the chances of making
typos.

To confirm, a couple of one-liners:

[~/bin/test],7:56pm% perl -mvars -e 'system "ps u -p $$"'
USER    PID %CPU %MEM   VSZ  RSS TTY      S    STARTED         TIME COMMAND
ken   79047  0.0  0.1 3.34M 680K pts/4    S  + 19:57:23     0:00.13 perl -mvars -e system "ps u -p $$"

[~/bin/test],7:57pm% perl -e 'system "ps u -p $$"'
USER    PID %CPU %MEM   VSZ  RSS TTY      S    STARTED         TIME COMMAND
ken   55275  0.0  0.1 3.00M 336K pts/4    S  + 19:57:39     0:00.06 perl -e system "ps u -p $$"



===
To: Ken Williams <ken@forum.swarthmore.edu>
From: Stas Bekman <stas@stason.org>
Subject: Re: vars vs fqdn vs our benchmark
Date: Wed, 27 Dec 2000 03:01:05 +0100 (CET)

On Tue, 26 Dec 2000, Ken Williams wrote:

> stas@stason.org (Stas Bekman) wrote:
> >A combination of C<strict> and C<vars> pragmas keeps modules clean and
> >reduces a bit of noise.  However, the C<vars> pragma also creates
> >aliases, as does C<Exporter>, which eat up more memory.  When
> >possible, try to use fully qualified names instead of C<use vars>.
> 
> I have to disagree with this benchmark.  The aliases take up only the
> tiniest bit of memory, much less than your benchmark seems to indicate. 
> What your benchmark shows is the code size of the 'vars.pm' module
> itself, not the memory cost of making aliases.  

Nothing like a good peer review :) Thanks Ken. I was wondering myself
about this cool save up. 

$owed_beers{ken}++  

You must come to ApacheCon or TPC to reset this counter, though :)

 -------

Here is a corrected version:

  package MyPackage1;
  use strict;
  use vars; # added only for fair comparison
  @MyPackage1::ISA = qw(CGI);
  $MyPackage1::VERSION = "1.00";
  1;

instead of:

  package MyPackage2;
  use strict;
  use vars qw(@ISA $VERSION);
  @ISA = qw(CGI);
  $VERSION = "1.00";
  1;

Here are the numbers under Perl version 5.6.0

  % perl -MGTop -MMyPackage1 -le 'print GTop->new->proc_mem($$)->size'
    2023424
  % perl -MGTop -MMyPackage2 -le 'print GTop->new->proc_mem($$)->size'
    2031616

We have a difference of 8192 bytes.

Note that Perl 5.6.0 introduced a new our() pragma which works like
my() scope-wise, but declares global variables.

  package MyPackage3;
  use strict;
  use vars; # added only for fair comparison
  our @ISA = qw(CGI);
  our $VERSION = "1.00";
  1;

which uses the same amount of memory as a fully qualified global
variable:

  % perl -MGTop -MMyPackage3 -le 'print GTop->new->proc_mem($$)->size'
  2023424


===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu