clean up add_object_entry()
This function used to call locate_object_entry_hash() _twice_ per added object while only once should suffice. Let's reorganize that code a bit. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nicolas Pitre committed
Apr 10, 2007 at 22:54 UTC
29b734e4788143603adf8046174219bac67794e0
1 file changed
+25
-27
builtin-pack-objects.c
+25
-27
@@ -781,12 +781,19 @@ static unsigned name_hash(const char *name)
781
782
static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)
783
{
784
- uint32_t idx = nr_objects;
784
struct object_entry *entry;
786
- struct packed_git *p;
785
+ struct packed_git *p, *found_pack = NULL;
786
off_t found_offset = 0;
788
- struct packed_git *found_pack = NULL;
789
- int ix, status = 0;
787
+ int ix;
788
+
789
+ ix = nr_objects ? locate_object_entry_hash(sha1) : -1;
790
+ if (ix >= 0) {
791
+ if (exclude) {
792
+ entry = objects + object_ix[ix] - 1;
793
+ entry->preferred_base = 1;
794
+ }
795
+ return 0;
796
+ }
797
798
if (!exclude) {
799
for (p = packed_git; p; p = p->next) {
@@ -803,43 +810,34 @@ static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclud
810
}
811
}
812
}
806
- if ((entry = locate_object_entry(sha1)) != NULL)
807
- goto already_added;
813
809
- if (idx >= nr_alloc) {
810
- nr_alloc = (idx + 1024) * 3 / 2;
814
+ if (nr_objects >= nr_alloc) {
815
+ nr_alloc = (nr_alloc + 1024) * 3 / 2;
816
objects = xrealloc(objects, nr_alloc * sizeof(*entry));
817
}
813
- entry = objects + idx;
814
- nr_objects = idx + 1;
818
+
819
+ entry = objects + nr_objects++;
820
memset(entry, 0, sizeof(*entry));
821
hashcpy(entry->sha1, sha1);
822
entry->hash = hash;
823
+ if (exclude)
824
+ entry->preferred_base = 1;
825
+ if (found_pack) {
826
+ entry->in_pack = found_pack;
827
+ entry->in_pack_offset = found_offset;
828
+ }
829
830
if (object_ix_hashsz * 3 <= nr_objects * 4)
831
rehash_objects();
821
- else {
822
- ix = locate_object_entry_hash(entry->sha1);
823
- if (0 <= ix)
824
- die("internal error in object hashing.");
825
- object_ix[-1 - ix] = idx + 1;
826
- }
827
- status = 1;
832
+ else
833
+ object_ix[-1 - ix] = nr_objects;
834
829
- already_added:
835
if (progress_update) {
836
fprintf(stderr, "Counting objects...%u\r", nr_objects);
837
progress_update = 0;
838
}
834
- if (exclude)
835
- entry->preferred_base = 1;
836
- else {
837
- if (found_pack) {
838
- entry->in_pack = found_pack;
839
- entry->in_pack_offset = found_offset;
840
- }
841
- }
842
- return status;
839
+
840
+ return 1;
841
}
842
843
struct pbase_tree_cache {