SPUG: Coupling of closure parsing and compilation

Fred Morris m3047 at inwa.net
Sun Oct 5 18:22:51 CDT 2003


I was toying with the notion of using closures for the second part of a
two-phase execution, but I'm not clear on the performance implications.
What's going to happen is an object is going to do some setup, then there
needs to be a handoff to something else, which needs to call back after
it's done:


(stuff happens..)

$a->{b} = $b;

$a->do_stuff();

$b->do_more_stuff();


package A;

sub do_stuff() {

    my $self = shift;

    (stuff happens)

    $self->{b}->here_ya_go( sub { $self->finis(); } );
}

sub finis() {

    ...
}

package B;

sub here_ya_go() {

    my $self = shift;
    my $finis = shift;

    $self->{finis} = $finis;
}

sub do_more_stuff() {

    my $self = shift;

    (more stuff happens)

    &{$self->{finis}}();

    (should I delete $self->{finis} here or am I just superstitious?)
}


There's more to it than that and I know that syntax is somewhat fractured,
but hopefully it makes the intent clear. :-\


I've used them for fairly static purposes with good results (as subroutine
arguments, for immediate execution).

What I can't quite get clear in my mind is the coupling between the parsing
of the Perl source and when the substitutions actually occur which produce
the closure, and what effect this would have on performance both in terms
of cpu and memory; the closures won't be particularly large chunks of code
(although they'll call fairly large chunks of code). But they will be
(re)created often.


Is the source for the closure parsed with the rest of the chunk, or at the
time that the closure is instantiated?

Is the instantiated closure produced by (cloning if necessary, and then)
substituting into the (parsed) opcode tree?

What's the overhead of using a closure once and then throwing it away?

Am I just asking for circular memory references here, given the OO "have
you call me" scenario (bear in mind, the object(s) creating the closures
won't be keeping handles to them, but they will be referencing themselves
within the closures).


Experience only, please. Pursuit of elegance is great, if it has a high
expectation of success.

--

Fred Morris
m3047 at inwa.net





More information about the spug-list mailing list