Speed up md5 hash filling/verifying by 20%
authorJens Axboe <jens.axboe@oracle.com>
Fri, 27 Jul 2007 11:16:24 +0000 (13:16 +0200)
committerJens Axboe <jens.axboe@oracle.com>
Fri, 27 Jul 2007 11:16:24 +0000 (13:16 +0200)
Get rid of the on-stack hash copies, hash directly into the buffer.
We need to 'fix' md5 to just initially clear a/b/c/d in md5_update().
Tested, works, generates same checksums.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
md5.c
md5.h
verify.c

diff --git a/md5.c b/md5.c
index cf1f814a0d63c2cee558c8049140156e90f9723c..6b61012e85da005d418a7c4ea1ef22349038ed82 100644 (file)
--- a/md5.c
+++ b/md5.c
@@ -9,10 +9,11 @@ static void md5_transform(uint32_t *hash, uint32_t const *in)
 {
        uint32_t a, b, c, d;
 
-       a = hash[0];
-       b = hash[1];
-       c = hash[2];
-       d = hash[3];
+       /*
+        * This used to be set from the hash[0..3] input, but we can
+        * skip a memory clear if we just start from 0 instead.
+        */
+       a = b = c = d = 0;
 
        MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
        MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
@@ -82,10 +83,10 @@ static void md5_transform(uint32_t *hash, uint32_t const *in)
        MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
        MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
 
-       hash[0] += a;
-       hash[1] += b;
-       hash[2] += c;
-       hash[3] += d;
+       hash[0] = a;
+       hash[1] = b;
+       hash[2] = c;
+       hash[3] = d;
 }
 
 void md5_update(struct md5_ctx *mctx, const uint8_t *data, unsigned int len)
diff --git a/md5.h b/md5.h
index 9d1cf4cfb5021524aae63e8c50cbaffe3f4245ef..80fbcec8ad3a5ef9f06ff8f7a7a33c2ad14d400d 100644 (file)
--- a/md5.h
+++ b/md5.h
@@ -17,7 +17,7 @@
        (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x)
 
 struct md5_ctx {
-       uint32_t hash[MD5_HASH_WORDS];
+       uint32_t *hash;
        uint32_t block[MD5_BLOCK_WORDS];
        uint64_t byte_count;
 };
index 178b6d6a21d4b007058eaca1322112057c50464f..42a56d7162631e51be05d10394b40fa91b1bd382 100644 (file)
--- a/verify.c
+++ b/verify.c
@@ -95,17 +95,18 @@ static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
 
 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
 {
-       unsigned char *p = io_u->buf;
-       struct md5_ctx md5_ctx;
+       unsigned char *p = io_u->buf + sizeof(*hdr);
+       uint32_t hash[MD5_HASH_WORDS];
+       struct md5_ctx md5_ctx = {
+               .hash = hash,
+       };
 
-       memset(&md5_ctx, 0, sizeof(md5_ctx));
-       p += sizeof(*hdr);
        md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
 
-       if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
+       if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(hash))) {
                log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
                hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
-               hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
+               hexdump(md5_ctx.hash, sizeof(hash));
                return 1;
        }
 
@@ -166,11 +167,11 @@ static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
 
 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
 {
-       struct md5_ctx md5_ctx;
+       struct md5_ctx md5_ctx = {
+               .hash = (uint32_t *) hdr->md5_digest,
+       };
 
-       memset(&md5_ctx, 0, sizeof(md5_ctx));
        md5_update(&md5_ctx, p, len);
-       memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
 }
 
 /*