packed_ref_store: get rid of the `ref_cache` entirely

Now that everything has been changed to read what it needs directly out of the `packed-refs` file, `packed_ref_store` doesn't need to maintain a `ref_cache` at all. So get rid of it. First of all, this will save a lot of memory and lots of little allocations. Instead of needing to store complicated parsed data structures in memory, we just mmap the file (potentially sharing memory with other processes) and parse only what we need. Moreover, since the mmapped access to the file reads only the parts of the file that it needs, this might save reading all of the data from disk at all (at least if the file starts out sorted). Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 25, 2017 at 10:00 UTC 9dd389f3d8d220d764beaca7237ac33de5de81c7
1 file changed +2 -27
refs/packed-backend.c
+2 -27
@@ -45,8 +45,6 @@ struct packed_ref_cache {
45 */
46 struct packed_ref_store *refs;
47
48 - struct ref_cache *cache;
49 -
48 /* Is the `packed-refs` file currently mmapped? */
49 int mmapped;
50
@@ -148,7 +146,6 @@ static void release_packed_ref_buffer(struct packed_ref_cache *packed_refs)
146 static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
147 {
148 if (!--packed_refs->referrers) {
151 - free_ref_cache(packed_refs->cache);
149 stat_validity_clear(&packed_refs->validity);
150 release_packed_ref_buffer(packed_refs);
151 free(packed_refs);
@@ -719,15 +716,10 @@ static const char *find_reference_location(struct packed_ref_cache *cache,
716 static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
717 {
718 struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
722 - struct ref_dir *dir;
723 - struct ref_iterator *iter;
719 int sorted = 0;
725 - int ok;
720
721 packed_refs->refs = refs;
722 acquire_packed_ref_cache(packed_refs);
729 - packed_refs->cache = create_ref_cache(NULL, NULL);
730 - packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
723 packed_refs->peeled = PEELED_NONE;
724
725 if (!load_contents(packed_refs))
@@ -800,23 +792,6 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
792 packed_refs->eof = buf_copy + size;
793 }
794
803 - dir = get_ref_dir(packed_refs->cache->root);
804 - iter = mmapped_ref_iterator_begin(
805 - packed_refs,
806 - packed_refs->buf + packed_refs->header_len,
807 - packed_refs->eof);
808 - while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
809 - struct ref_entry *entry =
810 - create_ref_entry(iter->refname, iter->oid, iter->flags);
811 -
812 - if ((iter->flags & REF_KNOWS_PEELED))
813 - ref_iterator_peel(iter, &entry->u.value.peeled);
814 - add_ref_entry(dir, entry);
815 - }
816 -
817 - if (ok != ITER_DONE)
818 - die("error reading packed-refs file %s", refs->path);
819 -
795 return packed_refs;
796 }
797
@@ -975,8 +950,8 @@ static struct ref_iterator *packed_ref_iterator_begin(
950 else
951 start = packed_refs->buf + packed_refs->header_len;
952
978 - iter->iter0 = mmapped_ref_iterator_begin(
979 - packed_refs, start, packed_refs->eof);
953 + iter->iter0 = mmapped_ref_iterator_begin(packed_refs,
954 + start, packed_refs->eof);
955
956 iter->flags = flags;
957