in reply to Garbage collection problem??

Every time I ran it on my machine, the second time through was faster. That's what I expected; Perl had to do less work allocating memory for the lexical variables thanks to the same optimization that makes my $lexical if 0; so dangerous.

Replies are listed 'Best First'.
Re^2: Garbage collection problem??
by sgt (Deacon) on Dec 27, 2006 at 18:37 UTC

    Hi Chromatic. If I understand you correctly, you are saying that an optimization like  my $i if 0 occurs in this code. I suppose you mean that some construct will infer a runtime penalty (as opposed to compile-time penalty). Just a bit puzzled, would you care to explain a bit more?

    Personally I don't like  my $i if 0; (I would even call it "dangerous" in the sense of not {truly} maintainable) as I prefer the idiom of true shared variables  { my $shared; sub f1 { ... # use $shared } sub f2 {...} }

    In any case I fail to see some optimization kicking in on cygwin. How much faster is the second run on your system? aren't my-allocations very fast with respect to a foreach loop anyway? again seems that all "my slots" will be reserved at compile-time, no?

    The only thing I see is that could be flagged as non-pbp is the construct  my $region; for $region {...} that can be transformed into for my ... for recent enough perl (I wonder what is the best way to handle all versions of perl though...).

    cheers --stephan

      I actually meant that Perl won't free all of the memory used by lexicals when it leaves the scope because of an optimization. The idea is that the next time control enters the scope, the memory requirements will be similar. Avoiding malloc() calls can improve performance.

        Thanks. Interesting point.