Last active
August 29, 2015 14:25
-
-
Save ab5tract/691fa9756a0a722bd845 to your computer and use it in GitHub Desktop.
Sereal::Pool::Perl5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
use Inline::Perl5; | |
class Sereal::Encoder::Perl5 { | |
has Inline::Perl5 $!p5; | |
has $!encoder; | |
submethod BUILD() { | |
$!p5 := Inline::Perl5.new; | |
$!p5.use('Sereal::Encoder'); | |
$!p5.run('sub generate_encoder { Sereal::Encoder->new };'); | |
$!encoder := $!p5.call('generate_encoder'); | |
$!p5.run('sub batch_encoder { my $encoder = Sereal::Encoder->new; my @r; push @r, $encoder->encode($_) for @_; return @r }'); | |
} | |
method encode( $blob ) { | |
# my $val := $!encoder.encode( $blob ); | |
# $!encoder := $!p5.call('generate_encoder'); | |
# $val; | |
$!encoder.encode( $blob ); | |
} | |
method batch-encode( *@data ) { | |
# say @data; | |
$!p5.call('batch_encoder',@data); | |
} | |
method Str { | |
$!p5 ~ "_" ~ $!encoder; | |
} | |
method gist { | |
~self; | |
} | |
} | |
class Sereal::Decoder::Perl5 { | |
has Inline::Perl5 $!p5; | |
has $!decoder; | |
submethod BUILD() { | |
$!p5 := Inline::Perl5.new; | |
$!p5.use('Sereal::Decoder'); | |
$!p5.run('sub generate_decoder { Sereal::Decoder->new };'); | |
$!decoder := $!p5.call('generate_decoder'); | |
} | |
method decode( $blob ) { | |
$!decoder.decode( $blob ); | |
} | |
method Str { | |
$!p5 ~ "_" ~ $!decoder; | |
} | |
method gist { | |
~self; | |
} | |
} | |
class Sereal::Pool::Perl5 { | |
has Sereal::Encoder::Perl5 @.encoders; | |
has Sereal::Decoder::Perl5 @.decoders; | |
has Int $.pool-size; | |
submethod BUILD( :$!pool-size ) { | |
@!encoders = Sereal::Encoder::Perl5.new xx $!pool-size; | |
@!decoders = Sereal::Decoder::Perl5.new xx $!pool-size; | |
} | |
method batch-encode( *@batch where * <= $!pool-size ) { | |
await do for @!encoders[0..+@batch-1].kv -> $k,$v { start { $v.batch-encode( @batch[$k] ); } } | |
} | |
method Str { | |
[~] @!encoders, @!decoders; | |
} | |
} | |
#use Test; | |
# | |
#can my $d = Sereal::Decoder::Perl5.new, "Can create Sereal::Decoder::Perl5 object"; | |
my $init = now; | |
my $e = Sereal::Encoder::Perl5.new; | |
say ~$e; | |
say "Elapsed: {now - $init}"; | |
my $pool = Sereal::Pool::Perl5.new( :pool-size(77) ); | |
say ~$pool; | |
say "Elapsed: {now - $init}"; | |
my $enc-val = $e.encode( %( hash => "of", things => "to", love => "eventually" ) ); | |
my @dec-val = $pool.decoders>>.decode( $enc-val ); | |
say @dec-val.perl; | |
say "Elapsed: {now - $init}"; | |
@dec-val = await do for $pool.decoders -> $d { start { $d.decode( $enc-val ) } }; | |
say @dec-val.perl; | |
say "Elapsed: {now - $init}"; | |
# round 1 batch-encode | |
my @things = [ 0..^777 ] xx 1; | |
say +@things ~ " things of " ~ +@things[0] ~ " things each"; | |
my @enc-vals; | |
for @things.rotor( $pool.pool-size, :partial ) -> $t { | |
@enc-vals.push: [ $pool.batch-encode( @$t ) ]; | |
say "another tick: {+@enc-vals} ~~ {+@enc-vals[*-1]}"; | |
} | |
say "Elapsed: {now - $init}"; | |
# round 2 batch-encode | |
@things = [ 0..^777 ] xx 10; | |
say +@things ~ " things of " ~ +@things[0] ~ " things each"; | |
for @things.rotor( $pool.pool-size, :partial ) -> $t { | |
@enc-vals.push: [ $pool.batch-encode( @$t ) ]; | |
say "another tick: {+@enc-vals} ~~ {+@enc-vals[*-1]}"; | |
} | |
say "Elapsed: {now - $init}"; | |
# round 3 batch-encode | |
@things = [ 0..^777 ] xx 50; | |
say +@things ~ " things of " ~ +@things[0] ~ " things each. Elapsed: {now - $init}"; | |
for @things.rotor( $pool.pool-size, :partial ) -> $t { | |
@enc-vals.push: [ $pool.batch-encode( @$t ) ]; | |
say "another tick: {+@enc-vals} ~~ {+@enc-vals[*-1]}"; | |
} | |
say "Elapsed: {now - $init}"; | |
#my @enc-v; | |
#@enc-v = do for @things -> $t { $e.batch-encode(@$t) }; | |
#say "Elapsed: {now - $init}"; | |
# | |
try { | |
$pool.batch-encode( 0..80 ); # fails the 'where' constraint | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment