Skip to content

Commit b0f103e

Browse files
derrickstoleeGit for Windows Build Agent
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 1efba53 commit b0f103e

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
@@ -3044,6 +3044,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
30443044
struct thread_params {
30453045
pthread_t thread;
30463046
struct object_entry **list;
3047+
struct packing_region *regions;
30473048
unsigned list_size;
30483049
unsigned remaining;
30493050
int window;
@@ -3345,7 +3346,8 @@ static void find_deltas_by_region(struct object_entry *list,
33453346
progress_nr = regions[nr - 1].start + regions[nr - 1].nr;
33463347

33473348
if (progress)
3348-
progress_state = start_progress(_("Compressing objects by path"),
3349+
progress_state = start_progress(the_repository,
3350+
_("Compressing objects by path"),
33493351
progress_nr);
33503352

33513353
while (nr--)
@@ -3357,6 +3359,164 @@ static void find_deltas_by_region(struct object_entry *list,
33573359
stop_progress(&progress_state);
33583360
}
33593361

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

33843544
if (path_walk)
3385-
find_deltas_by_region(to_pack.objects, to_pack.regions,
3386-
0, to_pack.nr_regions);
3545+
ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3546+
0, to_pack.nr_regions);
33873547

33883548
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
33893549
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)