[FFmpeg-devel] [PATCH] md5: avoid unnecessary memcpy.

Giorgio Vazzana mywing81 at gmail.com
Fri May 17 21:05:06 CEST 2013


2013/5/17 Reimar Döffinger <Reimar.Doeffinger at gmx.de>:
> Signed-off-by: Reimar Döffinger <Reimar.Doeffinger at gmx.de>
> ---
>  libavutil/md5.c |   34 +++++++++++++++++++++++++++-------
>  1 file changed, 27 insertions(+), 7 deletions(-)
>
> diff --git a/libavutil/md5.c b/libavutil/md5.c
> index f8f08f1..7375ce5 100644
> --- a/libavutil/md5.c
> +++ b/libavutil/md5.c
> @@ -139,20 +139,40 @@ void av_md5_init(AVMD5 *ctx)
>      ctx->ABCD[3] = 0x67452301;
>  }
>
> -void av_md5_update(AVMD5 *ctx, const uint8_t *src, const int len)
> +void av_md5_update(AVMD5 *ctx, const uint8_t *src, int len)

I think md5.h needs to be update too?

>  {
> -    int i, j;
> +    const uint8_t *end;
> +    int j;
>
>      j = ctx->len & 63;
>      ctx->len += len;
>
> -    for (i = 0; i < len; i++) {
> -        ctx->block[j++] = src[i];
> -        if (j == 64) {
> -            body(ctx->ABCD, (uint32_t *) ctx->block);
> -            j = 0;
> +    if (j) {
> +        int cnt = FFMIN(len, 64 - j);
> +        memcpy(ctx->block + j, src, cnt);
> +        src += cnt;
> +        len -= cnt;
> +        if (j + cnt < 64)
> +            return;
> +        body(ctx->ABCD, (uint32_t *)ctx->block);
> +    }
> +
> +    end = src + (len & ~63);
> +    if (HAVE_BIGENDIAN || (!HAVE_FAST_UNALIGNED && ((intptr_t)src & 3))) {
> +       while (src < end) {
> +           memcpy(ctx->block, src, 64);
> +           body(ctx->ABCD, (uint32_t *) ctx->block);
> +           src += 64;
> +        }
> +    } else {
> +        while (src < end) {
> +            body(ctx->ABCD, (uint32_t *)src);
> +            src += 64;
>          }
>      }
> +    len &= 63;
> +    if (len > 0)
> +        memcpy(ctx->block, src, len);
>  }
>
>  void av_md5_final(AVMD5 *ctx, uint8_t *dst)
> --
> 1.7.10.4

$ for i in $(seq 1 3); do time ./avutil_md5_test < data; done
d8b61b2c0025919d5321461045c8226f

real    0m1.820s
user    0m1.510s
sys    0m0.270s
d8b61b2c0025919d5321461045c8226f

real    0m1.855s
user    0m1.510s
sys    0m0.310s
d8b61b2c0025919d5321461045c8226f

real    0m1.819s
user    0m1.450s
sys    0m0.330s

LGTM


More information about the ffmpeg-devel mailing list