dragonchild has asked for the wisdom of the Perl Monks concerning the following question:

I'm curious - how far out and how expensive is the scratchpad a closure carries around? One of the things a module of mine does is to generate accessors using closures. I'm playing around with it and noticed that I did something like:
sub make_accessor { my $self = shift; my ($name, $index) = @_; no strict 'refs'; *{ref($self) . "::$name"} = sub { my $self = shift; return $self->[$index] unless @_; $self->[$index] = $_[0]; $self; }; 1; }

That's a lot of extra lexicals that aren't being used in the closure. Does it carry them around? I rewrote it to look something like:

sub make_accessor { my ($index) = $_[2]; no strict 'refs'; *{ref($_[0]) . "::$_[1]"} = sub { return $_[0]->[$index] unless @_ > 1; $_[0]->[$index] = $_[1]; $_[0]; }; 1; }

I benchmarked as follows and had the following results:

use Benchmark qw(cmpthese); my @array; make_accessor1(code1, 0); make_accessor2(code2, 0); cmpthese(-10, { lexicals => sub { code1(5) }, no_lexic => sub { code2(5) }, }); sub make_accessor1 { my ($name, $index) = @_; *{$name} = sub { $array[$index] = $_[0] }; } sub make_accessor2 { my $index = $_[1]; *{$_[0]} = sub { $array[$index] = $_[0] }; }

Benchmark: running lexicals, no_lexic, each for at least 5 CPU seconds +... lexicals: 5 wallclock secs ( 5.43 usr + -0.01 sys = 5.42 CPU) @ 70 +0151.48/s (n=3794821) no_lexic: 7 wallclock secs ( 5.34 usr + 0.00 sys = 5.34 CPU) @ 71 +6661.80/s (n=3826974) Rate lexicals no_lexic lexicals 700151/s -- -2% no_lexic 716662/s 2% --

The speed difference wasn't impressive. But, I don't know how to check memory usage ... (Obviously, in the case above, it won't be large, but if the closure creation was more complicated ... ?)

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Closures and lexical scope
by Abigail-II (Bishop) on Aug 27, 2003 at 14:53 UTC
    That's a lot of extra lexicals that aren't being used in the closure. Does it carry them around?

    That's easy to test:

    #!/usr/bin/perl use strict; use warnings; sub DESTROY { print "DESTROY called!\n"; } sub make_accessor { my $self = shift; my ($name, $index) = (bless ([] => 'main'), 1); no strict 'refs'; *{ref($self) . "::$name"} = sub { my $self = shift; return $self->[$index] unless @_; $self->[$index] = $_[0]; $self; }; 1; } my $foo = make_accessor; print "End of program\n"; __END__ DESTROY called! End of program

    So the answer is "no".

    Abigail

      I understand now. By adding $name = $name; to the closure, the DESTROY() call is done after the "End of program" line is printed.

      Thank you! :-)

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Closures and lexical scope
by broquaint (Abbot) on Aug 27, 2003 at 15:15 UTC
    That's a lot of extra lexicals that aren't being used in the closure. Does it carry them around?
    The long and the short of it is 'no'. A closure will only store the lexical variables in its lexical pad that are referenced within the subroutine. As Abigail-II's code aptly demonstrates $name's DESTROY method is called upon the exit of make_accessor() (which is its immediate lexical scope) and therefore wasn't held in the closure created.
    HTH

    _________
    broquaint

    update: added clarification

Re: Closures and lexical scope
by tcf22 (Priest) on Aug 27, 2003 at 14:47 UTC
    Maybe the GTop module does what you want. I've never used it, but it looks like it might be helpful.

    Try someting like this:
    use Data::Dumper; use GTop; my $gtop = GTop->new; my @array; make_accessor1('code1', 0); make_accessor2('code2', 0); ##Lexical my $data = $gtop->proc_mem($$)->rss; report('Before Lexical', $data); &code1(5); $data = $gtop->proc_mem($$)->rss; report('After Lexical', $data); ##Non-Lexical $data = $gtop->proc_mem($$)->rss; report('Before Non-Lexical', $data); &code2(5); $data = $gtop->proc_mem($$)->rss; report('After Non-Lexical', $data); ########## # SUBS ########## sub make_accessor1 { my ($name, $index) = @_; *{$name} = sub { $array[$index] = $_[0] }; } sub make_accessor2 { my $index = $_[1]; *{$_[0]} = sub { $array[$index] = $_[0] }; } ##REPORTER sub report{ my $title = $_[0]; my $data = $_[1]; print "$title\n--------------------\n"; print Dumper $data; print "--------------------\n\n"; }


    Update: Added sample code
Re: Closures and lexical scope
by perrin (Chancellor) on Aug 27, 2003 at 14:49 UTC
    On Linux you can check size by reading the /proc filesystem. See Apache::SizeLimit for sample code.