Commit d7d46cfbfd19983ee71b53607abcaf55872be83a

Vicent Marti 2015-12-16T17:00:25

index: Preallocate the entries vector with size hint

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);