Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Slow GC after Scalar::Util::weaken

by jura05 (Novice)
on May 21, 2010 at 17:49 UTC ( [id://841116]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all!

I found that garbage collection after weakening is extemely slow in some cases. Here is the code:

#!/usr/bin/perl -w use strict; use Scalar::Util qw(weaken); my @data = (1, 2, 3); print time, " test 0: main start\n"; &gogogo(); print time, " test 3: main end\n"; sub gogogo { print time, " test 1: func start\n"; my @h; for (1 .. 200000) { my %hash = (data => \@data); weaken($hash{data}); push @h, \%hash; } print time, " test 2: func end\n"; }
Timing:
1273747847 test 0: main start 1273747847 test 1: func start 1273747847 test 2: func end 1273747873 test 3: main end

I ran it on i686 Linux, perl v.5.8.8, but the same behaviour is within perl 5.12 :-(

It this a bug or what?

Update. Submitted a bug:
http://rt.perl.org/rt3/Public/Bug/Display.html?id=75254
Thanks for your replies!

Replies are listed 'Best First'.
Re: Slow GC after Scalar::Util::weaken
by ikegami (Patriarch) on May 21, 2010 at 18:02 UTC
    wow! Please send this to perlbug@perl.org.

    5.12, with weaken:

    1274464820 test 0: main start 1274464820 test 1: func start 1274464820 test 2: func end 1274464855 test 3: main end

    Weaken commented out:

    1274464870 test 0: main start 1274464870 test 1: func start 1274464870 test 2: func end 1274464870 test 3: main end
Re: Slow GC after Scalar::Util::weaken
by MidLifeXis (Monsignor) on May 21, 2010 at 20:27 UTC

    Same results here. Different architecture.

    This is perl, v5.8.8 built for PA-RISC2.0

    With weaken() call:

    1274473378 test 0: main start 1274473378 test 1: func start 1274473380 test 2: func end 1274473443 test 3: main end

    Without weaken() call:

    1274473556 test 0: main start 1274473556 test 1: func start 1274473558 test 2: func end 1274473558 test 3: main end

    Run multiple times with similar results.

    --MidLifeXis

      Sample TAP-ready file:

      #!/bin/env perl # Test script for RT 75254 # URL: http://rt.perl.org/rt3/Public/Bug/Display.html?id=75254 # URL: http://www.perlmonks.org/?node_id=841116 use strict; use warnings; use Time::HiRes qw(time); use Test::More; use Scalar::Util qw(weaken); # The ratio of time with weaken vs without for which we test. What is # the performance target? my @timefactors = reverse ( 1.5, 2, 5, 10, 25, 50, 100 ); plan tests => 5 + scalar(@timefactors); my @data = (1, 2, 3); pass("Begin performance test for weaken(): this may take a while."); # Record the time without using weaken() my $withouttime; { my $endtime = &withoutweaken(); my $stoptime = time; $withouttime = $stoptime - $endtime; } pass(sprintf("Time without weaken(): %.2f", $withouttime)); # Record the time using weaken() my $withtime; { my $endtime = &withweaken(); my $stoptime = time; $withtime = $stoptime - $endtime; } pass(sprintf("Time with weaken(): %.2f", $withtime)); my $timeratio = ($withtime / $withouttime); pass(sprintf( "Time Ratio: TIME(with) / TIME(without): %.2f", $timerat +io )); foreach ( @timefactors ) { ok( $timeratio < $_, "Time ratio is < $_" ); } pass("End performance test for weaken()."); sub withweaken { my @h; for (1 .. 200000) { my %hash = (data => \@data); weaken($hash{data}); push @h, \%hash; } return time; } sub withoutweaken { my @h; for (1 .. 200000) { my %hash = (data => \@data); push @h, \%hash; } return time; }

      --MidLifeXis

Re: Slow GC after Scalar::Util::weaken
by proceng (Scribe) on May 22, 2010 at 04:16 UTC
    Just for completeness sake: All current versions (5.8.9, 5.10.1 and 5.12.1) show the same behaviour.

    Verified on: This is perl 5, version 12, subversion 1 (v5.12.1) built for x86_64-linux-thread-multi-ld (other versions on same platform).

Re: Slow GC after Scalar::Util::weaken (O)
by tye (Sage) on Jun 03, 2010 at 16:27 UTC

    Perl weak references are implemented as a singly-linked list. Destroying a weak reference is thus O(n) if you have n weak references to a particular variable. So destroying N weak references to the same variable is O(N**2).

    Having 200k weak references to the same variable is not a normal use-case. I'm not surprised that it is slow.

    - tye        

      According to Devel::Peek, it's an array rather than a linked list. That doesn't change the complexity, though.

      To delete weak ref #X:
      O(X) to locate + O(N-X) to delete
      = O(N) total

      Best, average and worse case to delete one weak ref of N is O(N).

      To delete all N weak refs:
      O(N) + O(N-1) + O(N-2) + ... + O(1)
      = O(N**2)

      Best, average and worse case to delete all N weak refs is O(N**2).


      A hash, on the other hand, would scale much better.

      Best, average and worse case to delete one weak ref of N is O(1).
      Best, average and worse case to delete all N weak refs is O(N).

      Update: Added analysis for hash.

Re: Slow GC after Scalar::Util::weaken
by ikegami (Patriarch) on Jun 03, 2010 at 17:59 UTC
    A workaround is to add a layer in between.
    #!/usr/bin/perl -w use strict; use Scalar::Util qw(weaken); my @data = (1, 2, 3); weaken( my $indirection = \@data ); print time, " test 0: main start\n"; &gogogo(); print time, " test 3: main end\n"; sub gogogo { print time, " test 1: func start\n"; my @h; for (1 .. 200000) { my %hash = (data => $indirection); push @h, \%hash; } print time, " test 2: func end\n"; }
    You'd have to change
    my $data = $h[0]{data};
    to
    my $data = ${ $h[0]{data} };
    if you use this workaround.
Re: Slow GC after Scalar::Util::weaken
by ikegami (Patriarch) on Jun 03, 2010 at 18:13 UTC
    Alternate workaround: Destroy the strong ref before the weak refs:
    #!/usr/bin/perl -w use strict; use Scalar::Util qw(weaken); my $data = [1, 2, 3]; print time, " test 0: main start\n"; &gogogo(); print time, " test 3: main end\n"; sub gogogo { print time, " test 1: func start\n"; my @h; for (1 .. 200000) { my %hash = (data => $data); weaken($hash{data}); push @h, \%hash; } print time, " test 2: func end\n"; undef $data; return; }

    The undef can't be the last statement for reasons unclear to me.

      Thanks, ikegami! I would agree with tye that is not normal use-case; I should rework my code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://841116]
Approved by ikegami
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-23 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found