Skip to content

Commit 2b762f3

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.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 212b8ed commit 2b762f3

File tree

2 files changed

+164
-5
lines changed

2 files changed

+164
-5
lines changed

builtin/pack-objects.c

+160-2
Original file line numberDiff line numberDiff line change
@@ -2929,6 +2929,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
29292929
struct thread_params {
29302930
pthread_t thread;
29312931
struct object_entry **list;
2932+
struct packing_region *regions;
29322933
unsigned list_size;
29332934
unsigned remaining;
29342935
int window;
@@ -3242,6 +3243,163 @@ static void find_deltas_by_region(struct object_entry *list,
32423243
stop_progress(&progress_state);
32433244
}
32443245

3246+
static void *threaded_find_deltas_by_path(void *arg)
3247+
{
3248+
struct thread_params *me = arg;
3249+
3250+
progress_lock();
3251+
while (me->remaining) {
3252+
while (me->remaining) {
3253+
progress_unlock();
3254+
find_deltas_for_region(to_pack.objects,
3255+
me->regions,
3256+
me->processed);
3257+
progress_lock();
3258+
me->remaining--;
3259+
me->regions++;
3260+
}
3261+
3262+
me->working = 0;
3263+
pthread_cond_signal(&progress_cond);
3264+
progress_unlock();
3265+
3266+
/*
3267+
* We must not set ->data_ready before we wait on the
3268+
* condition because the main thread may have set it to 1
3269+
* before we get here. In order to be sure that new
3270+
* work is available if we see 1 in ->data_ready, it
3271+
* was initialized to 0 before this thread was spawned
3272+
* and we reset it to 0 right away.
3273+
*/
3274+
pthread_mutex_lock(&me->mutex);
3275+
while (!me->data_ready)
3276+
pthread_cond_wait(&me->cond, &me->mutex);
3277+
me->data_ready = 0;
3278+
pthread_mutex_unlock(&me->mutex);
3279+
3280+
progress_lock();
3281+
}
3282+
progress_unlock();
3283+
/* leave ->working 1 so that this doesn't get more work assigned */
3284+
return NULL;
3285+
}
3286+
3287+
static void ll_find_deltas_by_region(struct object_entry *list,
3288+
struct packing_region *regions,
3289+
uint32_t start, uint32_t nr)
3290+
{
3291+
struct thread_params *p;
3292+
int i, ret, active_threads = 0;
3293+
unsigned int processed = 0;
3294+
uint32_t progress_nr;
3295+
init_threaded_search();
3296+
3297+
if (!nr)
3298+
return;
3299+
3300+
progress_nr = regions[nr - 1].start + regions[nr - 1].nr;
3301+
if (delta_search_threads <= 1) {
3302+
find_deltas_by_region(list, regions, start, nr);
3303+
cleanup_threaded_search();
3304+
return;
3305+
}
3306+
3307+
if (progress > pack_to_stdout)
3308+
fprintf_ln(stderr, _("Path-based delta compression using up to %d threads"),
3309+
delta_search_threads);
3310+
CALLOC_ARRAY(p, delta_search_threads);
3311+
3312+
if (progress)
3313+
progress_state = start_progress(_("Compressing objects by path"),
3314+
progress_nr);
3315+
/* Partition the work amongst work threads. */
3316+
for (i = 0; i < delta_search_threads; i++) {
3317+
unsigned sub_size = nr / (delta_search_threads - i);
3318+
3319+
p[i].window = window;
3320+
p[i].depth = depth;
3321+
p[i].processed = &processed;
3322+
p[i].working = 1;
3323+
p[i].data_ready = 0;
3324+
3325+
p[i].regions = regions;
3326+
p[i].list_size = sub_size;
3327+
p[i].remaining = sub_size;
3328+
3329+
regions += sub_size;
3330+
nr -= sub_size;
3331+
}
3332+
3333+
/* Start work threads. */
3334+
for (i = 0; i < delta_search_threads; i++) {
3335+
if (!p[i].list_size)
3336+
continue;
3337+
pthread_mutex_init(&p[i].mutex, NULL);
3338+
pthread_cond_init(&p[i].cond, NULL);
3339+
ret = pthread_create(&p[i].thread, NULL,
3340+
threaded_find_deltas_by_path, &p[i]);
3341+
if (ret)
3342+
die(_("unable to create thread: %s"), strerror(ret));
3343+
active_threads++;
3344+
}
3345+
3346+
/*
3347+
* Now let's wait for work completion. Each time a thread is done
3348+
* with its work, we steal half of the remaining work from the
3349+
* thread with the largest number of unprocessed objects and give
3350+
* it to that newly idle thread. This ensure good load balancing
3351+
* until the remaining object list segments are simply too short
3352+
* to be worth splitting anymore.
3353+
*/
3354+
while (active_threads) {
3355+
struct thread_params *target = NULL;
3356+
struct thread_params *victim = NULL;
3357+
unsigned sub_size = 0;
3358+
3359+
progress_lock();
3360+
for (;;) {
3361+
for (i = 0; !target && i < delta_search_threads; i++)
3362+
if (!p[i].working)
3363+
target = &p[i];
3364+
if (target)
3365+
break;
3366+
pthread_cond_wait(&progress_cond, &progress_mutex);
3367+
}
3368+
3369+
for (i = 0; i < delta_search_threads; i++)
3370+
if (p[i].remaining > 2*window &&
3371+
(!victim || victim->remaining < p[i].remaining))
3372+
victim = &p[i];
3373+
if (victim) {
3374+
sub_size = victim->remaining / 2;
3375+
target->regions = victim->regions + victim->remaining - sub_size;
3376+
victim->list_size -= sub_size;
3377+
victim->remaining -= sub_size;
3378+
}
3379+
target->list_size = sub_size;
3380+
target->remaining = sub_size;
3381+
target->working = 1;
3382+
progress_unlock();
3383+
3384+
pthread_mutex_lock(&target->mutex);
3385+
target->data_ready = 1;
3386+
pthread_cond_signal(&target->cond);
3387+
pthread_mutex_unlock(&target->mutex);
3388+
3389+
if (!sub_size) {
3390+
pthread_join(target->thread, NULL);
3391+
pthread_cond_destroy(&target->cond);
3392+
pthread_mutex_destroy(&target->mutex);
3393+
active_threads--;
3394+
}
3395+
}
3396+
cleanup_threaded_search();
3397+
free(p);
3398+
3399+
display_progress(progress_state, progress_nr);
3400+
stop_progress(&progress_state);
3401+
}
3402+
32453403
static void prepare_pack(int window, int depth)
32463404
{
32473405
struct object_entry **delta_list;
@@ -3267,8 +3425,8 @@ static void prepare_pack(int window, int depth)
32673425
return;
32683426

32693427
if (path_walk)
3270-
find_deltas_by_region(to_pack.objects, to_pack.regions,
3271-
0, to_pack.nr_regions);
3428+
ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3429+
0, to_pack.nr_regions);
32723430

32733431
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
32743432
nr_deltas = n = 0;

t/perf/p5313-pack-objects.sh

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test_perf 'thin pack with --path-walk' '
3333
'
3434

3535
test_size 'thin pack size with --path-walk' '
36-
wc -c <out
36+
test_file_size out
3737
'
3838

3939
test_perf 'big pack' '
@@ -49,7 +49,7 @@ test_perf 'big pack with --path-walk' '
4949
'
5050

5151
test_size 'big pack size with --path-walk' '
52-
wc -c <out
52+
test_file_size out
5353
'
5454

5555
test_perf 'repack' '
@@ -66,7 +66,8 @@ test_perf 'repack with --path-walk' '
6666
'
6767

6868
test_size 'repack size with --path-walk' '
69-
wc -c <.git/objects/pack/pack-*.pack
69+
pack=$(ls .git/objects/pack/pack-*.pack) &&
70+
test_file_size "$pack"
7071
'
7172

7273
test_done

0 commit comments

Comments
 (0)