#!/usr/bin/perl

# ->resume is not exactly cheap (it saves/restores a LOT
# of global variables), but shouldn't be slow. just to show
# how fast it is, this little proggie compares a normal subroutine
# call with two calls of transfer in a loop.

use Coro;
use Benchmark;

sub a() { }

$a = bless {}, main::;

sub b {
   my ($self) = shift;
   $self->{b} = shift if @_;
   $self->{b};
}

$b = async {
   # do a little unrolling...
   while() {
      cede; cede; cede; cede; cede;
   }
};

cede;

$main = $Coro::main;

*transfer = \&Coro::State::transfer;

sub doit0 {
   while() {
      # some unrolling here as well..
      transfer($c0, $main,  0); transfer($c0, $main,  0);
      transfer($c0, $main,  0); transfer($c0, $main,  0);
      transfer($c0, $main,  0); transfer($c0, $main,  0);
   }
}

sub doit1 {
   while() {
      # some unrolling here as well..
      transfer($c1, $main, -1); transfer($c1, $main, -1);
      transfer($c1, $main, -1); transfer($c1, $main, -1);
      transfer($c1, $main, -1); transfer($c1, $main, -1);
   }
}

$c0 = Coro::State::_newprocess [sub {
   doit0(1,2,3,4,5,6,7,8,9);
}];

$c1 = Coro::State::_newprocess [sub {
   doit1(1,2,3,4,5,6,7,8,9);
}];

transfer($main, $c0, 0);
transfer($main, $c1, -1);

timethese 1000000, {
   empty    => '&a; &a',
   method   => '$a->b(5); $a->b(6)',
   cede     => 'cede',
   transfer0=> 'transfer($main, $c0, 0)',
   transfer1=> 'transfer($main, $c1, -1)',
};

