MooX::Pression — now much faster

The test suite for MooX::Pression used to run in 79 seconds on my laptop. Now it's at 10 seconds.

And no, I didn't cut out any tests — I switched from using Keyword::Declare to a combination of Keyword::Simple and PPR. (Keyword::Declare is a wrapper around Keyword::Simple and PPR, but I found out by using them directly, I could massively improve compile-time speed.)

MooX::Pression allows you to build classes and roles with multimethods, types, method signatures, and sweet, sweet, sugary syntax…

use v5.18;
use strict;
use warnings;

my $json = MyApp->new_json_encoder;

say $json->stringify({
   foo  => 123,
   bar  => [1,2,3],
   baz  => \1,
   quux => { xyzzy => 666 },
});

package MyApp {
   use MooX::Pression;
   
   class JSON::Encoder {
      multi method stringify (Undef $value) {
         'null';
      }
      
      multi method stringify (ScalarRef[Bool] $value) {
         $$value ? 'true' : 'false';
      }
      
      multi method stringify (Num $value) {
         $value;
      }
      
      multi method stringify :alias(quote_str) (Str $value)  {
         sprintf(q<"%s">, quotemeta $value);
      }
      
      multi method stringify (ArrayRef $arr) {
         sprintf(
            q<[%s]>,
            join(q<,>, map($self->stringify($_), @$arr))
         );
      }
      
      multi method stringify (HashRef $hash) {
         sprintf(
            q<{%s}>,
            join(
               q<,>,
               map sprintf(
                  q<%s:%s>,
                  $self->quote_str($_),
                  $self->stringify($hash->{$_}),
               ), sort keys %$hash
            ),
         );
      }
   }
}

 

1 Comment

Too good, very tempting. Thanks for your great contributions.

Leave a comment

About Toby Inkster

user-pic I'm tobyink on CPAN, IRC and PerlMonks.