Merge branch 'nd/unpack-entry-optim-in-pack-objects'
authorJonathan Nieder <jrnieder@gmail.com>
Wed, 25 Sep 2013 06:29:55 +0000 (23:29 -0700)
committerJonathan Nieder <jrnieder@gmail.com>
Wed, 25 Sep 2013 06:29:55 +0000 (23:29 -0700)
* nd/unpack-entry-optim-in-pack-objects:
pack-objects: no crc check when the cached version is used

1  2 
sha1_file.c
diff --combined sha1_file.c
index f1b649742f0d363c252c47f66f6c7083647f48e6,6f5a1e0717be8cdd9beb87c4010216104c6ff931..f80bbe467437a4ffc161a4cdd09f97cf5436d4f9
@@@ -614,7 -614,7 +614,7 @@@ static void scan_windows(struct packed_
        }
  }
  
 -static int unuse_one_window(struct packed_git *current, int keep_fd)
 +static int unuse_one_window(struct packed_git *current)
  {
        struct packed_git *p, *lru_p = NULL;
        struct pack_window *lru_w = NULL, *lru_l = NULL;
                pack_mapped -= lru_w->len;
                if (lru_l)
                        lru_l->next = lru_w->next;
 -              else {
 +              else
                        lru_p->windows = lru_w->next;
 -                      if (!lru_p->windows && lru_p->pack_fd != -1
 -                              && lru_p->pack_fd != keep_fd) {
 -                              close(lru_p->pack_fd);
 -                              pack_open_fds--;
 -                              lru_p->pack_fd = -1;
 -                      }
 -              }
                free(lru_w);
                pack_open_windows--;
                return 1;
        return 0;
  }
  
 -void release_pack_memory(size_t need, int fd)
 +void release_pack_memory(size_t need)
  {
        size_t cur = pack_mapped;
 -      while (need >= (cur - pack_mapped) && unuse_one_window(NULL, fd))
 +      while (need >= (cur - pack_mapped) && unuse_one_window(NULL))
                ; /* nothing */
  }
  
@@@ -651,7 -658,7 +651,7 @@@ void *xmmap(void *start, size_t length
        if (ret == MAP_FAILED) {
                if (!length)
                        return NULL;
 -              release_pack_memory(length, fd);
 +              release_pack_memory(length);
                ret = mmap(start, length, prot, flags, fd, offset);
                if (ret == MAP_FAILED)
                        die_errno("Out of memory? mmap failed");
@@@ -675,83 -682,6 +675,83 @@@ void close_pack_windows(struct packed_g
        }
  }
  
 +/*
 + * The LRU pack is the one with the oldest MRU window, preferring packs
 + * with no used windows, or the oldest mtime if it has no windows allocated.
 + */
 +static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struct pack_window **mru_w, int *accept_windows_inuse)
 +{
 +      struct pack_window *w, *this_mru_w;
 +      int has_windows_inuse = 0;
 +
 +      /*
 +       * Reject this pack if it has windows and the previously selected
 +       * one does not.  If this pack does not have windows, reject
 +       * it if the pack file is newer than the previously selected one.
 +       */
 +      if (*lru_p && !*mru_w && (p->windows || p->mtime > (*lru_p)->mtime))
 +              return;
 +
 +      for (w = this_mru_w = p->windows; w; w = w->next) {
 +              /*
 +               * Reject this pack if any of its windows are in use,
 +               * but the previously selected pack did not have any
 +               * inuse windows.  Otherwise, record that this pack
 +               * has windows in use.
 +               */
 +              if (w->inuse_cnt) {
 +                      if (*accept_windows_inuse)
 +                              has_windows_inuse = 1;
 +                      else
 +                              return;
 +              }
 +
 +              if (w->last_used > this_mru_w->last_used)
 +                      this_mru_w = w;
 +
 +              /*
 +               * Reject this pack if it has windows that have been
 +               * used more recently than the previously selected pack.
 +               * If the previously selected pack had windows inuse and
 +               * we have not encountered a window in this pack that is
 +               * inuse, skip this check since we prefer a pack with no
 +               * inuse windows to one that has inuse windows.
 +               */
 +              if (*mru_w && *accept_windows_inuse == has_windows_inuse &&
 +                  this_mru_w->last_used > (*mru_w)->last_used)
 +                      return;
 +      }
 +
 +      /*
 +       * Select this pack.
 +       */
 +      *mru_w = this_mru_w;
 +      *lru_p = p;
 +      *accept_windows_inuse = has_windows_inuse;
 +}
 +
 +static int close_one_pack(void)
 +{
 +      struct packed_git *p, *lru_p = NULL;
 +      struct pack_window *mru_w = NULL;
 +      int accept_windows_inuse = 1;
 +
 +      for (p = packed_git; p; p = p->next) {
 +              if (p->pack_fd == -1)
 +                      continue;
 +              find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
 +      }
 +
 +      if (lru_p) {
 +              close(lru_p->pack_fd);
 +              pack_open_fds--;
 +              lru_p->pack_fd = -1;
 +              return 1;
 +      }
 +
 +      return 0;
 +}
 +
  void unuse_pack(struct pack_window **w_cursor)
  {
        struct pack_window *w = *w_cursor;
@@@ -847,7 -777,7 +847,7 @@@ static int open_packed_git_1(struct pac
                        pack_max_fds = 1;
        }
  
 -      while (pack_max_fds <= pack_open_fds && unuse_one_window(NULL, -1))
 +      while (pack_max_fds <= pack_open_fds && close_one_pack())
                ; /* nothing */
  
        p->pack_fd = git_open_noatime(p->pack_name);
@@@ -963,7 -893,7 +963,7 @@@ unsigned char *use_pack(struct packed_g
                        win->len = (size_t)len;
                        pack_mapped += win->len;
                        while (packed_git_limit < pack_mapped
 -                              && unuse_one_window(p, p->pack_fd))
 +                              && unuse_one_window(p))
                                ; /* nothing */
                        win->base = xmmap(NULL, win->len,
                                PROT_READ, MAP_PRIVATE,
@@@ -1009,7 -939,7 +1009,7 @@@ static struct packed_git *alloc_packed_
  
  static void try_to_free_pack_memory(size_t size)
  {
 -      release_pack_memory(size, -1);
 +      release_pack_memory(size);
  }
  
  struct packed_git *add_packed_git(const char *path, int path_len, int local)
@@@ -2126,6 -2056,16 +2126,16 @@@ void *unpack_entry(struct packed_git *p
                int i;
                struct delta_base_cache_entry *ent;
  
+               ent = get_delta_base_cache_entry(p, curpos);
+               if (eq_delta_base_cache_entry(ent, p, curpos)) {
+                       type = ent->type;
+                       data = ent->data;
+                       size = ent->size;
+                       clear_delta_base_cache_entry(ent);
+                       base_from_cache = 1;
+                       break;
+               }
                if (do_check_packed_object_crc && p->index_version > 1) {
                        struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
                        unsigned long len = revidx[1].offset - obj_offset;
                        }
                }
  
-               ent = get_delta_base_cache_entry(p, curpos);
-               if (eq_delta_base_cache_entry(ent, p, curpos)) {
-                       type = ent->type;
-                       data = ent->data;
-                       size = ent->size;
-                       clear_delta_base_cache_entry(ent);
-                       base_from_cache = 1;
-                       break;
-               }
                type = unpack_object_header(p, &w_curs, &curpos, &size);
                if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA)
                        break;
@@@ -2995,10 -2925,7 +2995,10 @@@ int has_sha1_file(const unsigned char *
  
        if (find_pack_entry(sha1, &e))
                return 1;
 -      return has_loose_object(sha1);
 +      if (has_loose_object(sha1))
 +              return 1;
 +      reprepare_packed_git();
 +      return find_pack_entry(sha1, &e);
  }
  
  static void check_tree(const void *buf, size_t size)