Skip to content

Commit 0a26f35

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 7bdfbc1 commit 0a26f35

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
@@ -2958,6 +2958,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
29582958
struct thread_params {
29592959
pthread_t thread;
29602960
struct object_entry **list;
2961+
struct packing_region *regions;
29612962
unsigned list_size;
29622963
unsigned remaining;
29632964
int window;
@@ -3271,6 +3272,163 @@ static void find_deltas_by_region(struct object_entry *list,
32713272
stop_progress(&progress_state);
32723273
}
32733274

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

32983456
if (path_walk)
3299-
find_deltas_by_region(to_pack.objects, to_pack.regions,
3300-
0, to_pack.nr_regions);
3457+
ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3458+
0, to_pack.nr_regions);
33013459

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