Skip to content

Commit aa6239c

Browse files
derrickstoleeGit for Windows Build Agent
authored and
Git for Windows Build Agent
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 406a8d4 commit aa6239c

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
@@ -2937,6 +2937,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
29372937
struct thread_params {
29382938
pthread_t thread;
29392939
struct object_entry **list;
2940+
struct packing_region *regions;
29402941
unsigned list_size;
29412942
unsigned remaining;
29422943
int window;
@@ -3250,6 +3251,163 @@ static void find_deltas_by_region(struct object_entry *list,
32503251
stop_progress(&progress_state);
32513252
}
32523253

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

32773435
if (path_walk)
3278-
find_deltas_by_region(to_pack.objects, to_pack.regions,
3279-
0, to_pack.nr_regions);
3436+
ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3437+
0, to_pack.nr_regions);
32803438

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