The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Coro::LocalScalar::XS - Different scalar values in coroutines

ABOUT

This is optimized XS version of Coro::LocalScalar. It's almost two times faster and has simplier api - only one function. This module destroys all local data(and DESTROYs are called) when coroutine get destroyed. This is useful for example if you want to have global variable $request with different object in each coro.

Coro::LocalScalar::XS keeps reference to localized variable, so localize only variables, that will persist all execution time. Localizing hundreds of variables is also bad idea, because each variable adds little overhead when each coro is destroyed

SYNOPSIS

        use Coro;
        use Coro::EV;


        my $scalar;

        use Coro::LocalScalar::XS;
        localize($scalar); # $scalar is now different in all coros. Current value of $scalar is deleted.

        # $hash{element} = undef; # hash element MUST exist if you want to localize it correctly
        # localize($hash{element}); 
        # localizing arrays or hashes unsupported, use refs
        
        # or
        # use Coro::LocalScalar::XS qw//; # don't export localize
        # Coro::LocalScalar::XS->localize($scalar);

        async {
                        $scalar = "thread 1";
                        print "1 - $scalar\n";
                        cede;
                        print "3 - $scalar\n";
                        cede;
                        print "5 - $scalar\n";
                        
        };

        async {
                        $scalar = "thread 2";
                        print "2 - $scalar\n";
                        cede;
                        print "4 - $scalar\n";
                        cede;
                        print "6 - $scalar\n";
        };

        EV::loop;
        

        
        1 - thread 1
        2 - thread 2
        3 - thread 1
        4 - thread 2
        5 - thread 1
        6 - thread 2
        

BENCHMARK

         t/benchmark.pl
        
                                                        Rate Coro::LocalScalar Coro::LocalScalar::XS Coro::Localize
        Coro::LocalScalar     10000/s                --                  -45%           -52%
        Coro::LocalScalar::XS 18282/s               83%                    --           -12%
        Coro::Localize        20661/s              107%                   13%             --
        

Coro::Localize is little bit faster, but Coro::LocalScalar::XS allows localizing hash elements