index: Preallocate the entries vector with size hint
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
diff --git a/src/index.c b/src/index.c
index 04acd4f..03873f4 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1556,11 +1556,19 @@ int git_index__fill(git_index *index, const git_vector *source_entries)
assert(index);
+ if (!source_entries->length)
+ return 0;
+
if (git_mutex_lock(&index->lock) < 0) {
giterr_set(GITERR_OS, "Unable to acquire index lock");
return -1;
}
+ if (git_vector_size_hint(&index->entries, source_entries->length) < 0) {
+ git_mutex_unlock(&index->lock);
+ return -1;
+ }
+
git_vector_foreach(source_entries, i, source_entry) {
git_index_entry *entry = NULL;
diff --git a/src/vector.c b/src/vector.c
index 93d09bb..a81d463 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -40,6 +40,13 @@ GIT_INLINE(int) resize_vector(git_vector *v, size_t new_size)
return 0;
}
+int git_vector_size_hint(git_vector *v, size_t size_hint)
+{
+ if (v->_alloc_size >= size_hint)
+ return 0;
+ return resize_vector(v, size_hint);
+}
+
int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp)
{
size_t bytes;
diff --git a/src/vector.h b/src/vector.h
index aac46c4..b7500de 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -32,6 +32,7 @@ void git_vector_free_deep(git_vector *v); /* free each entry and self */
void git_vector_clear(git_vector *v);
int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp);
void git_vector_swap(git_vector *a, git_vector *b);
+int git_vector_size_hint(git_vector *v, size_t size_hint);
void **git_vector_detach(size_t *size, size_t *asize, git_vector *v);