Skip to content

Commit 999c28c

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 87e7307 commit 999c28c

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
@@ -3046,6 +3046,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
30463046
struct thread_params {
30473047
pthread_t thread;
30483048
struct object_entry **list;
3049+
struct packing_region *regions;
30493050
unsigned list_size;
30503051
unsigned remaining;
30513052
int window;
@@ -3347,7 +3348,8 @@ static void find_deltas_by_region(struct object_entry *list,
33473348
progress_nr = regions[nr - 1].start + regions[nr - 1].nr;
33483349

33493350
if (progress)
3350-
progress_state = start_progress(_("Compressing objects by path"),
3351+
progress_state = start_progress(the_repository,
3352+
_("Compressing objects by path"),
33513353
progress_nr);
33523354

33533355
while (nr--)
@@ -3359,6 +3361,164 @@ static void find_deltas_by_region(struct object_entry *list,
33593361
stop_progress(&progress_state);
33603362
}
33613363

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

33863546
if (path_walk)
3387-
find_deltas_by_region(to_pack.objects, to_pack.regions,
3388-
0, to_pack.nr_regions);
3547+
ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3548+
0, to_pack.nr_regions);
33893549

33903550
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
33913551
nr_deltas = n = 0;

t/perf/p5313-pack-objects.sh

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

7171
test_size 'thin pack size with --path-walk' '
72-
wc -c <out
72+
test_file_size out
7373
'
7474

7575
test_perf 'big pack with --path-walk' '
7676
git pack-objects --stdout --revs --sparse --path-walk <in-big >out
7777
'
7878

7979
test_size 'big pack size with --path-walk' '
80-
wc -c <out
80+
test_file_size out
8181
'
8282

8383
test_perf 'repack with --path-walk' '
8484
git repack -adf --path-walk
8585
'
8686

8787
test_size 'repack size with --path-walk' '
88-
wc -c <.git/objects/pack/pack-*.pack
88+
pack=$(ls .git/objects/pack/pack-*.pack) &&
89+
test_file_size "$pack"
8990
'
9091

9192
test_done

0 commit comments

Comments
 (0)