Skip to content

Commit c4df926

Browse files
committed
pack-objects: allocate memory more efficiently
The packbuilder code allocates memory in chunks. When it needs to allocate, it tries to add 1024 to the number of objects and multiply by 3/2. However, it actually multiplies by 1 instead, since it performs an integral division in the expression "3 / 2" and only then multiplies by the increased number of objects. The current behavior causes the code to waste massive amounts of time copying memory when it reallocates, causing inserting all non-blob objects in the Linux repository into a new pack to take some indeterminate time greater than 5 minutes instead of 52 seconds. Correct this error by first dividing by two, and only then multiplying by 3. We still check for overflow for the multiplication, which is the only part that can overflow. This appears to be the only place in the code base which has this problem.
1 parent f92d495 commit c4df926

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid,
223223

224224
if (pb->nr_objects >= pb->nr_alloc) {
225225
GIT_ERROR_CHECK_ALLOC_ADD(&newsize, pb->nr_alloc, 1024);
226-
GIT_ERROR_CHECK_ALLOC_MULTIPLY(&newsize, newsize, 3 / 2);
226+
GIT_ERROR_CHECK_ALLOC_MULTIPLY(&newsize, newsize / 2, 3);
227227

228228
if (!git__is_uint32(newsize)) {
229229
git_error_set(GIT_ERROR_NOMEMORY, "packfile too large to fit in memory.");

0 commit comments

Comments
 (0)