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.


In reply to Closures and lexical scope by dragonchild

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.