Skip to content

Commit 56f3cfc

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 2b548d3 commit 56f3cfc

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
@@ -2925,6 +2925,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
29252925
struct thread_params {
29262926
pthread_t thread;
29272927
struct object_entry **list;
2928+
struct packing_region *regions;
29282929
unsigned list_size;
29292930
unsigned remaining;
29302931
int window;
@@ -3238,6 +3239,163 @@ static void find_deltas_by_region(struct object_entry *list,
32383239
stop_progress(&progress_state);
32393240
}
32403241

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

32653423
if (path_walk)
3266-
find_deltas_by_region(to_pack.objects, to_pack.regions,
3267-
0, to_pack.nr_regions);
3424+
ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3425+
0, to_pack.nr_regions);
32683426

32693427
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
32703428
nr_deltas = n = 0;

t/perf/p5313-pack-objects.sh

+4-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test_perf 'thin pack with --path-walk' '
4141
'
4242

4343
test_size 'thin pack size with --path-walk' '
44-
wc -c <out
44+
test_file_size out
4545
'
4646

4747
test_perf 'big pack' '
@@ -65,7 +65,7 @@ test_perf 'big pack with --path-walk' '
6565
'
6666

6767
test_size 'big pack size with --path-walk' '
68-
wc -c <out
68+
test_file_size out
6969
'
7070

7171
test_perf 'repack' '
@@ -91,7 +91,8 @@ test_perf 'repack with --path-walk' '
9191
'
9292

9393
test_size 'repack size with --path-walk' '
94-
wc -c <.git/objects/pack/pack-*.pack
94+
pack=$(ls .git/objects/pack/pack-*.pack) &&
95+
test_file_size "$pack"
9596
'
9697

9798
test_done

0 commit comments

Comments
 (0)