filter-branch documentation: some more touch-ups.
[gitweb.git] / diffcore-delta.c
index 835d82c9939644faa808bb2f616136549af521db..a038b166c5aa095ffdf31c2fb0e3263ed13cf8b1 100644 (file)
@@ -5,23 +5,20 @@
 /*
  * Idea here is very simple.
  *
- * We have total of (sz-N+1) N-byte overlapping sequences in buf whose
- * size is sz.  If the same N-byte sequence appears in both source and
- * destination, we say the byte that starts that sequence is shared
- * between them (i.e. copied from source to destination).
+ * Almost all data we are interested in are text, but sometimes we have
+ * to deal with binary data.  So we cut them into chunks delimited by
+ * LF byte, or 64-byte sequence, whichever comes first, and hash them.
  *
- * For each possible N-byte sequence, if the source buffer has more
- * instances of it than the destination buffer, that means the
- * difference are the number of bytes not copied from source to
- * destination.  If the counts are the same, everything was copied
- * from source to destination.  If the destination has more,
- * everything was copied, and destination added more.
+ * For those chunks, if the source buffer has more instances of it
+ * than the destination buffer, that means the difference are the
+ * number of bytes not copied from source to destination.  If the
+ * counts are the same, everything was copied from source to
+ * destination.  If the destination has more, everything was copied,
+ * and destination added more.
  *
  * We are doing an approximation so we do not really have to waste
  * memory by actually storing the sequence.  We just hash them into
  * somewhere around 2^16 hashbuckets and count the occurrences.
- *
- * The length of the sequence is arbitrarily set to 8 for now.
  */
 
 /* Wild guess at the initial hash size */
@@ -40,8 +37,8 @@
 #define HASHBASE 107927
 
 struct spanhash {
-       unsigned long hashval;
-       unsigned long cnt;
+       unsigned int hashval;
+       unsigned int cnt;
 };
 struct spanhash_top {
        int alloc_log2;
@@ -50,7 +47,7 @@ struct spanhash_top {
 };
 
 static struct spanhash *spanhash_find(struct spanhash_top *top,
-                                     unsigned long hashval)
+                                     unsigned int hashval)
 {
        int sz = 1 << top->alloc_log2;
        int bucket = hashval & (sz - 1);
@@ -99,7 +96,7 @@ static struct spanhash_top *spanhash_rehash(struct spanhash_top *orig)
 }
 
 static struct spanhash_top *add_spanhash(struct spanhash_top *top,
-                                        unsigned long hashval)
+                                        unsigned int hashval, int cnt)
 {
        int bucket, lim;
        struct spanhash *h;
@@ -110,14 +107,14 @@ static struct spanhash_top *add_spanhash(struct spanhash_top *top,
                h = &(top->data[bucket++]);
                if (!h->cnt) {
                        h->hashval = hashval;
-                       h->cnt = 1;
+                       h->cnt = cnt;
                        top->free--;
                        if (top->free < 0)
                                return spanhash_rehash(top);
                        return top;
                }
                if (h->hashval == hashval) {
-                       h->cnt++;
+                       h->cnt += cnt;
                        return top;
                }
                if (lim <= bucket)
@@ -125,11 +122,14 @@ static struct spanhash_top *add_spanhash(struct spanhash_top *top,
        }
 }
 
-static struct spanhash_top *hash_chars(unsigned char *buf, unsigned long sz)
+static struct spanhash_top *hash_chars(struct diff_filespec *one)
 {
-       int i;
-       unsigned long accum1, accum2, hashval;
+       int i, n;
+       unsigned int accum1, accum2, hashval;
        struct spanhash_top *hash;
+       unsigned char *buf = one->data;
+       unsigned int sz = one->size;
+       int is_text = !one->is_binary;
 
        i = INITIAL_HASH_SIZE;
        hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i));
@@ -137,25 +137,32 @@ static struct spanhash_top *hash_chars(unsigned char *buf, unsigned long sz)
        hash->free = INITIAL_FREE(i);
        memset(hash->data, 0, sizeof(struct spanhash) * (1<<i));
 
-       /* an 8-byte shift register made of accum1 and accum2.  New
-        * bytes come at LSB of accum2, and shifted up to accum1
-        */
-       for (i = accum1 = accum2 = 0; i < 7; i++, sz--) {
-               accum1 = (accum1 << 8) | (accum2 >> 24);
-               accum2 = (accum2 << 8) | *buf++;
-       }
+       n = 0;
+       accum1 = accum2 = 0;
        while (sz) {
-               accum1 = (accum1 << 8) | (accum2 >> 24);
-               accum2 = (accum2 << 8) | *buf++;
-               hashval = (accum1 + accum2 * 0x61) % HASHBASE;
-               hash = add_spanhash(hash, hashval);
+               unsigned int c = *buf++;
+               unsigned int old_1 = accum1;
                sz--;
+
+               /* Ignore CR in CRLF sequence if text */
+               if (is_text && c == '\r' && sz && *buf == '\n')
+                       continue;
+
+               accum1 = (accum1 << 7) ^ (accum2 >> 25);
+               accum2 = (accum2 << 7) ^ (old_1 >> 25);
+               accum1 += c;
+               if (++n < 64 && c != '\n')
+                       continue;
+               hashval = (accum1 + accum2 * 0x61) % HASHBASE;
+               hash = add_spanhash(hash, hashval, n);
+               n = 0;
+               accum1 = accum2 = 0;
        }
        return hash;
 }
 
-int diffcore_count_changes(void *src, unsigned long src_size,
-                          void *dst, unsigned long dst_size,
+int diffcore_count_changes(struct diff_filespec *src,
+                          struct diff_filespec *dst,
                           void **src_count_p,
                           void **dst_count_p,
                           unsigned long delta_limit,
@@ -166,21 +173,18 @@ int diffcore_count_changes(void *src, unsigned long src_size,
        struct spanhash_top *src_count, *dst_count;
        unsigned long sc, la;
 
-       if (src_size < 8 || dst_size < 8)
-               return -1;
-
        src_count = dst_count = NULL;
        if (src_count_p)
                src_count = *src_count_p;
        if (!src_count) {
-               src_count = hash_chars(src, src_size);
+               src_count = hash_chars(src);
                if (src_count_p)
                        *src_count_p = src_count;
        }
        if (dst_count_p)
                dst_count = *dst_count_p;
        if (!dst_count) {
-               dst_count = hash_chars(dst, dst_size);
+               dst_count = hash_chars(dst);
                if (dst_count_p)
                        *dst_count_p = dst_count;
        }