[sheepdog] [PATCH 1/4] sheep: don't trim before calculating sha1

Liu Yuan namei.unix at gmail.com
Tue Jul 16 11:30:18 CEST 2013


Trim the object before getting sha1 don't give us much benefit because
1. Most of objects can't be trimmed
2. Require farm to trim the object again
   - need malloc() tmp space for the trim

This is all about sha1ing more bytes vs triming the object, which is faster.
Both will take cpu cycles and no big win one over another.

Without triming the object, farm gets life easier.

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 collie/farm/sha1_file.c |   20 +++++---------------
 sheep/plain_store.c     |    5 -----
 2 files changed, 5 insertions(+), 20 deletions(-)

diff --git a/collie/farm/sha1_file.c b/collie/farm/sha1_file.c
index a2f3561..dc134a4 100644
--- a/collie/farm/sha1_file.c
+++ b/collie/farm/sha1_file.c
@@ -28,22 +28,13 @@
 #include "farm.h"
 #include "util.h"
 
-static void get_sha1(unsigned char *buf, unsigned len, unsigned char *sha1)
+static void get_buffer_sha1(void *buf, size_t len, unsigned char *sha1)
 {
 	struct sha1_ctx c;
-	uint64_t offset = 0;
-	uint32_t length = len;
-	void *tmp = valloc(length);
-
-	memcpy(tmp, buf, len);
-	trim_zero_blocks(tmp, &offset, &length);
 
 	sha1_init(&c);
-	sha1_update(&c, (uint8_t *)&offset, sizeof(offset));
-	sha1_update(&c, (uint8_t *)&length, sizeof(length));
-	sha1_update(&c, tmp, length);
+	sha1_update(&c, buf, len);
 	sha1_final(&c, sha1);
-	free(tmp);
 }
 
 static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
@@ -159,7 +150,7 @@ int sha1_file_write(void *buf, size_t len, unsigned char *outsha1)
 {
 	unsigned char sha1[SHA1_DIGEST_SIZE];
 
-	get_sha1(buf, len, sha1);
+	get_buffer_sha1(buf, len, sha1);
 	if (sha1_buffer_write(sha1, buf, len) < 0)
 		return -1;
 	if (outsha1)
@@ -167,12 +158,11 @@ int sha1_file_write(void *buf, size_t len, unsigned char *outsha1)
 	return 0;
 }
 
-static int verify_sha1_file(const unsigned char *sha1,
-			    void *buf, unsigned long len)
+static int verify_sha1_file(const unsigned char *sha1, void *buf, size_t len)
 {
 	unsigned char tmp[SHA1_DIGEST_SIZE];
 
-	get_sha1(buf, len, tmp);
+	get_buffer_sha1(buf, len, tmp);
 	if (memcmp((char *)tmp, (char *)sha1, SHA1_DIGEST_SIZE) != 0) {
 		fprintf(stderr, "failed, %s != %s\n", sha1_to_hex(sha1),
 			sha1_to_hex(tmp));
diff --git a/sheep/plain_store.c b/sheep/plain_store.c
index 4854be8..dea5e86 100644
--- a/sheep/plain_store.c
+++ b/sheep/plain_store.c
@@ -528,7 +528,6 @@ int default_get_hash(uint64_t oid, uint32_t epoch, uint8_t *sha1)
 	void *buf;
 	struct siocb iocb = {};
 	struct sha1_ctx c;
-	uint64_t offset = 0;
 	uint32_t length;
 	bool is_readonly_obj = oid_is_readonly(oid);
 	char path[PATH_MAX];
@@ -560,11 +559,7 @@ int default_get_hash(uint64_t oid, uint32_t epoch, uint8_t *sha1)
 		return ret;
 	}
 
-	trim_zero_blocks(buf, &offset, &length);
-
 	sha1_init(&c);
-	sha1_update(&c, (uint8_t *)&offset, sizeof(offset));
-	sha1_update(&c, (uint8_t *)&length, sizeof(length));
 	sha1_update(&c, buf, length);
 	sha1_final(&c, sha1);
 	free(buf);
-- 
1.7.9.5




More information about the sheepdog mailing list