Skip to content

Commit c288df6

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 b96b46b commit c288df6

File tree

1 file changed

+161
-2
lines changed

1 file changed

+161
-2
lines changed

builtin/pack-objects.c

+161-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;
@@ -3278,6 +3279,164 @@ static void find_deltas_by_region(struct object_entry *list,
32783279
stop_progress(&progress_state);
32793280
}
32803281

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

33053464
if (path_walk)
3306-
find_deltas_by_region(to_pack.objects, to_pack.regions,
3307-
0, to_pack.nr_regions);
3465+
ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3466+
0, to_pack.nr_regions);
33083467

33093468
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
33103469
nr_deltas = n = 0;

0 commit comments

Comments
 (0)