Skip to content

Commit 9ac3f0e

Browse files
pcloudsgitster
authored andcommitted
pack-objects: fix performance issues on packing large deltas
Let's start with some background about oe_delta_size() and oe_set_delta_size(). If you already know, skip the next paragraph. These two are added in 0aca34e (pack-objects: shrink delta_size field in struct object_entry - 2018-04-14) to help reduce 'struct object_entry' size. The delta size field in this struct is reduced to only contain max 1MB. So if any new delta is produced and larger than 1MB, it's dropped because we can't really save such a large size anywhere. Fallback is provided in case existing packfiles already have large deltas, then we can retrieve it from the pack. While this should help small machines repacking large repos without large deltas (i.e. less memory pressure), dropping large deltas during the delta selection process could end up with worse pack files. And if existing packfiles already have >1MB delta and pack-objects is instructed to not reuse deltas, all of them will be dropped on the floor, and the resulting pack would be definitely bigger. There is also a regression in terms of CPU/IO if we have large on-disk deltas because fallback code needs to parse the pack every time the delta size is needed and just access to the mmap'd pack data is enough for extra page faults when memory is under pressure. Both of these issues were reported on the mailing list. Here's some numbers for comparison. Version Pack (MB) MaxRSS(kB) Time (s) ------- --------- ---------- -------- 2.17.0 5498 43513628 2494.85 2.18.0 10531 40449596 4168.94 This patch provides a better fallback that is - cheaper in terms of cpu and io because we won't have to read existing pack files as much - better in terms of pack size because the pack heuristics is back to 2.17.0 time, we do not drop large deltas at all If we encounter any delta (on-disk or created during try_delta phase) that is larger than the 1MB limit, we stop using delta_size_ field for this because it can't contain such size anyway. A new array of delta size is dynamically allocated and can hold all the deltas that 2.17.0 can. This array only contains delta sizes that delta_size_ can't contain. With this, we do not have to drop deltas in try_delta() anymore. Of course the downside is we use slightly more memory, even compared to 2.17.0. But since this is considered an uncommon case, a bit more memory consumption should not be a problem. Delta size limit is also raised from 1MB to 16MB to better cover common case and avoid that extra memory consumption (99.999% deltas in this reported repo are under 12MB; Jeff noted binary artifacts topped out at about 3MB in some other private repos). Other fields are shuffled around to keep this struct packed tight. We don't use more memory in common case even with this limit update. A note about thread synchronization. Since this code can be run in parallel during delta searching phase, we need a mutex. The realloc part in packlist_alloc() is not protected because it only happens during the object counting phase, which is always single-threaded. Access to e->delta_size_ (and by extension pack->delta_size[e - pack->objects]) is unprotected as before, the thread scheduler in pack-objects must make sure "e" is never updated by two different threads. The area under the new lock is as small as possible, avoiding locking at all in common case, since lock contention with high thread count could be expensive (most blobs are small enough that delta compute time is short and we end up taking the lock very often). The previous attempt to always hold a lock in oe_delta_size() and oe_set_delta_size() increases execution time by 33% when repacking linux.git with with 40 threads. Reported-by: Elijah Newren <[email protected]> Helped-by: Elijah Newren <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f6a5576 commit 9ac3f0e

File tree

5 files changed

+58
-15
lines changed

5 files changed

+58
-15
lines changed

builtin/pack-objects.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,10 +2008,6 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
20082008
delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
20092009
if (!delta_buf)
20102010
return 0;
2011-
if (delta_size >= (1U << OE_DELTA_SIZE_BITS)) {
2012-
free(delta_buf);
2013-
return 0;
2014-
}
20152011

20162012
if (DELTA(trg_entry)) {
20172013
/* Prefer only shallower same-sized deltas. */
@@ -2263,6 +2259,7 @@ static void init_threaded_search(void)
22632259
pthread_mutex_init(&cache_mutex, NULL);
22642260
pthread_mutex_init(&progress_mutex, NULL);
22652261
pthread_cond_init(&progress_cond, NULL);
2262+
pthread_mutex_init(&to_pack.lock, NULL);
22662263
old_try_to_free_routine = set_try_to_free_routine(try_to_free_from_threads);
22672264
}
22682265

ci/run-build-and-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ then
1414
export GIT_TEST_SPLIT_INDEX=yes
1515
export GIT_TEST_FULL_IN_PACK_ARRAY=true
1616
export GIT_TEST_OE_SIZE=10
17+
export GIT_TEST_OE_DELTA_SIZE=5
1718
make --quiet test
1819
fi
1920

pack-objects.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ void prepare_packing_data(struct packing_data *pdata)
146146

147147
pdata->oe_size_limit = git_env_ulong("GIT_TEST_OE_SIZE",
148148
1U << OE_SIZE_BITS);
149+
pdata->oe_delta_size_limit = git_env_ulong("GIT_TEST_OE_DELTA_SIZE",
150+
1UL << OE_DELTA_SIZE_BITS);
149151
}
150152

151153
struct object_entry *packlist_alloc(struct packing_data *pdata,
@@ -160,6 +162,8 @@ struct object_entry *packlist_alloc(struct packing_data *pdata,
160162

161163
if (!pdata->in_pack_by_idx)
162164
REALLOC_ARRAY(pdata->in_pack, pdata->nr_alloc);
165+
if (pdata->delta_size)
166+
REALLOC_ARRAY(pdata->delta_size, pdata->nr_alloc);
163167
}
164168

165169
new_entry = pdata->objects + pdata->nr_objects++;

pack-objects.h

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define PACK_OBJECTS_H
33

44
#include "object-store.h"
5+
#include "thread-utils.h"
56

67
#define OE_DFS_STATE_BITS 2
78
#define OE_DEPTH_BITS 12
@@ -12,7 +13,7 @@
1213
* above this limit. Don't lower it too much.
1314
*/
1415
#define OE_SIZE_BITS 31
15-
#define OE_DELTA_SIZE_BITS 20
16+
#define OE_DELTA_SIZE_BITS 23
1617

1718
/*
1819
* State flags for depth-first search used for analyzing delta cycles.
@@ -92,11 +93,12 @@ struct object_entry {
9293
*/
9394
unsigned delta_size_:OE_DELTA_SIZE_BITS; /* delta data size (uncompressed) */
9495
unsigned delta_size_valid:1;
96+
unsigned char in_pack_header_size;
9597
unsigned in_pack_idx:OE_IN_PACK_BITS; /* already in pack */
9698
unsigned z_delta_size:OE_Z_DELTA_BITS;
9799
unsigned type_valid:1;
98-
unsigned type_:TYPE_BITS;
99100
unsigned no_try_delta:1;
101+
unsigned type_:TYPE_BITS;
100102
unsigned in_pack_type:TYPE_BITS; /* could be delta */
101103
unsigned preferred_base:1; /*
102104
* we do not pack this, but is available
@@ -106,17 +108,16 @@ struct object_entry {
106108
unsigned tagged:1; /* near the very tip of refs */
107109
unsigned filled:1; /* assigned write-order */
108110
unsigned dfs_state:OE_DFS_STATE_BITS;
109-
unsigned char in_pack_header_size;
110111
unsigned depth:OE_DEPTH_BITS;
111112

112113
/*
113114
* pahole results on 64-bit linux (gcc and clang)
114115
*
115-
* size: 80, bit_padding: 20 bits, holes: 8 bits
116+
* size: 80, bit_padding: 9 bits
116117
*
117118
* and on 32-bit (gcc)
118119
*
119-
* size: 76, bit_padding: 20 bits, holes: 8 bits
120+
* size: 76, bit_padding: 9 bits
120121
*/
121122
};
122123

@@ -128,6 +129,7 @@ struct packing_data {
128129
uint32_t index_size;
129130

130131
unsigned int *in_pack_pos;
132+
unsigned long *delta_size;
131133

132134
/*
133135
* Only one of these can be non-NULL and they have different
@@ -138,10 +140,29 @@ struct packing_data {
138140
struct packed_git **in_pack_by_idx;
139141
struct packed_git **in_pack;
140142

143+
#ifndef NO_PTHREADS
144+
pthread_mutex_t lock;
145+
#endif
146+
141147
uintmax_t oe_size_limit;
148+
uintmax_t oe_delta_size_limit;
142149
};
143150

144151
void prepare_packing_data(struct packing_data *pdata);
152+
153+
static inline void packing_data_lock(struct packing_data *pdata)
154+
{
155+
#ifndef NO_PTHREADS
156+
pthread_mutex_lock(&pdata->lock);
157+
#endif
158+
}
159+
static inline void packing_data_unlock(struct packing_data *pdata)
160+
{
161+
#ifndef NO_PTHREADS
162+
pthread_mutex_unlock(&pdata->lock);
163+
#endif
164+
}
165+
145166
struct object_entry *packlist_alloc(struct packing_data *pdata,
146167
const unsigned char *sha1,
147168
uint32_t index_pos);
@@ -330,18 +351,34 @@ static inline unsigned long oe_delta_size(struct packing_data *pack,
330351
{
331352
if (e->delta_size_valid)
332353
return e->delta_size_;
333-
return oe_size(pack, e);
354+
355+
/*
356+
* pack->detla_size[] can't be NULL because oe_set_delta_size()
357+
* must have been called when a new delta is saved with
358+
* oe_set_delta().
359+
* If oe_delta() returns NULL (i.e. default state, which means
360+
* delta_size_valid is also false), then the caller must never
361+
* call oe_delta_size().
362+
*/
363+
return pack->delta_size[e - pack->objects];
334364
}
335365

336366
static inline void oe_set_delta_size(struct packing_data *pack,
337367
struct object_entry *e,
338368
unsigned long size)
339369
{
340-
e->delta_size_ = size;
341-
e->delta_size_valid = e->delta_size_ == size;
342-
if (!e->delta_size_valid && size != oe_size(pack, e))
343-
BUG("this can only happen in check_object() "
344-
"where delta size is the same as entry size");
370+
if (size < pack->oe_delta_size_limit) {
371+
e->delta_size_ = size;
372+
e->delta_size_valid = 1;
373+
} else {
374+
packing_data_lock(pack);
375+
if (!pack->delta_size)
376+
ALLOC_ARRAY(pack->delta_size, pack->nr_alloc);
377+
packing_data_unlock(pack);
378+
379+
pack->delta_size[e - pack->objects] = size;
380+
e->delta_size_valid = 0;
381+
}
345382
}
346383

347384
#endif

t/README

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ packs on demand. This normally only happens when the object size is
315315
over 2GB. This variable forces the code path on any object larger than
316316
<n> bytes.
317317

318+
GIT_TEST_OE_DELTA_SIZE=<n> exercises the uncomon pack-objects code
319+
path where deltas larger than this limit require extra memory
320+
allocation for bookkeeping.
321+
318322
Naming Tests
319323
------------
320324

0 commit comments

Comments
 (0)