autoconf: Check for ll hh j z t size specifiers introduced by C99
[gitweb.git] / pack-objects.c
index 614e87bc8cb92998226362f98ff2a87a8e6bbdc8..861c7f08ff1b68eee141442411b4706a1bcc2518 100644 (file)
@@ -28,7 +28,7 @@ struct object_entry {
        struct object_entry *delta;     /* delta base object */
        struct packed_git *in_pack;     /* already in pack */
        unsigned int in_pack_offset;
-       struct object_entry *delta_child; /* delitified objects who bases me */
+       struct object_entry *delta_child; /* deltified objects who bases me */
        struct object_entry *delta_sibling; /* other deltified objects who
                                             * uses the same base as me
                                             */
@@ -39,7 +39,7 @@ struct object_entry {
 };
 
 /*
- * Objects we are going to pack are colected in objects array (dynamically
+ * Objects we are going to pack are collected in objects array (dynamically
  * expanded).  nr_objects & nr_alloc controls this array.  They are stored
  * in the order we see -- typically rev-list --objects order that gives us
  * nice "minimum seek" order.
@@ -63,6 +63,7 @@ static const char *base_name;
 static unsigned char pack_file_sha1[20];
 static int progress = 1;
 static volatile sig_atomic_t progress_update = 0;
+static int window = 10;
 
 /*
  * The object names in objects array are hashed with this hashtable,
@@ -156,7 +157,7 @@ static void prepare_pack_revindex(struct pack_revindex *rix)
 
        rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1));
        for (i = 0; i < num_ent; i++) {
-               unsigned int hl = *((unsigned int *)(index + 24 * i));
+               unsigned int hl = *((unsigned int *)((char *) index + 24*i));
                rix->revindex[i] = ntohl(hl);
        }
        /* This knows the pack format -- the 20-byte trailer
@@ -300,7 +301,7 @@ static unsigned long write_object(struct sha1file *f,
                use_packed_git(p);
 
                datalen = find_packed_object_size(p, entry->in_pack_offset);
-               buf = p->pack_base + entry->in_pack_offset;
+               buf = (char *) p->pack_base + entry->in_pack_offset;
                sha1write(f, buf, datalen);
                unuse_packed_git(p);
                hdrlen = 0; /* not really */
@@ -463,48 +464,21 @@ static void rehash_objects(void)
        }
 }
 
-struct name_path {
-       struct name_path *up;
-       const char *elem;
-       int len;
-};
-
-#define DIRBITS 12
-
-static unsigned name_hash(struct name_path *path, const char *name)
+static unsigned name_hash(const char *name)
 {
-       struct name_path *p = path;
-       const char *n = name + strlen(name);
-       unsigned hash = 0, name_hash = 0, name_done = 0;
-
-       if (n != name && n[-1] == '\n')
-               n--;
-       while (name <= --n) {
-               unsigned char c = *n;
-               if (c == '/' && !name_done) {
-                       name_hash = hash;
-                       name_done = 1;
-                       hash = 0;
-               }
-               hash = hash * 11 + c;
-       }
-       if (!name_done) {
-               name_hash = hash;
-               hash = 0;
-       }
-       for (p = path; p; p = p->up) {
-               hash = hash * 11 + '/';
-               n = p->elem + p->len;
-               while (p->elem <= --n) {
-                       unsigned char c = *n;
-                       hash = hash * 11 + c;
-               }
-       }
+       unsigned char c;
+       unsigned hash = 0;
+
        /*
-        * Make sure "Makefile" and "t/Makefile" are hashed separately
-        * but close enough.
+        * This effectively just creates a sortable number from the
+        * last sixteen non-whitespace characters. Last characters
+        * count "most", so things that end in ".c" sort together.
         */
-       hash = (name_hash<<DIRBITS) | (hash & ((1U<<DIRBITS )-1));
+       while ((c = *name++) != 0) {
+               if (isspace(c))
+                       continue;
+               hash = (hash >> 2) + (c << 24);
+       }
        return hash;
 }
 
@@ -686,48 +660,39 @@ static int name_cmp_len(const char *name)
 }
 
 static void add_pbase_object(struct tree_desc *tree,
-                            struct name_path *up,
                             const char *name,
-                            int cmplen)
+                            int cmplen,
+                            const char *fullname)
 {
-       while (tree->size) {
-               const unsigned char *sha1;
-               const char *entry_name;
-               int entry_len;
-               unsigned mode;
+       struct name_entry entry;
+
+       while (tree_entry(tree,&entry)) {
                unsigned long size;
                char type[20];
 
-               sha1 = tree_entry_extract(tree, &entry_name, &mode);
-               update_tree_entry(tree);
-               entry_len = strlen(entry_name);
-               if (entry_len != cmplen ||
-                   memcmp(entry_name, name, cmplen) ||
-                   !has_sha1_file(sha1) ||
-                   sha1_object_info(sha1, type, &size))
+               if (entry.pathlen != cmplen ||
+                   memcmp(entry.path, name, cmplen) ||
+                   !has_sha1_file(entry.sha1) ||
+                   sha1_object_info(entry.sha1, type, &size))
                        continue;
                if (name[cmplen] != '/') {
-                       unsigned hash = name_hash(up, name);
-                       add_object_entry(sha1, hash, 1);
+                       unsigned hash = name_hash(fullname);
+                       add_object_entry(entry.sha1, hash, 1);
                        return;
                }
                if (!strcmp(type, tree_type)) {
                        struct tree_desc sub;
-                       struct name_path me;
                        struct pbase_tree_cache *tree;
                        const char *down = name+cmplen+1;
                        int downlen = name_cmp_len(down);
 
-                       tree = pbase_tree_get(sha1);
+                       tree = pbase_tree_get(entry.sha1);
                        if (!tree)
                                return;
                        sub.buf = tree->tree_data;
                        sub.size = tree->tree_size;
 
-                       me.up = up;
-                       me.elem = entry_name;
-                       me.len = entry_len;
-                       add_pbase_object(&sub, &me, down, downlen);
+                       add_pbase_object(&sub, down, downlen, fullname);
                        pbase_tree_put(tree);
                }
        }
@@ -783,14 +748,14 @@ static void add_preferred_base_object(char *name, unsigned hash)
 
        for (it = pbase_tree; it; it = it->next) {
                if (cmplen == 0) {
-                       hash = name_hash(NULL, "");
+                       hash = name_hash("");
                        add_object_entry(it->pcache.sha1, hash, 1);
                }
                else {
                        struct tree_desc tree;
                        tree.buf = it->pcache.tree_data;
                        tree.size = it->pcache.tree_size;
-                       add_pbase_object(&tree, NULL, name, cmplen);
+                       add_pbase_object(&tree, name, cmplen, name);
                }
        }
 }
@@ -994,6 +959,7 @@ static int type_size_sort(const struct object_entry *a, const struct object_entr
 struct unpacked {
        struct object_entry *entry;
        void *data;
+       struct delta_index *index;
 };
 
 /*
@@ -1004,64 +970,87 @@ struct unpacked {
  * more importantly, the bigger file is likely the more recent
  * one.
  */
-static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
+static int try_delta(struct unpacked *trg, struct unpacked *src,
+                    unsigned max_depth)
 {
-       struct object_entry *cur_entry = cur->entry;
-       struct object_entry *old_entry = old->entry;
-       unsigned long size, oldsize, delta_size, sizediff;
-       long max_size;
+       struct object_entry *trg_entry = trg->entry;
+       struct object_entry *src_entry = src->entry;
+       unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
+       char type[10];
        void *delta_buf;
 
        /* Don't bother doing diffs between different types */
-       if (cur_entry->type != old_entry->type)
+       if (trg_entry->type != src_entry->type)
                return -1;
 
        /* We do not compute delta to *create* objects we are not
         * going to pack.
         */
-       if (cur_entry->preferred_base)
+       if (trg_entry->preferred_base)
                return -1;
 
-       /* If the current object is at pack edge, take the depth the
+       /*
+        * We do not bother to try a delta that we discarded
+        * on an earlier try, but only when reusing delta data.
+        */
+       if (!no_reuse_delta && trg_entry->in_pack &&
+           trg_entry->in_pack == src_entry->in_pack)
+               return 0;
+
+       /*
+        * If the current object is at pack edge, take the depth the
         * objects that depend on the current object into account --
         * otherwise they would become too deep.
         */
-       if (cur_entry->delta_child) {
-               if (max_depth <= cur_entry->delta_limit)
+       if (trg_entry->delta_child) {
+               if (max_depth <= trg_entry->delta_limit)
                        return 0;
-               max_depth -= cur_entry->delta_limit;
+               max_depth -= trg_entry->delta_limit;
        }
-
-       size = cur_entry->size;
-       oldsize = old_entry->size;
-       sizediff = oldsize > size ? oldsize - size : size - oldsize;
-
-       if (size < 50)
-               return -1;
-       if (old_entry->depth >= max_depth)
+       if (src_entry->depth >= max_depth)
                return 0;
 
-       /*
-        * NOTE!
-        *
-        * We always delta from the bigger to the smaller, since that's
-        * more space-efficient (deletes don't have to say _what_ they
-        * delete).
-        */
-       max_size = size / 2 - 20;
-       if (cur_entry->delta)
-               max_size = cur_entry->delta_size-1;
+       /* Now some size filtering heuristics. */
+       trg_size = trg_entry->size;
+       max_size = trg_size/2 - 20;
+       max_size = max_size * (max_depth - src_entry->depth) / max_depth;
+       if (max_size == 0)
+               return 0;
+       if (trg_entry->delta && trg_entry->delta_size <= max_size)
+               max_size = trg_entry->delta_size-1;
+       src_size = src_entry->size;
+       sizediff = src_size < trg_size ? trg_size - src_size : 0;
        if (sizediff >= max_size)
                return 0;
-       delta_buf = diff_delta(old->data, oldsize,
-                              cur->data, size, &delta_size, max_size);
+
+       /* Load data if not already done */
+       if (!trg->data) {
+               trg->data = read_sha1_file(trg_entry->sha1, type, &sz);
+               if (sz != trg_size)
+                       die("object %s inconsistent object length (%lu vs %lu)",
+                           sha1_to_hex(trg_entry->sha1), sz, trg_size);
+       }
+       if (!src->data) {
+               src->data = read_sha1_file(src_entry->sha1, type, &sz);
+               if (sz != src_size)
+                       die("object %s inconsistent object length (%lu vs %lu)",
+                           sha1_to_hex(src_entry->sha1), sz, src_size);
+       }
+       if (!src->index) {
+               src->index = create_delta_index(src->data, src_size);
+               if (!src->index)
+                       die("out of memory");
+       }
+
+       delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
        if (!delta_buf)
                return 0;
-       cur_entry->delta = old_entry;
-       cur_entry->delta_size = delta_size;
-       cur_entry->depth = old_entry->depth + 1;
+
+       trg_entry->delta = src_entry;
+       trg_entry->delta_size = delta_size;
+       trg_entry->depth = src_entry->depth + 1;
        free(delta_buf);
-       return 0;
+       return 1;
 }
 
 static void progress_interval(int signum)
@@ -1086,8 +1075,6 @@ static void find_deltas(struct object_entry **list, int window, int depth)
        while (--i >= 0) {
                struct object_entry *entry = list[i];
                struct unpacked *n = array + idx;
-               unsigned long size;
-               char type[10];
                int j;
 
                if (!entry->preferred_base)
@@ -1109,11 +1096,13 @@ static void find_deltas(struct object_entry **list, int window, int depth)
                         */
                        continue;
 
+               if (entry->size < 50)
+                       continue;
+               free_delta_index(n->index);
+               n->index = NULL;
                free(n->data);
+               n->data = NULL;
                n->entry = entry;
-               n->data = read_sha1_file(entry->sha1, type, &size);
-               if (size != entry->size)
-                       die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
 
                j = window;
                while (--j > 0) {
@@ -1127,15 +1116,13 @@ static void find_deltas(struct object_entry **list, int window, int depth)
                        if (try_delta(n, m, depth) < 0)
                                break;
                }
-#if 0
                /* if we made n a delta, and if n is already at max
                 * depth, leaving it in the window is pointless.  we
                 * should evict it first.
-                * ... in theory only; somehow this makes things worse.
                 */
                if (entry->delta && depth <= entry->depth)
                        continue;
-#endif
+
                idx++;
                if (idx >= window)
                        idx = 0;
@@ -1144,8 +1131,10 @@ static void find_deltas(struct object_entry **list, int window, int depth)
        if (progress)
                fputc('\n', stderr);
 
-       for (i = 0; i < window; ++i)
+       for (i = 0; i < window; ++i) {
+               free_delta_index(array[i].index);
                free(array[i].data);
+       }
        free(array);
 }
 
@@ -1228,17 +1217,28 @@ static void setup_progress_signal(void)
        setitimer(ITIMER_REAL, &v, NULL);
 }
 
+static int git_pack_config(const char *k, const char *v)
+{
+       if(!strcmp(k, "pack.window")) {
+               window = git_config_int(k, v);
+               return 0;
+       }
+       return git_default_config(k, v);
+}
+
 int main(int argc, char **argv)
 {
        SHA_CTX ctx;
        char line[40 + 1 + PATH_MAX + 2];
-       int window = 10, depth = 10, pack_to_stdout = 0;
+       int depth = 10, pack_to_stdout = 0;
        struct object_entry **list;
        int num_preferred_base = 0;
        int i;
 
        setup_git_directory();
+       git_config(git_pack_config);
 
+       progress = isatty(2);
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
 
@@ -1251,6 +1251,10 @@ int main(int argc, char **argv)
                                local = 1;
                                continue;
                        }
+                       if (!strcmp("--progress", arg)) {
+                               progress = 1;
+                               continue;
+                       }
                        if (!strcmp("--incremental", arg)) {
                                incremental = 1;
                                continue;
@@ -1269,6 +1273,10 @@ int main(int argc, char **argv)
                                        usage(pack_usage);
                                continue;
                        }
+                       if (!strcmp("--progress", arg)) {
+                               progress = 1;
+                               continue;
+                       }
                        if (!strcmp("-q", arg)) {
                                progress = 0;
                                continue;
@@ -1323,7 +1331,7 @@ int main(int argc, char **argv)
                }
                if (get_sha1_hex(line, sha1))
                        die("expected sha1, got garbage:\n %s", line);
-               hash = name_hash(NULL, line+41);
+               hash = name_hash(line+41);
                add_preferred_base_object(line+41, hash);
                add_object_entry(sha1, hash, 0);
        }