Skip to content

Commit 96790ca

Browse files
committed
Merge branch 'jc/pack-order-tweak'
* jc/pack-order-tweak: pack-objects: optimize "recency order" core: log offset pack data accesses happened
2 parents 5d2fc91 + 1b4bb16 commit 96790ca

File tree

5 files changed

+165
-1
lines changed

5 files changed

+165
-1
lines changed

builtin/pack-objects.c

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ struct object_entry {
5151
* objects against.
5252
*/
5353
unsigned char no_try_delta;
54+
unsigned char tagged; /* near the very tip of refs */
55+
unsigned char filled; /* assigned write-order */
5456
};
5557

5658
/*
@@ -96,6 +98,7 @@ static unsigned long window_memory_limit = 0;
9698
*/
9799
static int *object_ix;
98100
static int object_ix_hashsz;
101+
static struct object_entry *locate_object_entry(const unsigned char *sha1);
99102

100103
/*
101104
* stats
@@ -200,6 +203,7 @@ static void copy_pack_data(struct sha1file *f,
200203
}
201204
}
202205

206+
/* Return 0 if we will bust the pack-size limit */
203207
static unsigned long write_object(struct sha1file *f,
204208
struct object_entry *entry,
205209
off_t write_offset)
@@ -434,6 +438,134 @@ static int write_one(struct sha1file *f,
434438
return 1;
435439
}
436440

441+
static int mark_tagged(const char *path, const unsigned char *sha1, int flag,
442+
void *cb_data)
443+
{
444+
unsigned char peeled[20];
445+
struct object_entry *entry = locate_object_entry(sha1);
446+
447+
if (entry)
448+
entry->tagged = 1;
449+
if (!peel_ref(path, peeled)) {
450+
entry = locate_object_entry(peeled);
451+
if (entry)
452+
entry->tagged = 1;
453+
}
454+
return 0;
455+
}
456+
457+
static void add_to_write_order(struct object_entry **wo,
458+
int *endp,
459+
struct object_entry *e)
460+
{
461+
if (e->filled)
462+
return;
463+
wo[(*endp)++] = e;
464+
e->filled = 1;
465+
}
466+
467+
static void add_descendants_to_write_order(struct object_entry **wo,
468+
int *endp,
469+
struct object_entry *e)
470+
{
471+
struct object_entry *child;
472+
473+
for (child = e->delta_child; child; child = child->delta_sibling)
474+
add_to_write_order(wo, endp, child);
475+
for (child = e->delta_child; child; child = child->delta_sibling)
476+
add_descendants_to_write_order(wo, endp, child);
477+
}
478+
479+
static void add_family_to_write_order(struct object_entry **wo,
480+
int *endp,
481+
struct object_entry *e)
482+
{
483+
struct object_entry *root;
484+
485+
for (root = e; root->delta; root = root->delta)
486+
; /* nothing */
487+
add_to_write_order(wo, endp, root);
488+
add_descendants_to_write_order(wo, endp, root);
489+
}
490+
491+
static struct object_entry **compute_write_order(void)
492+
{
493+
int i, wo_end;
494+
495+
struct object_entry **wo = xmalloc(nr_objects * sizeof(*wo));
496+
497+
for (i = 0; i < nr_objects; i++) {
498+
objects[i].tagged = 0;
499+
objects[i].filled = 0;
500+
objects[i].delta_child = NULL;
501+
objects[i].delta_sibling = NULL;
502+
}
503+
504+
/*
505+
* Fully connect delta_child/delta_sibling network.
506+
* Make sure delta_sibling is sorted in the original
507+
* recency order.
508+
*/
509+
for (i = nr_objects - 1; 0 <= i; i--) {
510+
struct object_entry *e = &objects[i];
511+
if (!e->delta)
512+
continue;
513+
/* Mark me as the first child */
514+
e->delta_sibling = e->delta->delta_child;
515+
e->delta->delta_child = e;
516+
}
517+
518+
/*
519+
* Mark objects that are at the tip of tags.
520+
*/
521+
for_each_tag_ref(mark_tagged, NULL);
522+
523+
/*
524+
* Give the commits in the original recency order until
525+
* we see a tagged tip.
526+
*/
527+
for (i = wo_end = 0; i < nr_objects; i++) {
528+
if (objects[i].tagged)
529+
break;
530+
add_to_write_order(wo, &wo_end, &objects[i]);
531+
}
532+
533+
/*
534+
* Then fill all the tagged tips.
535+
*/
536+
for (; i < nr_objects; i++) {
537+
if (objects[i].tagged)
538+
add_to_write_order(wo, &wo_end, &objects[i]);
539+
}
540+
541+
/*
542+
* And then all remaining commits and tags.
543+
*/
544+
for (i = 0; i < nr_objects; i++) {
545+
if (objects[i].type != OBJ_COMMIT &&
546+
objects[i].type != OBJ_TAG)
547+
continue;
548+
add_to_write_order(wo, &wo_end, &objects[i]);
549+
}
550+
551+
/*
552+
* And then all the trees.
553+
*/
554+
for (i = 0; i < nr_objects; i++) {
555+
if (objects[i].type != OBJ_TREE)
556+
continue;
557+
add_to_write_order(wo, &wo_end, &objects[i]);
558+
}
559+
560+
/*
561+
* Finally all the rest in really tight order
562+
*/
563+
for (i = 0; i < nr_objects; i++)
564+
add_family_to_write_order(wo, &wo_end, &objects[i]);
565+
566+
return wo;
567+
}
568+
437569
static void write_pack_file(void)
438570
{
439571
uint32_t i = 0, j;
@@ -442,10 +574,12 @@ static void write_pack_file(void)
442574
struct pack_header hdr;
443575
uint32_t nr_remaining = nr_result;
444576
time_t last_mtime = 0;
577+
struct object_entry **write_order;
445578

446579
if (progress > pack_to_stdout)
447580
progress_state = start_progress("Writing objects", nr_result);
448581
written_list = xmalloc(nr_objects * sizeof(*written_list));
582+
write_order = compute_write_order();
449583

450584
do {
451585
unsigned char sha1[20];
@@ -469,7 +603,8 @@ static void write_pack_file(void)
469603
offset = sizeof(hdr);
470604
nr_written = 0;
471605
for (; i < nr_objects; i++) {
472-
if (!write_one(f, objects + i, &offset))
606+
struct object_entry *e = write_order[i];
607+
if (!write_one(f, e, &offset))
473608
break;
474609
display_progress(progress_state, written);
475610
}
@@ -546,6 +681,7 @@ static void write_pack_file(void)
546681
} while (nr_remaining && i < nr_objects);
547682

548683
free(written_list);
684+
free(write_order);
549685
stop_progress(&progress_state);
550686
if (written != nr_result)
551687
die("wrote %"PRIu32" objects while expecting %"PRIu32,

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,9 @@ extern int parse_sha1_header(const char *hdr, unsigned long *sizep);
773773
/* global flag to enable extra checks when accessing packed objects */
774774
extern int do_check_packed_object_crc;
775775

776+
/* for development: log offset of pack access */
777+
extern const char *log_pack_access;
778+
776779
extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);
777780

778781
extern int move_temp_to_file(const char *tmpfile, const char *filename);

config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,9 @@ static int git_default_core_config(const char *var, const char *value)
576576
return 0;
577577
}
578578

579+
if (!strcmp(var, "core.logpackaccess"))
580+
return git_config_string(&log_pack_access, var, value);
581+
579582
if (!strcmp(var, "core.autocrlf")) {
580583
if (value && !strcasecmp(value, "input")) {
581584
if (core_eol == EOL_CRLF)

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
3636
size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
3737
size_t delta_base_cache_limit = 16 * 1024 * 1024;
3838
unsigned long big_file_threshold = 512 * 1024 * 1024;
39+
const char *log_pack_access;
3940
const char *pager_program;
4041
int pager_use_color = 1;
4142
const char *editor_program;

sha1_file.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,24 @@ static void *unpack_delta_entry(struct packed_git *p,
17971797
return result;
17981798
}
17991799

1800+
static void write_pack_access_log(struct packed_git *p, off_t obj_offset)
1801+
{
1802+
static FILE *log_file;
1803+
1804+
if (!log_file) {
1805+
log_file = fopen(log_pack_access, "w");
1806+
if (!log_file) {
1807+
error("cannot open pack access log '%s' for writing: %s",
1808+
log_pack_access, strerror(errno));
1809+
log_pack_access = NULL;
1810+
return;
1811+
}
1812+
}
1813+
fprintf(log_file, "%s %"PRIuMAX"\n",
1814+
p->pack_name, (uintmax_t)obj_offset);
1815+
fflush(log_file);
1816+
}
1817+
18001818
int do_check_packed_object_crc;
18011819

18021820
void *unpack_entry(struct packed_git *p, off_t obj_offset,
@@ -1806,6 +1824,9 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
18061824
off_t curpos = obj_offset;
18071825
void *data;
18081826

1827+
if (log_pack_access)
1828+
write_pack_access_log(p, obj_offset);
1829+
18091830
if (do_check_packed_object_crc && p->index_version > 1) {
18101831
struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
18111832
unsigned long len = revidx[1].offset - obj_offset;

0 commit comments

Comments
 (0)