Hypersonic

 view release on metacpan or  search on metacpan

bench/future-benchmark.pl  view on Meta::CPAN

my $start = time();
for (1..$chain_iter) {
    my $f = Hypersonic::Future->new;
    my $chain = $f->then(sub { $_[0] * 2 })
                 ->catch(sub { 0 })
                 ->finally(sub { });
    $f->done(21);
}
my $elapsed = time() - $start;
my $rate = $chain_iter / $elapsed;
printf "Hypersonic::Future: %.0f/s (%d iterations in %.2f seconds)\n", $rate, $chain_iter, $elapsed;
print "\n";

# ============================================================================
# Summary
# ============================================================================
print "=" x 70, "\n";
print "Benchmark complete!\n";
print "=" x 70, "\n";

if ($has_future_xs) {

lib/Hypersonic/Future.pm  view on Meta::CPAN

	Hypersonic::Future:  3 wallclock secs ( 2.06 usr +  0.01 sys =  2.07 CPU) @ 2986523.67/s (n=6182104)

	----------------------------------------------------------------------
	Test 10: on_done() + done() - Callback [Hypersonic only]
	----------------------------------------------------------------------
	Hypersonic::Future:  2 wallclock secs ( 2.54 usr +  0.03 sys =  2.57 CPU) @ 2142030.74/s (n=5505019)

	----------------------------------------------------------------------
	Test 11: then()->catch()->finally() [Hypersonic only]
	----------------------------------------------------------------------
	Hypersonic::Future: 1415799/s (10000 iterations in 0.01 seconds)

	======================================================================
	Benchmark complete!
	======================================================================

lib/Hypersonic/UA.pm  view on Meta::CPAN

    my ($class, $builder) = @_;

    $builder->comment('Run all pending async requests to completion')
      ->xs_function('xs_ua_run')
      ->xs_preamble
      ->line('if (items < 1) croak("Usage: $ua->run()");')
      ->blank
      ->line('SV *self_sv = ST(0);')
      ->blank
      ->comment('Poll until all pending requests complete')
      ->line('int iterations = 0;')
      ->line('int max_iterations = 10000;')
      ->blank
      ->line('SPAGAIN;')
      ->line('while (iterations++ < max_iterations) {')
      ->line('    ENTER; SAVETMPS;')
      ->line('    PUSHMARK(SP);')
      ->line('    XPUSHs(self_sv);')
      ->line('    PUTBACK;')
      ->blank
      ->line('    call_method("pending", G_SCALAR);')
      ->line('    SPAGAIN;')
      ->blank
      ->line('    int pending = POPi;')
      ->line('    PUTBACK;')

lib/Hypersonic/UA.pm  view on Meta::CPAN


    $builder->comment('Run one async request to completion')
      ->xs_function('xs_ua_run_one')
      ->xs_preamble
      ->line('if (items < 2) croak("Usage: $ua->run_one($future)");')
      ->blank
      ->line('SV *self_sv = ST(0);')
      ->line('SV *future_sv = ST(1);')
      ->blank
      ->comment('Poll until this specific future resolves')
      ->line('int iterations = 0;')
      ->line('int max_iterations = 10000;')
      ->blank
      ->line('SPAGAIN;')
      ->line('while (iterations++ < max_iterations) {')
      ->comment('    Check if future is done')
      ->line('    ENTER; SAVETMPS;')
      ->line('    PUSHMARK(SP);')
      ->line('    XPUSHs(future_sv);')
      ->line('    PUTBACK;')
      ->blank
      ->line('    call_method("is_ready", G_SCALAR);')
      ->line('    SPAGAIN;')
      ->blank
      ->line('    int ready = POPi;')

lib/Hypersonic/UA/Async.pm  view on Meta::CPAN

      ->line('SV **pending_svp = hv_fetch(ua_hv, "_async_pending", 14, 0);')
      ->if('!pending_svp || !SvROK(*pending_svp)')
        ->line('ST(0) = sv_2mortal(newSViv(0));')
        ->line('XSRETURN(1);')
      ->endif
      ->blank
      ->line('AV *pending_av = (AV *)SvRV(*pending_svp);')
      ->blank
      ->comment('Main tick loop - process until no pending or no progress')
      ->line('int total_completed = 0;')
      ->line('int iterations = 0;')
      ->line('int max_iterations = 1000;  /* Safety cap */')
      ->blank
      ->line('tick_loop:')
      ->line('{')
      ->line('    I32 len = av_len(pending_av) + 1;')
      ->line('    if (len == 0 || iterations++ >= max_iterations) {')
      ->line('        ST(0) = sv_2mortal(newSViv(len));')
      ->line('        XSRETURN(1);')
      ->line('    }')
      ->blank
      ->comment('    Collect slots from pending array')
      ->line('    int slots[MAX_ASYNC_CONTEXTS];')
      ->line('    int slot_count = 0;')
      ->blank
      ->line('    for (j = 0; j < len; j++) {')
      ->line('        SV **slot_svp = av_fetch(pending_av, j, 0);')

t/2013-ua-run-poll.t  view on Meta::CPAN

    require XS::JIT::Builder;
    my $builder = XS::JIT::Builder->new();
    
    Hypersonic::UA->gen_xs_run($builder);
    my $code = $builder->code();
    
    like($code, qr/xs_ua_run/, 'Function name');
    like($code, qr/Usage:.*\$ua->run/, 'Usage message');
    
    # Loop structure
    like($code, qr/while.*iterations/, 'Has iteration loop');
    like($code, qr/max_iterations/, 'Has max iterations limit');
    
    # Pending check
    like($code, qr/call_method.*"pending"/, 'Checks pending count');
    like($code, qr/pending == 0.*break/, 'Breaks when no pending');
    
    # Tick call
    like($code, qr/call_method.*"tick"/, 'Calls tick');
};

subtest 'gen_xs_run_one generates correct code' => sub {



( run in 2.116 seconds using v1.01-cache-2.11-cpan-71847e10f99 )