DCIT / perl-Crypt-JWT

Other
54 stars 18 forks source link

Speed up decode_jwt #37

Closed dkechag closed 1 year ago

dkechag commented 1 year ago

I noticed that for large tokens (not huge, just a couple dozen kb), especially for JWS, the decode function was very slow, and it looks like most of the runtime (90% in my case) was spent by the regex. While large tokens are probably not common, I thought I'd make it faster.

This is some benchmarking code:

my $data = "The rain in Spain stays mainly in the plain." x 10000;
my $key  = '-----BEGIN PRIVATE KEY-----
    MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgYirTZSx+5O8Y6tlG
    cka6W6btJiocdrdolfcukSoTEk+hRANCAAQkvPNu7Pa1GcsWU4v7ptNfqCJVq8Cx
    zo0MUVPQgwJ3aJtNM1QMOQUayCrRwfklg+D/rFSUwEUqtZh7fJDiFqz3
    -----END PRIVATE KEY-----';

my $token = encode_jwt(
    payload => $data,
    alg     => 'ES256',
    key     => \$key,
);

cmpthese(-2, {
    decode_jwt => sub {
        my $out = decode_jwt(token=>$token, key=>\$key);
    },
    decode_jwt2 => sub {
        my $out = decode_jwt2(token=>$token, key=>\$key);
    },
});

Where decode_jwt2 is the version on this PR. In this (extreme) example the overall speedup of decode is over 4x with perl 5.38.0:

              Rate  decode_jwt decode_jwt2
decode_jwt  37.8/s          --        -78%
decode_jwt2  169/s        345%          --
dkechag commented 1 year ago

The CI tests fail on cygwin, seems it can't install JSON::XS, CryptX so nothing related to this PR.