Skip to content

Commit ce94083

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 2e3a1d0 commit ce94083

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
@@ -2959,6 +2959,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
29592959
struct thread_params {
29602960
pthread_t thread;
29612961
struct object_entry **list;
2962+
struct packing_region *regions;
29622963
unsigned list_size;
29632964
unsigned remaining;
29642965
int window;
@@ -3272,6 +3273,163 @@ static void find_deltas_by_region(struct object_entry *list,
32723273
stop_progress(&progress_state);
32733274
}
32743275

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

32993457
if (path_walk)
3300-
find_deltas_by_region(to_pack.objects, to_pack.regions,
3301-
0, to_pack.nr_regions);
3458+
ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3459+
0, to_pack.nr_regions);
33023460

33033461
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
33043462
nr_deltas = n = 0;

t/perf/p5313-pack-objects.sh

+4-3
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)