Skip to content

Commit 039f1a7

Browse files
derrickstoleedscho
authored andcommitted
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.6: thin pack with --path-walk 0.01 0.01 +0.0% 5313.7: thin pack size with --path-walk 475 475 +0.0% 5313.12: big pack with --path-walk 1.99 1.87 -6.0% 5313.13: big pack size with --path-walk 14.4M 14.3M -0.4% 5313.18: repack with --path-walk 98.14 41.46 -57.8% 5313.19: repack size with --path-walk 197.2M 197.3M +0.0% Signed-off-by: Derrick Stolee <[email protected]>
1 parent f7e4812 commit 039f1a7

File tree

2 files changed

+167
-6
lines changed

2 files changed

+167
-6
lines changed

builtin/pack-objects.c

Lines changed: 163 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3041,6 +3041,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
30413041
struct thread_params {
30423042
pthread_t thread;
30433043
struct object_entry **list;
3044+
struct packing_region *regions;
30443045
unsigned list_size;
30453046
unsigned remaining;
30463047
int window;
@@ -3342,7 +3343,8 @@ static void find_deltas_by_region(struct object_entry *list,
33423343
progress_nr = regions[nr - 1].start + regions[nr - 1].nr;
33433344

33443345
if (progress)
3345-
progress_state = start_progress(_("Compressing objects by path"),
3346+
progress_state = start_progress(the_repository,
3347+
_("Compressing objects by path"),
33463348
progress_nr);
33473349

33483350
while (nr--)
@@ -3354,6 +3356,164 @@ static void find_deltas_by_region(struct object_entry *list,
33543356
stop_progress(&progress_state);
33553357
}
33563358

3359+
static void *threaded_find_deltas_by_path(void *arg)
3360+
{
3361+
struct thread_params *me = arg;
3362+
3363+
progress_lock();
3364+
while (me->remaining) {
3365+
while (me->remaining) {
3366+
progress_unlock();
3367+
find_deltas_for_region(to_pack.objects,
3368+
me->regions,
3369+
me->processed);
3370+
progress_lock();
3371+
me->remaining--;
3372+
me->regions++;
3373+
}
3374+
3375+
me->working = 0;
3376+
pthread_cond_signal(&progress_cond);
3377+
progress_unlock();
3378+
3379+
/*
3380+
* We must not set ->data_ready before we wait on the
3381+
* condition because the main thread may have set it to 1
3382+
* before we get here. In order to be sure that new
3383+
* work is available if we see 1 in ->data_ready, it
3384+
* was initialized to 0 before this thread was spawned
3385+
* and we reset it to 0 right away.
3386+
*/
3387+
pthread_mutex_lock(&me->mutex);
3388+
while (!me->data_ready)
3389+
pthread_cond_wait(&me->cond, &me->mutex);
3390+
me->data_ready = 0;
3391+
pthread_mutex_unlock(&me->mutex);
3392+
3393+
progress_lock();
3394+
}
3395+
progress_unlock();
3396+
/* leave ->working 1 so that this doesn't get more work assigned */
3397+
return NULL;
3398+
}
3399+
3400+
static void ll_find_deltas_by_region(struct object_entry *list,
3401+
struct packing_region *regions,
3402+
uint32_t start, uint32_t nr)
3403+
{
3404+
struct thread_params *p;
3405+
int i, ret, active_threads = 0;
3406+
unsigned int processed = 0;
3407+
uint32_t progress_nr;
3408+
init_threaded_search();
3409+
3410+
if (!nr)
3411+
return;
3412+
3413+
progress_nr = regions[nr - 1].start + regions[nr - 1].nr;
3414+
if (delta_search_threads <= 1) {
3415+
find_deltas_by_region(list, regions, start, nr);
3416+
cleanup_threaded_search();
3417+
return;
3418+
}
3419+
3420+
if (progress > pack_to_stdout)
3421+
fprintf_ln(stderr, _("Path-based delta compression using up to %d threads"),
3422+
delta_search_threads);
3423+
CALLOC_ARRAY(p, delta_search_threads);
3424+
3425+
if (progress)
3426+
progress_state = start_progress(the_repository,
3427+
_("Compressing objects by path"),
3428+
progress_nr);
3429+
/* Partition the work amongst work threads. */
3430+
for (i = 0; i < delta_search_threads; i++) {
3431+
unsigned sub_size = nr / (delta_search_threads - i);
3432+
3433+
p[i].window = window;
3434+
p[i].depth = depth;
3435+
p[i].processed = &processed;
3436+
p[i].working = 1;
3437+
p[i].data_ready = 0;
3438+
3439+
p[i].regions = regions;
3440+
p[i].list_size = sub_size;
3441+
p[i].remaining = sub_size;
3442+
3443+
regions += sub_size;
3444+
nr -= sub_size;
3445+
}
3446+
3447+
/* Start work threads. */
3448+
for (i = 0; i < delta_search_threads; i++) {
3449+
if (!p[i].list_size)
3450+
continue;
3451+
pthread_mutex_init(&p[i].mutex, NULL);
3452+
pthread_cond_init(&p[i].cond, NULL);
3453+
ret = pthread_create(&p[i].thread, NULL,
3454+
threaded_find_deltas_by_path, &p[i]);
3455+
if (ret)
3456+
die(_("unable to create thread: %s"), strerror(ret));
3457+
active_threads++;
3458+
}
3459+
3460+
/*
3461+
* Now let's wait for work completion. Each time a thread is done
3462+
* with its work, we steal half of the remaining work from the
3463+
* thread with the largest number of unprocessed objects and give
3464+
* it to that newly idle thread. This ensure good load balancing
3465+
* until the remaining object list segments are simply too short
3466+
* to be worth splitting anymore.
3467+
*/
3468+
while (active_threads) {
3469+
struct thread_params *target = NULL;
3470+
struct thread_params *victim = NULL;
3471+
unsigned sub_size = 0;
3472+
3473+
progress_lock();
3474+
for (;;) {
3475+
for (i = 0; !target && i < delta_search_threads; i++)
3476+
if (!p[i].working)
3477+
target = &p[i];
3478+
if (target)
3479+
break;
3480+
pthread_cond_wait(&progress_cond, &progress_mutex);
3481+
}
3482+
3483+
for (i = 0; i < delta_search_threads; i++)
3484+
if (p[i].remaining > 2*window &&
3485+
(!victim || victim->remaining < p[i].remaining))
3486+
victim = &p[i];
3487+
if (victim) {
3488+
sub_size = victim->remaining / 2;
3489+
target->regions = victim->regions + victim->remaining - sub_size;
3490+
victim->list_size -= sub_size;
3491+
victim->remaining -= sub_size;
3492+
}
3493+
target->list_size = sub_size;
3494+
target->remaining = sub_size;
3495+
target->working = 1;
3496+
progress_unlock();
3497+
3498+
pthread_mutex_lock(&target->mutex);
3499+
target->data_ready = 1;
3500+
pthread_cond_signal(&target->cond);
3501+
pthread_mutex_unlock(&target->mutex);
3502+
3503+
if (!sub_size) {
3504+
pthread_join(target->thread, NULL);
3505+
pthread_cond_destroy(&target->cond);
3506+
pthread_mutex_destroy(&target->mutex);
3507+
active_threads--;
3508+
}
3509+
}
3510+
cleanup_threaded_search();
3511+
free(p);
3512+
3513+
display_progress(progress_state, progress_nr);
3514+
stop_progress(&progress_state);
3515+
}
3516+
33573517
static void prepare_pack(int window, int depth)
33583518
{
33593519
struct object_entry **delta_list;
@@ -3379,8 +3539,8 @@ static void prepare_pack(int window, int depth)
33793539
return;
33803540

33813541
if (path_walk)
3382-
find_deltas_by_region(to_pack.objects, to_pack.regions,
3383-
0, to_pack.nr_regions);
3542+
ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3543+
0, to_pack.nr_regions);
33843544

33853545
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
33863546
nr_deltas = n = 0;

t/perf/p5313-pack-objects.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,24 @@ test_perf 'thin pack with --path-walk' '
7272
'
7373

7474
test_size 'thin pack size with --path-walk' '
75-
wc -c <out
75+
test_file_size out
7676
'
7777

7878
test_perf 'big pack with --path-walk' '
7979
git pack-objects --stdout --revs --sparse --path-walk <in-big >out
8080
'
8181

8282
test_size 'big pack size with --path-walk' '
83-
wc -c <out
83+
test_file_size out
8484
'
8585

8686
test_perf 'repack with --path-walk' '
8787
git repack -adf --path-walk
8888
'
8989

9090
test_size 'repack size with --path-walk' '
91-
wc -c <.git/objects/pack/pack-*.pack
91+
pack=$(ls .git/objects/pack/pack-*.pack) &&
92+
test_file_size "$pack"
9293
'
9394

9495
test_done

0 commit comments

Comments
 (0)