Skip to content

Commit ae73d26

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.20: big pack 2.38 1.99 -16.4% 5313.21: big pack size 16.1M 16.0M -0.2% 5313.24: repack 107.32 45.41 -57.7% 5313.25: repack size 213.3M 213.2M -0.0% (Test output is formatted to better fit in message.) 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 --name-hash-version=1 case. These time improvements correlate with repositories with data shapes that significantly improve their data size as well. The --path-walk feature frequently takes longer than --name-hash-verison=2, trading some extrac computation for some additional compression. The natural place where this additional computation comes from is the two compression passes that --path-walk takes, though the first pass is naturally faster due to the path boundaries avoiding a number of delta compression attempts. For example, the microsoft/fluentui repo has significant size reduction from --name-hash-version=1 to --name-hash-version=2 followed by further improvements with --path-walk. The threaded computation makes --path-walk more competitive in time compared to --name-hash-version=2, though still ~31% more expensive in that metric. Repack Method Pack Size Time ------------------------------------------ Hash v1 439.4M 87.24s Hash v2 161.7M 21.51s Path Walk (Before) 142.5M 81.29s Path Walk (After) 142.5M 28.16s Similar results hold for the Git repository: Repack Method Pack Size Time ------------------------------------------ Hash v1 248.8M 30.44s Hash v2 249.0M 30.15s Path Walk (Before) 213.2M 142.50s Path Walk (After) 213.3M 45.41s ...as well as the nodejs/node repository: Repack Method Pack Size Time ------------------------------------------ Hash v1 739.9M 71.18s Hash v2 764.6M 67.82s Path Walk (Before) 698.1M 208.10s Path Walk (After) 698.0M 75.10s Finally, the Linux kernel repository is a good test for this repacking time change, even though the space savings is more subtle: Repack Method Pack Size Time ------------------------------------------ Hash v1 2.5G 554.41s Hash v2 2.5G 549.62s Path Walk (before) 2.2G 1562.36s Path Walk (before) 2.2G 559.00s Signed-off-by: Derrick Stolee <[email protected]>
1 parent 622439d commit ae73d26

File tree

1 file changed

+161
-2
lines changed

1 file changed

+161
-2
lines changed

builtin/pack-objects.c

Lines changed: 161 additions & 2 deletions
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;
@@ -3281,6 +3282,164 @@ static void find_deltas_by_region(struct object_entry *list,
32813282
stop_progress(&progress_state);
32823283
}
32833284

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

33083467
if (path_walk)
3309-
find_deltas_by_region(to_pack.objects, to_pack.regions,
3310-
0, to_pack.nr_regions);
3468+
ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3469+
0, to_pack.nr_regions);
33113470

33123471
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
33133472
nr_deltas = n = 0;

0 commit comments

Comments
 (0)