You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Stas Bekman <st...@stason.org> on 2000/12/26 22:32:42 UTC

vars vs fqdn vs our benchmark

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





_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:stas@stason.org   http://apachetoday.com http://logilune.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/  



Re: vars vs fqdn vs our benchmark

Posted by Stas Bekman <st...@stason.org>.
On 26 Dec 2000, Randal L. Schwartz wrote:

> >>>>> "Stas" == Stas Bekman <st...@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".

Ok.

...the main target of this material is the book we are working on. So I
suppose that till the time it will be published 5.6.1 will be gold :)


_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:stas@stason.org   http://apachetoday.com http://logilune.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/  



Re: vars vs fqdn vs our benchmark

Posted by "Randal L. Schwartz" <me...@stonehenge.com>.
>>>>> "Stas" == Stas Bekman <st...@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".

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<me...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Re: vars vs fqdn vs our benchmark

Posted by Stas Bekman <st...@stason.org>.
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


_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:stas@stason.org   http://apachetoday.com http://logilune.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/  



Re: vars vs fqdn vs our benchmark

Posted by Ken Williams <ke...@forum.swarthmore.edu>.
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 $$"



  -------------------                            -------------------
  Ken Williams                             Last Bastion of Euclidity
  ken@forum.swarthmore.edu                            The Math Forum