Skip to content

Commit 785dfb3

Browse files
committed
pack-objects: thread the path-based compression
Adapting the implementation of ll_find_deltas(), create a threaded version of the --path-walk compression step in 'git pack-objects'. This involves adding a 'regions' member to the thread_params struct, allowing each thread to own a section of paths. We can simplify the way jobs are split because there is no value in extending the batch based on name-hash the way sections of the object entry array are attempted to be grouped. We re-use the 'list_size' and 'remaining' items for the purpose of borrowing work in progress from other "victim" threads when a thread has finished its batch of work more quickly. Using the Git repository as a test repo, the p5313 performance test shows that the resulting size of the repo is the same, but the threaded implementation gives gains of varying degrees depending on the number of objects being packed. (This was tested on a 16-core machine.) Test HEAD~1 HEAD ----------------------------------------------------------------- 5313.2: thin pack 0.00 0.00 = 5313.3: thin pack size 589 589 +0.0% 5313.4: thin pack with --path-walk 0.00 0.00 = 5313.5: thin pack size with --path-walk 589 589 +0.0% 5313.6: big pack 2.84 2.80 -1.4% 5313.7: big pack size 14.0M 14.1M +0.3% 5313.8: big pack with --path-walk 5.46 3.77 -31.0% 5313.9: big pack size with --path-walk 13.2M 13.2M -0.0% 5313.10: repack 22.11 21.50 -2.8% 5313.11: repack size 126.4M 126.2M -0.2% 5313.12: repack with --path-walk 66.89 26.41 -60.5% 5313.13: repack size with --path-walk 109.6M 109.6M +0.0% This 60% reduction in 'git repack --path-walk' time is typical across all repos I used for testing. What is interesting is to compare when the overall time improves enough to outperform the standard case. These time improvements correlate with repositories with data shapes that significantly improve their data size as well. For example, the microsoft/fluentui repo has a 439M to 122M size reduction, and the repack time is now 36.6 seconds with --path-walk compared to 95+ seconds without it: Test HEAD~! HEAD ----------------------------------------------------------------- 5313.2: thin pack 0.41 0.42 +2.4% 5313.3: thin pack size 1.2M 1.2M +0.0% 5313.4: thin pack with --path-walk 0.08 0.05 -37.5% 5313.5: thin pack size with --path-walk 18.4K 18.4K +0.0% 5313.6: big pack 4.47 4.53 +1.3% 5313.7: big pack size 19.6M 19.7M +0.3% 5313.8: big pack with --path-walk 6.76 3.51 -48.1% 5313.9: big pack size with --path-walk 16.5M 16.4M -0.2% 5313.10: repack 96.87 99.05 +2.3% 5313.11: repack size 439.5M 439.0M -0.1% 5313.12: repack with --path-walk 95.68 36.55 -61.8% 5313.13: repack size with --path-walk 122.6M 122.6M +0.0% In a more extreme example, an internal repository that has a similar name-hash collision issue to microsoft/fluentui reduces its size from 6.4G to 805M with the --path-walk option. This also reduces the repacking time from 2,138 seconds to 478 seconds. Test HEAD~1 HEAD ------------------------------------------------------------------ 5313.10: repack 2138.22 2138.19 -0.0% 5313.11: repack size 6.4G 6.4G -0.0% 5313.12: repack with --path-walk 1351.46 477.91 -64.6% 5313.13: repack size with --path-walk 804.1M 804.1M -0.0% Finally, the Linux kernel repository is a good test for this repacking time change, even though the space savings is more reasonable: Test HEAD~1 HEAD ---------------------------------------------------------------- 5313.10: repack 734.26 735.11 +0.1% 5313.11: repack size 2.5G 2.5G -0.0% 5313.12: repack with --path-walk 1457.23 598.17 -59.0% 5313.13: repack size with --path-walk 2.2G 2.2G +0.0% Signed-off-by: Derrick Stolee <[email protected]>
1 parent e234fd7 commit 785dfb3

File tree

1 file changed

+160
-2
lines changed

1 file changed

+160
-2
lines changed

builtin/pack-objects.c

+160-2
Original file line numberDiff line numberDiff line change
@@ -2964,6 +2964,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
29642964
struct thread_params {
29652965
pthread_t thread;
29662966
struct object_entry **list;
2967+
struct packing_region *regions;
29672968
unsigned list_size;
29682969
unsigned remaining;
29692970
int window;
@@ -3277,6 +3278,163 @@ static void find_deltas_by_region(struct object_entry *list,
32773278
stop_progress(&progress_state);
32783279
}
32793280

3281+
static void *threaded_find_deltas_by_path(void *arg)
3282+
{
3283+
struct thread_params *me = arg;
3284+
3285+
progress_lock();
3286+
while (me->remaining) {
3287+
while (me->remaining) {
3288+
progress_unlock();
3289+
find_deltas_for_region(to_pack.objects,
3290+
me->regions,
3291+
me->processed);
3292+
progress_lock();
3293+
me->remaining--;
3294+
me->regions++;
3295+
}
3296+
3297+
me->working = 0;
3298+
pthread_cond_signal(&progress_cond);
3299+
progress_unlock();
3300+
3301+
/*
3302+
* We must not set ->data_ready before we wait on the
3303+
* condition because the main thread may have set it to 1
3304+
* before we get here. In order to be sure that new
3305+
* work is available if we see 1 in ->data_ready, it
3306+
* was initialized to 0 before this thread was spawned
3307+
* and we reset it to 0 right away.
3308+
*/
3309+
pthread_mutex_lock(&me->mutex);
3310+
while (!me->data_ready)
3311+
pthread_cond_wait(&me->cond, &me->mutex);
3312+
me->data_ready = 0;
3313+
pthread_mutex_unlock(&me->mutex);
3314+
3315+
progress_lock();
3316+
}
3317+
progress_unlock();
3318+
/* leave ->working 1 so that this doesn't get more work assigned */
3319+
return NULL;
3320+
}
3321+
3322+
static void ll_find_deltas_by_region(struct object_entry *list,
3323+
struct packing_region *regions,
3324+
uint32_t start, uint32_t nr)
3325+
{
3326+
struct thread_params *p;
3327+
int i, ret, active_threads = 0;
3328+
unsigned int processed = 0;
3329+
uint32_t progress_nr;
3330+
init_threaded_search();
3331+
3332+
if (!nr)
3333+
return;
3334+
3335+
progress_nr = regions[nr - 1].start + regions[nr - 1].nr;
3336+
if (delta_search_threads <= 1) {
3337+
find_deltas_by_region(list, regions, start, nr);
3338+
cleanup_threaded_search();
3339+
return;
3340+
}
3341+
3342+
if (progress > pack_to_stdout)
3343+
fprintf_ln(stderr, _("Path-based delta compression using up to %d threads"),
3344+
delta_search_threads);
3345+
CALLOC_ARRAY(p, delta_search_threads);
3346+
3347+
if (progress)
3348+
progress_state = start_progress(_("Compressing objects by path"),
3349+
progress_nr);
3350+
/* Partition the work amongst work threads. */
3351+
for (i = 0; i < delta_search_threads; i++) {
3352+
unsigned sub_size = nr / (delta_search_threads - i);
3353+
3354+
p[i].window = window;
3355+
p[i].depth = depth;
3356+
p[i].processed = &processed;
3357+
p[i].working = 1;
3358+
p[i].data_ready = 0;
3359+
3360+
p[i].regions = regions;
3361+
p[i].list_size = sub_size;
3362+
p[i].remaining = sub_size;
3363+
3364+
regions += sub_size;
3365+
nr -= sub_size;
3366+
}
3367+
3368+
/* Start work threads. */
3369+
for (i = 0; i < delta_search_threads; i++) {
3370+
if (!p[i].list_size)
3371+
continue;
3372+
pthread_mutex_init(&p[i].mutex, NULL);
3373+
pthread_cond_init(&p[i].cond, NULL);
3374+
ret = pthread_create(&p[i].thread, NULL,
3375+
threaded_find_deltas_by_path, &p[i]);
3376+
if (ret)
3377+
die(_("unable to create thread: %s"), strerror(ret));
3378+
active_threads++;
3379+
}
3380+
3381+
/*
3382+
* Now let's wait for work completion. Each time a thread is done
3383+
* with its work, we steal half of the remaining work from the
3384+
* thread with the largest number of unprocessed objects and give
3385+
* it to that newly idle thread. This ensure good load balancing
3386+
* until the remaining object list segments are simply too short
3387+
* to be worth splitting anymore.
3388+
*/
3389+
while (active_threads) {
3390+
struct thread_params *target = NULL;
3391+
struct thread_params *victim = NULL;
3392+
unsigned sub_size = 0;
3393+
3394+
progress_lock();
3395+
for (;;) {
3396+
for (i = 0; !target && i < delta_search_threads; i++)
3397+
if (!p[i].working)
3398+
target = &p[i];
3399+
if (target)
3400+
break;
3401+
pthread_cond_wait(&progress_cond, &progress_mutex);
3402+
}
3403+
3404+
for (i = 0; i < delta_search_threads; i++)
3405+
if (p[i].remaining > 2*window &&
3406+
(!victim || victim->remaining < p[i].remaining))
3407+
victim = &p[i];
3408+
if (victim) {
3409+
sub_size = victim->remaining / 2;
3410+
target->regions = victim->regions + victim->remaining - sub_size;
3411+
victim->list_size -= sub_size;
3412+
victim->remaining -= sub_size;
3413+
}
3414+
target->list_size = sub_size;
3415+
target->remaining = sub_size;
3416+
target->working = 1;
3417+
progress_unlock();
3418+
3419+
pthread_mutex_lock(&target->mutex);
3420+
target->data_ready = 1;
3421+
pthread_cond_signal(&target->cond);
3422+
pthread_mutex_unlock(&target->mutex);
3423+
3424+
if (!sub_size) {
3425+
pthread_join(target->thread, NULL);
3426+
pthread_cond_destroy(&target->cond);
3427+
pthread_mutex_destroy(&target->mutex);
3428+
active_threads--;
3429+
}
3430+
}
3431+
cleanup_threaded_search();
3432+
free(p);
3433+
3434+
display_progress(progress_state, progress_nr);
3435+
stop_progress(&progress_state);
3436+
}
3437+
32803438
static void prepare_pack(int window, int depth)
32813439
{
32823440
struct object_entry **delta_list;
@@ -3302,8 +3460,8 @@ static void prepare_pack(int window, int depth)
33023460
return;
33033461

33043462
if (path_walk)
3305-
find_deltas_by_region(to_pack.objects, to_pack.regions,
3306-
0, to_pack.nr_regions);
3463+
ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3464+
0, to_pack.nr_regions);
33073465

33083466
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
33093467
nr_deltas = n = 0;

0 commit comments

Comments
 (0)