From 023dcae19aed0f1e3abaed1d708807ebd7dacffc Mon Sep 17 00:00:00 2001 From: Moinak Ghosh Date: Fri, 17 Aug 2012 13:18:50 +0530 Subject: [PATCH] Speed up sort comparator function. --- rabin/rabin_polynomial.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/rabin/rabin_polynomial.c b/rabin/rabin_polynomial.c index 695139b..eea9a9a 100755 --- a/rabin/rabin_polynomial.c +++ b/rabin/rabin_polynomial.c @@ -264,18 +264,21 @@ cmpblks(const void *a, const void *b) if (a1->cksum_n_offset < b1->cksum_n_offset) { return (-1); } else if (a1->cksum_n_offset == b1->cksum_n_offset) { + int l1 = a1->length; + int l2 = b1->length; + /* * If fingerprints match then compare lengths. Length match makes * for strong exact detection/ordering during sort while stopping - * short of expensive memcmp(). + * short of expensive memcmp() during sorting. + * + * Even though rabin_blockentry_t->length is unsigned we use signed + * int here to avoid branches. In practice a rabin block size at + * this point varies from 2K to 128K. The length is unsigned in + * order to support non-duplicate block merging and large blocks + * after this point. */ - if (a1->length < b1->length) { - return (-1); - } else if (a1->length == b1->length) { - return (0); - } else { - return (1); - } + return (l1 - l2); } else { return (1); }