diff --git a/Documentation/Makefile b/Documentation/Makefile index 11208591d01c3e..551fa9b30de8f6 100644 --- a/Documentation/Makefile +++ b/Documentation/Makefile @@ -130,6 +130,7 @@ TECH_DOCS += technical/hash-function-transition TECH_DOCS += technical/large-object-promisors TECH_DOCS += technical/long-running-process-protocol TECH_DOCS += technical/multi-pack-index +TECH_DOCS += technical/pack-chunk-hints TECH_DOCS += technical/packfile-uri TECH_DOCS += technical/pack-heuristics TECH_DOCS += technical/paint-down-to-common diff --git a/Documentation/config/pack.adoc b/Documentation/config/pack.adoc index 22384c2d2f0773..c62bf746cab740 100644 --- a/Documentation/config/pack.adoc +++ b/Documentation/config/pack.adoc @@ -14,6 +14,22 @@ pack.windowMemory:: suffixed with "k", "m", or "g". When left unconfigured (or set explicitly to 0), there will be no limit. +pack.pathWalkUseChunkHints:: + When packing with path-walk, use content-defined chunk fingerprints + to look for one additional delta base when the normal same-path + window finds no small delta. Git considers recently evicted + candidates from the same path and validates a hint with the normal + delta matcher. + This can reduce pack size when the window misses a similar version + of a large blob, at the cost of fingerprinting and possibly + rereading a base. It has no effect without path-walk or when + `pack.windowMemory` is nonzero. Like the ordinary search, hints + skip bases in the same pack as an undeltified target unless + delta reuse is disabled (for example, with `git repack -f`). + Defaults to false and does not change the pack format. See + link:technical/pack-chunk-hints.html[Chunk hints for path-walk packing] + for how Git chooses and checks a hinted base. + pack.compression:: An integer -1..9, indicating the compression level for objects in a pack file. -1 is the zlib default. 0 means no diff --git a/Documentation/technical/meson.build b/Documentation/technical/meson.build index c439a7a072d395..e6a88391f561d4 100644 --- a/Documentation/technical/meson.build +++ b/Documentation/technical/meson.build @@ -17,6 +17,7 @@ articles = [ 'large-object-promisors.adoc', 'long-running-process-protocol.adoc', 'multi-pack-index.adoc', + 'pack-chunk-hints.adoc', 'packfile-uri.adoc', 'pack-heuristics.adoc', 'paint-down-to-common.adoc', diff --git a/Documentation/technical/pack-chunk-hints.adoc b/Documentation/technical/pack-chunk-hints.adoc new file mode 100644 index 00000000000000..27055937d0e952 --- /dev/null +++ b/Documentation/technical/pack-chunk-hints.adoc @@ -0,0 +1,70 @@ += Chunk hints for path-walk packing + +The delta search in linkgit:git-pack-objects[1] considers bases in a +sliding window. A similar version of a large blob can leave that window +before Git reaches the target. Widening the window keeps more unpacked +objects and delta indexes in memory and compares more pairs. Chunk hints +let Git try one base outside the window without keeping its data or +delta index. + +== Collecting fingerprints + +Path-walk groups objects by path. Hints are local to one such group and +are not used by the later global search. The order within a group is +the delta search order, not the order of commits in the history. + +When an eligible blob leaves the window, Git fingerprints it only if +the ordinary search already read its bytes. Hints cover blobs from +4 MiB to 64 MiB and do not save preferred bases. Git retains one +fingerprint set per evicted blob, up to 16 blobs for the current path. +That limits how many eligible bases Git remembers, not how many chunks +each set can hold. Git evicts the least recent set as it adds another and +releases them all when it finishes that path. + +Git uses a rolling Gear hash to choose content-defined chunk boundaries. +A shift in the file need not shift every later boundary, as it would +with fixed-size chunks. The chunker looks for cuts between 16 KiB and +256 KiB, with a looser cut criterion starting at 64 KiB. The last +chunk may be shorter. + +Each entry contains a 64-bit FNV-1a fingerprint of the entire chunk +and the chunk's length. The Gear table and cut masks are constants. +Git updates both hashes in the same scan where it looks for a cut. +It sorts the entries and removes duplicates, so repeated chunks do not +inflate the score. The set does not preserve offsets, order, or +repetition counts. It cannot reconstruct a blob and is not written to +disk or sent to a peer. + +== Trying a hinted base + +The normal search runs first. If it finds a delta no larger than one +eighth of the target before compression, Git skips the hint. Otherwise, +if the target bytes are already loaded, Git can compare the target's +fingerprint set against the retained bases. It screens out bases based +on delta depth, islands, and the ordinary reuse rules before scoring. + +The score is the sum of the lengths of distinct matching chunks. Git +tries at most one candidate, and only if its score exceeds one quarter +of the target size. Equal scores favor the more recently evicted base. + +Git reloads that base from the local object database without fetching +missing objects. If that read fails, or the normal byte-level delta +matcher rejects the candidate, Git keeps the result of the normal +search. Fingerprint collisions can change which base Git tries, but +cannot cause it to copy mismatched bytes. + +== Cost and compatibility + +Hints cost a fingerprint pass over eligible evicted blobs, plus a +possible base reread. The retained fingerprints and one temporary +fingerprint set use at most about 1.1 MiB per worker on a typical 64-bit +build. This does not include rereading a base or building its delta +index. Rereading a packed base may require reconstructing an on-disk +delta chain larger than the base itself. +For that reason Git skips hints when a nonzero window-memory limit +is set. Hints are off by default; see `pack.pathWalkUseChunkHints` +in linkgit:git-config[1]. + +Hints only help choose a base. Git still writes an ordinary delta +against one base in the existing linkgit:gitformat-pack[5] format. +The reader does not need to know about the chunker or fingerprints. diff --git a/Makefile b/Makefile index 4c84eea6e2ea3e..727b040bf16c9e 100644 --- a/Makefile +++ b/Makefile @@ -1237,6 +1237,7 @@ LIB_OBJS += oidset.o LIB_OBJS += oidtree.o LIB_OBJS += pack-bitmap-write.o LIB_OBJS += pack-bitmap.o +LIB_OBJS += pack-cdc.o LIB_OBJS += pack-check.o LIB_OBJS += pack-mtimes.o LIB_OBJS += pack-objects.o diff --git a/builtin/fast-import.c b/builtin/fast-import.c index ab5d2a23448c72..a79e3e649bdcb0 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -1284,7 +1284,8 @@ static void *gfi_unpack_entry( */ p->pack_size = pack_size + the_hash_algo->rawsz; } - data = unpack_entry(the_repository, p, oe->idx.offset, &type, &size_st); + data = unpack_entry(the_repository, p, oe->idx.offset, &type, &size_st, + 0, NULL); if (sizep) *sizep = cast_size_t_to_ulong(size_st); return data; diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c index 87c3102e622404..4522c106c473fa 100644 --- a/builtin/pack-objects.c +++ b/builtin/pack-objects.c @@ -20,6 +20,7 @@ #include "list-objects.h" #include "list-objects-filter-options.h" #include "pack-objects.h" +#include "pack-cdc.h" #include "progress.h" #include "refs.h" #include "thread-utils.h" @@ -233,6 +234,7 @@ static int pack_to_stdout; static int sparse; static int thin; static int path_walk = -1; +static int path_walk_use_chunk_hints; static int num_preferred_base; static struct progress *progress_state; @@ -987,6 +989,32 @@ static void compute_layer_order(struct object_entry **wo, unsigned int *wo_end) } } +static void rebuild_delta_children(void) +{ + struct object_entry *objects = to_pack.objects; + unsigned int i; + + for (i = 0; i < to_pack.nr_objects; i++) { + SET_DELTA_CHILD(&objects[i], NULL); + SET_DELTA_SIBLING(&objects[i], NULL); + } + + /* External bases are roots but can have children as well. */ + for (i = 0; i < to_pack.nr_ext; i++) + SET_DELTA_CHILD(&to_pack.ext_bases[i], NULL); + + /* Link each base's children in the original recency order. */ + for (i = to_pack.nr_objects; i > 0;) { + struct object_entry *entry = &objects[--i]; + struct object_entry *base = DELTA(entry); + + if (!base) + continue; + entry->delta_sibling_idx = base->delta_child_idx; + SET_DELTA_CHILD(base, entry); + } +} + static struct object_entry **compute_write_order(void) { uint32_t max_layers = 1; @@ -998,23 +1026,8 @@ static struct object_entry **compute_write_order(void) for (i = 0; i < to_pack.nr_objects; i++) { objects[i].tagged = 0; objects[i].filled = 0; - SET_DELTA_CHILD(&objects[i], NULL); - SET_DELTA_SIBLING(&objects[i], NULL); - } - - /* - * Fully connect delta_child/delta_sibling network. - * Make sure delta_sibling is sorted in the original - * recency order. - */ - for (i = to_pack.nr_objects; i > 0;) { - struct object_entry *e = &objects[--i]; - if (!DELTA(e)) - continue; - /* Mark me as the first child */ - e->delta_sibling_idx = DELTA(e)->delta_child_idx; - SET_DELTA_CHILD(DELTA(e), e); } + rebuild_delta_children(); /* * Mark objects that are at the tip of tags. @@ -2800,6 +2813,24 @@ size_t oe_get_size_slow(struct packing_data *pack, return size; } +static int delta_reuse_precludes_pair(const struct object_entry *target, + const struct object_entry *source) +{ + /* + * We do not bother to try a delta that we discarded on an + * earlier try, but only when reusing delta data. Note that + * src_entry that is marked as the preferred_base should always + * be considered, as even if we produce a suboptimal delta against + * it, we will still save the transfer cost, as we already know + * the other side has it and we won't send src_entry at all. + */ + return reuse_delta && IN_PACK(target) && + IN_PACK(target) == IN_PACK(source) && + !source->preferred_base && + target->in_pack_type != OBJ_REF_DELTA && + target->in_pack_type != OBJ_OFS_DELTA; +} + static int try_delta(struct unpacked *trg, struct unpacked *src, unsigned max_depth, size_t *mem_usage) { @@ -2815,19 +2846,7 @@ static int try_delta(struct unpacked *trg, struct unpacked *src, if (oe_type(trg_entry) != oe_type(src_entry)) return -1; - /* - * We do not bother to try a delta that we discarded on an - * earlier try, but only when reusing delta data. Note that - * src_entry that is marked as the preferred_base should always - * be considered, as even if we produce a suboptimal delta against - * it, we will still save the transfer cost, as we already know - * the other side has it and we won't send src_entry at all. - */ - if (reuse_delta && IN_PACK(trg_entry) && - IN_PACK(trg_entry) == IN_PACK(src_entry) && - !src_entry->preferred_base && - trg_entry->in_pack_type != OBJ_REF_DELTA && - trg_entry->in_pack_type != OBJ_OFS_DELTA) + if (delta_reuse_precludes_pair(trg_entry, src_entry)) return 0; /* Let's not bust the allowed depth. */ @@ -2970,11 +2989,139 @@ static unsigned int check_delta_limit(struct object_entry *me, unsigned int n) return m; } -static size_t free_unpacked(struct unpacked *n) +#define CDC_HISTORY_BLOBS 16 + +struct cdc_base { + struct object_entry *entry; + struct cdc_fingerprint_set *fingerprints; + unsigned int depth; +}; + +struct cdc_history { + struct cdc_base bases[CDC_HISTORY_BLOBS]; + unsigned int nr, next; +}; + +static void remember_cdc_base(struct cdc_history *history, + const struct unpacked *old) +{ + struct cdc_base *base; + struct cdc_fingerprint_set *fingerprints; + + /* Only fingerprint bytes the ordinary search has already read. */ + if (!old->data || old->entry->preferred_base || + oe_type(old->entry) != OBJ_BLOB) + return; + fingerprints = pack_cdc_fingerprint_blob(old->data, SIZE(old->entry)); + if (!fingerprints) + return; + + /* The archive and one temporary set bound fingerprint memory. */ + base = &history->bases[history->next]; + if (!base->fingerprints) + history->nr++; + free(base->fingerprints); + base->entry = old->entry; + base->fingerprints = fingerprints; + base->depth = old->depth; + history->next = (history->next + 1) % CDC_HISTORY_BLOBS; +} + +static const struct cdc_base *find_cdc_base(const struct cdc_history *history, + const struct unpacked *target, + unsigned int max_depth) +{ + const struct cdc_base *best = NULL; + struct cdc_fingerprint_set *target_fingerprints = NULL; + size_t target_size, best_score; + + if (!history->nr || !target->data || oe_type(target->entry) != OBJ_BLOB) + return NULL; + target_size = SIZE(target->entry); + best_score = target_size / 4; + for (unsigned int i = 0; i < history->nr; i++) { + unsigned int pos = (history->next + CDC_HISTORY_BLOBS - 1 - i) % + CDC_HISTORY_BLOBS; + const struct cdc_base *base = &history->bases[pos]; + size_t score; + + if (base->depth >= max_depth || + delta_reuse_precludes_pair(target->entry, base->entry) || + !in_same_island(&target->entry->idx.oid, &base->entry->idx.oid)) + continue; + if (!target_fingerprints) { + target_fingerprints = pack_cdc_fingerprint_blob(target->data, + target_size); + if (!target_fingerprints) + return NULL; + } + score = pack_cdc_shared_bytes(target_fingerprints, base->fingerprints); + if (score > best_score) { + best = base; + best_score = score; + } + } + free(target_fingerprints); + return best; +} + +static int try_cdc_delta(const struct cdc_history *history, + struct unpacked *target, unsigned int max_depth) +{ + const struct cdc_base *base; + struct unpacked source = { 0 }; + struct object_info info = OBJECT_INFO_INIT; + enum object_type type; + size_t size; + int ret, accepted = 0; + + /* Do not look farther afield when the window found a small delta. */ + if (DELTA(target->entry) && + DELTA_SIZE(target->entry) <= SIZE(target->entry) / 8) + return 0; + base = find_cdc_base(history, target, max_depth); + if (!base) + return 0; + source.entry = base->entry; + source.depth = base->depth; + info.typep = &type; + info.sizep = &size; + info.contentp = &source.data; + + /* This object would not have been read now on the stock path. */ + packing_data_lock(&to_pack); + ret = odb_read_object_info_extended(the_repository->objects, + &source.entry->idx.oid, &info, + OBJECT_INFO_SKIP_FETCH_OBJECT); + packing_data_unlock(&to_pack); + if (ret || !source.data || type != OBJ_BLOB || size != SIZE(source.entry)) + goto out; + + accepted = try_delta(target, &source, max_depth, &size) > 0; + if (accepted && trace2_is_enabled()) { + char target_oid[GIT_MAX_HEXSZ + 1], base_oid[GIT_MAX_HEXSZ + 1]; + struct strbuf message = STRBUF_INIT; + + strbuf_addf(&message, "%s %s", + oid_to_hex_r(target_oid, &target->entry->idx.oid), + oid_to_hex_r(base_oid, &source.entry->idx.oid)); + trace2_data_string("pack-objects", the_repository, + "path-walk-cdc/hint", message.buf); + strbuf_release(&message); + } + free_delta_index(source.index); +out: + free(source.data); + return accepted; +} + +static size_t free_unpacked(struct unpacked *n, struct cdc_history *history) { size_t freed_mem = sizeof_delta_index(n->index); free_delta_index(n->index); n->index = NULL; + if (history) + remember_cdc_base(history, n); if (n->data) { freed_mem += SIZE(n->entry); FREE_AND_NULL(n->data); @@ -2985,13 +3132,17 @@ static size_t free_unpacked(struct unpacked *n) } static void find_deltas(struct object_entry **list, unsigned *list_size, - int window, int depth, unsigned *processed) + int window, int depth, unsigned *processed, + int use_chunk_hints) { uint32_t i, idx = 0, count = 0; struct unpacked *array; + struct cdc_history *history = NULL; size_t mem_usage = 0; CALLOC_ARRAY(array, window); + if (use_chunk_hints) + CALLOC_ARRAY(history, 1); for (;;) { struct object_entry *entry; @@ -3011,14 +3162,14 @@ static void find_deltas(struct object_entry **list, unsigned *list_size, } progress_unlock(); - mem_usage -= free_unpacked(n); + mem_usage -= free_unpacked(n, history); n->entry = entry; while (window_memory_limit && mem_usage > window_memory_limit && count > 1) { const uint32_t tail = (idx + window - count) % window; - mem_usage -= free_unpacked(array + tail); + mem_usage -= free_unpacked(array + tail, NULL); count--; } @@ -3057,6 +3208,9 @@ static void find_deltas(struct object_entry **list, unsigned *list_size, best_base = other_idx; } + if (history && try_cdc_delta(history, n, max_depth)) + best_base = -1; + /* * If we decided to cache the delta data, then it is best * to compress it right away. First because we have to do @@ -3099,7 +3253,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size, * currently deltified object, to keep it longer. It will * be the first base object to be attempted next. */ - if (DELTA(entry)) { + if (best_base >= 0) { struct unpacked swap = array[best_base]; int dist = (window + idx - best_base) % window; int dst = best_base; @@ -3124,6 +3278,11 @@ static void find_deltas(struct object_entry **list, unsigned *list_size, free(array[i].data); } free(array); + if (history) { + for (i = 0; i < CDC_HISTORY_BLOBS; i++) + free(history->bases[i].fingerprints); + free(history); + } } /* @@ -3185,7 +3344,7 @@ static void *threaded_find_deltas(void *arg) progress_unlock(); find_deltas(me->list, &me->remaining, - me->window, me->depth, me->processed); + me->window, me->depth, me->processed, 0); progress_lock(); me->working = 0; @@ -3222,7 +3381,7 @@ static void ll_find_deltas(struct object_entry **list, unsigned list_size, init_threaded_search(); if (delta_search_threads <= 1) { - find_deltas(list, &list_size, window, depth, processed); + find_deltas(list, &list_size, window, depth, processed, 0); cleanup_threaded_search(); return; } @@ -3433,7 +3592,14 @@ static void find_deltas_for_region(struct object_entry *list, } QSORT(delta_list, delta_list_nr, type_size_sort); - find_deltas(delta_list, &delta_list_nr, window, depth, processed); + /* + * Rereading an evicted packed blob can expand an on-disk delta + * chain larger than the blob itself. Do not do this extra work + * when the caller limits window memory. + */ + find_deltas(delta_list, &delta_list_nr, window, depth, processed, + path_walk_use_chunk_hints && !window_memory_limit && + delta_list_nr > (unsigned int)window); free(delta_list); } @@ -3648,9 +3814,15 @@ static void prepare_pack(int window, int depth) if (!to_pack.nr_objects || !window || !depth) return; - if (path_walk) + if (path_walk) { ll_find_deltas_by_region(to_pack.objects, to_pack.regions, 0, to_pack.nr_regions); + /* + * The remaining search may deltify a base from the path + * pass. Account for its children when limiting its depth. + */ + rebuild_delta_children(); + } ALLOC_ARRAY(delta_list, to_pack.nr_objects); nr_deltas = n = 0; @@ -3690,6 +3862,10 @@ static int git_pack_config(const char *k, const char *v, window = git_config_int(k, v, ctx->kvi); return 0; } + if (!strcmp(k, "pack.pathwalkusechunkhints")) { + path_walk_use_chunk_hints = git_config_bool(k, v); + return 0; + } if (!strcmp(k, "pack.windowmemory")) { window_memory_limit = git_config_ulong(k, v, ctx->kvi); return 0; diff --git a/meson.build b/meson.build index b5ac4403c83272..bcc1e54060d5ce 100644 --- a/meson.build +++ b/meson.build @@ -444,6 +444,7 @@ libgit_sources = [ 'oidtree.c', 'pack-bitmap-write.c', 'pack-bitmap.c', + 'pack-cdc.c', 'pack-check.c', 'pack-mtimes.c', 'pack-objects.c', diff --git a/odb.c b/odb.c index de5be285d35391..c4f7bcafee3655 100644 --- a/odb.c +++ b/odb.c @@ -450,13 +450,25 @@ enum odb_read_status odb_read_object_info_extended(struct object_database *odb, enum object_info_flags flags) { enum odb_read_status ret; + struct packed_read_context packed_read = { 0 }; + struct object_info lookup; + int own_context; if (oid->algo && (hash_algo_by_ptr(odb->repo->hash_algo) != oid->algo)) return oid_object_info_convert(odb->repo, oid, oi, flags); + own_context = (flags & OBJECT_INFO_SKIP_FETCH_OBJECT) && + oi && !oi->packed_read; + if (own_context) { + lookup = *oi; + lookup.packed_read = &packed_read; + oi = &lookup; + } obj_read_lock(); ret = do_oid_object_info_extended(odb, oid, oi, flags); obj_read_unlock(); + if (own_context) + free(packed_read.failures); return ret; } diff --git a/odb.h b/odb.h index 857637f98050a1..522e7baec15084 100644 --- a/odb.h +++ b/odb.h @@ -321,6 +321,13 @@ struct odb_source_info { } u; }; +struct packed_read_failure; + +struct packed_read_context { + struct packed_read_failure *failures; + size_t nr, alloc; +}; + /* * The object info contains the query and response that is to be used for * functions that end up reading object information. Callers are expected to @@ -366,6 +373,9 @@ struct object_info { */ struct odb_source_info *source_infop; + /* Internal: packed copies excluded for the current no-fetch lookup. */ + struct packed_read_context *packed_read; + /* * object-info protocol specific. Set by the protocol when the remote * does not recognize the requested object. diff --git a/odb/source-packed.c b/odb/source-packed.c index 7fcbbb4fe9e03d..85487a6083b479 100644 --- a/odb/source-packed.c +++ b/odb/source-packed.c @@ -23,7 +23,8 @@ static int find_pack_entry(struct odb_source_packed *store, const struct object_id *oid, struct pack_entry *e, enum object_info_flags flags, - struct packed_git **bad_pack) + struct packed_git **bad_pack, + const struct packed_read_context *read) { struct packfile_list_entry *l; enum midx_fill_result midx_result = MIDX_FILL_MISS; @@ -31,14 +32,19 @@ static int find_pack_entry(struct odb_source_packed *store, odb_source_prepare(&store->base, 0); if (store->midx) { midx_result = midx_fill_entry(store->midx, oid, e, bad_pack); - if (midx_result == MIDX_FILL_HIT) - return 1; + if (midx_result == MIDX_FILL_HIT) { + if (!read || !packed_read_failed(read, e->p, oid)) + return 1; + if (bad_pack && !*bad_pack) + *bad_pack = e->p; + } } for (l = store->packs.head; l; l = l->next) { struct packed_git *p = l->pack; - if (!p->multi_pack_index && packfile_fill_entry(p, oid, e, bad_pack)) { + if (!p->multi_pack_index && + packfile_fill_entry(p, oid, e, bad_pack, read)) { if (!store->skip_mru_updates) packfile_list_prepend(&store->packs, p); return 1; @@ -66,7 +72,7 @@ static int find_pack_entry(struct odb_source_packed *store, if (prepare_midx_pack(m, i)) continue; p = nth_midxed_pack(m, i); - if (p && packfile_fill_entry(p, oid, e, bad_pack)) + if (p && packfile_fill_entry(p, oid, e, bad_pack, read)) return 1; } } @@ -82,10 +88,12 @@ static enum odb_read_status odb_source_packed_read_object_info(struct odb_source { struct odb_source_packed *packed = odb_source_packed_downcast(source); struct packed_git *bad_pack = NULL; + struct packed_read_context *read = + oi && (flags & OBJECT_INFO_SKIP_FETCH_OBJECT) ? oi->packed_read : NULL; struct pack_entry e; int ret; - if (!find_pack_entry(packed, oid, &e, flags, &bad_pack)) { + if (!find_pack_entry(packed, oid, &e, flags, &bad_pack, read)) { /* * The lookup may have failed because the object is known to be * corrupt in one of the packfiles. Report the object as @@ -109,10 +117,11 @@ static enum odb_read_status odb_source_packed_read_object_info(struct odb_source goto out; } - ret = packed_object_info(packed, e.p, e.offset, oi); + ret = packed_object_info_with_index_pos(packed, e.p, e.offset, + NULL, oi, flags); if (ret < 0) { bad_pack = e.p; - mark_bad_packed_object(e.p, oid); + record_packed_read_failure(read, e.p, oid); goto out; } @@ -133,7 +142,7 @@ static int odb_source_packed_read_object_stream(struct odb_stream **out, struct odb_source_packed *packed = odb_source_packed_downcast(source); struct pack_entry e; - if (!find_pack_entry(packed, oid, &e, 0, NULL)) + if (!find_pack_entry(packed, oid, &e, 0, NULL, NULL)) return -1; return packfile_read_object_stream(out, oid, e.p, e.offset); @@ -158,7 +167,7 @@ static int odb_source_packed_for_each_object_wrapper(const struct object_id *oid struct object_info oi = *data->request; if (packed_object_info_with_index_pos(data->store, pack, offset, - &index_pos, &oi) < 0) { + &index_pos, &oi, 0) < 0) { mark_bad_packed_object(pack, oid); return -1; } @@ -639,7 +648,7 @@ static int odb_source_packed_freshen_object(struct odb_source *source, timesp = × } - if (!find_pack_entry(packed, oid, &e, 0, NULL)) + if (!find_pack_entry(packed, oid, &e, 0, NULL, NULL)) return 0; if (e.p->is_cruft) return 0; diff --git a/pack-cdc.c b/pack-cdc.c new file mode 100644 index 00000000000000..ecf7c392954dc7 --- /dev/null +++ b/pack-cdc.c @@ -0,0 +1,203 @@ +#include "git-compat-util.h" +#include "pack-cdc.h" + +#define CDC_MIN_CHUNK (16 * 1024) +#define CDC_AVG_CHUNK (64 * 1024) +#define CDC_MAX_CHUNK (256 * 1024) +#define CDC_MIN_BLOB_SIZE (4 * 1024 * 1024) +#define CDC_MAX_BLOB_SIZE (64 * 1024 * 1024) +#define CDC_STRICT_MASK (CDC_AVG_CHUNK * 2 - 1) +#define CDC_LOOSE_MASK (CDC_AVG_CHUNK / 2 - 1) +#define CDC_FNV_PRIME 1099511628211ULL + +/* SplitMix64 output for seed 0x243f6a8885a308d3, indexed by byte. */ +static const uint64_t gear[256] = { + 0x2cb0f69f4abea221ULL, 0x9417034723148989ULL, 0xdd555950609dfe03ULL, + 0xdbafb150deb12800ULL, 0x7e789b2e6c442cb6ULL, 0xf41e5636c7e4f8c4ULL, + 0x0959d150f8fba7e4ULL, 0xa97316f13cdb9eeaULL, 0x74cd8258f9520068ULL, + 0x55c74a62e116868bULL, 0xd2f4c799a2023cbdULL, 0xdf98cb79a37b51b9ULL, + 0x396f5885524f3905ULL, 0xaf1d56386ca3b276ULL, 0xa9ffbe6b5104e85aULL, + 0x6bd0c51b9fd533b3ULL, 0x980ce91c50ab4b56ULL, 0x28ac395780fe62c5ULL, + 0x768912e3a6bcedc7ULL, 0x50b3e8c9332c7c88ULL, 0xce3bbfe520bd47daULL, + 0xcba6c8e8e0bb7c4fULL, 0xbf194db8434a346dULL, 0x7d8f2a7b60416d7fULL, + 0x0849d1f6e0e10a5eULL, 0x7654b590d064e22fULL, 0x16d1da9507df3af2ULL, + 0xf63aef1089ea30e4ULL, 0x9ade6673cc6c522bULL, 0x4c75bc274e37087cULL, + 0xd35e12b49f51f27bULL, 0x22ddf2ffcee481eaULL, 0x06007fb13c59a1f1ULL, + 0x8966a38c651ea4daULL, 0x25242f018fc01ac6ULL, 0xa73ec74fa31b717cULL, + 0x7ee0abdd9797d3a2ULL, 0x5c06ff7dc4ac1880ULL, 0x8434e41042c28a7dULL, + 0x770a372d64327351ULL, 0xeed940dad9e9c06dULL, 0x8977e93646524825ULL, + 0xa9897f0a62a51616ULL, 0xa35d4250c53f2b3aULL, 0x4072542a94b9c33eULL, + 0x3154a7a62447e8abULL, 0x686865712a1a245eULL, 0x0fba67727d7b3b98ULL, + 0x0634e2024536912fULL, 0xd9ff52a26cf9881aULL, 0x9435dc0399f932daULL, + 0x18d39fc1af93e7f0ULL, 0x12f7147c1e7f46abULL, 0xdedf66783eddb4a0ULL, + 0x6f75480614554798ULL, 0xe40e95e8ef84bde2ULL, 0xbb41fe601fefb566ULL, + 0x5c3702e4c7bf19f1ULL, 0x8c7d1d0d3d4a8ec5ULL, 0xee779996ba62dccbULL, + 0x80ccb15bf530844bULL, 0xdf56e7dc4d57959cULL, 0x9eb86a81fe90b68eULL, + 0x6a25741fa696fbd3ULL, 0x7009346385a45644ULL, 0x8f4acc8c1520dd73ULL, + 0x75a59d61ae0f8464ULL, 0xd9600a5f4b8b735cULL, 0x90ee70d4c2774058ULL, + 0x8a5f6c4b9a613341ULL, 0xbae94e097390fd42ULL, 0x653727708a8cae7cULL, + 0x54a64593163b976fULL, 0x551fb9261926a565ULL, 0x903b2aad4c38672aULL, + 0x83731d929aa1ff24ULL, 0x48311d2ec01f36edULL, 0x53a5db5b92e313efULL, + 0xd3b8cb608aab8b70ULL, 0x0f022cd022ea0cbfULL, 0xba7e97a12f21baa6ULL, + 0xb895acc1e36f3046ULL, 0x88cb4b1adbf0f0c0ULL, 0xa08f47edd89b430bULL, + 0x4060ccb36efd6c18ULL, 0x0dcf835fb6b9345eULL, 0x38df4ac46ee5762bULL, + 0x986360357932dcbdULL, 0xbdeb8d63741fe7d9ULL, 0x5d23cb0aedffc430ULL, + 0x6a5efe3a842100a4ULL, 0x0d4cc01bf4e09a16ULL, 0x03dbef4217c97212ULL, + 0x3d8ded6c69c8b3acULL, 0x53d290fa4dcee280ULL, 0x00ce706478000997ULL, + 0xbdf7b12c56756763ULL, 0x06c99071719dc103ULL, 0xd5897678e0df3feeULL, + 0x74429d9ac72f7146ULL, 0x9730ae769149cbbaULL, 0x10ec1a636fd6612dULL, + 0x5dc5d9ea650fa766ULL, 0xb360e068cac3adc2ULL, 0xf8df11cb5ce17a0cULL, + 0xa9292bbae2191df9ULL, 0x3f3d169157da4aefULL, 0x41d2dab33367f9dfULL, + 0x95e671eefbd33caeULL, 0xd5bedcacb64a8fa9ULL, 0xe494760f1ba45656ULL, + 0x21b556b8b6ee2c5fULL, 0xa1ed31d3d69b05ccULL, 0x025819f971a39e83ULL, + 0xb9b3379a4081919aULL, 0x550758640bf14a28ULL, 0x151feebb4e040f10ULL, + 0x423490df7adfc8b3ULL, 0x8bae8d6e276c88e4ULL, 0x526dd4f720811612ULL, + 0xffd5fb93b0b2d28cULL, 0xa9abb68f830215a8ULL, 0x1751110c78d039feULL, + 0x103f09c76e08c0b5ULL, 0x2862583ce905324fULL, 0x939829751e945862ULL, + 0xfd2baf95439547eeULL, 0x3f96e3e88a7e3ef0ULL, 0x3db34783d40d6e72ULL, + 0xb2fd49e41fa25861ULL, 0x18d2c928bf0bc4a3ULL, 0x2806ff0a63ce82b4ULL, + 0x86748de3e14404e4ULL, 0xa22ae3b5ff1a68ceULL, 0x316214df224e0d71ULL, + 0xd8fb60f9bcdde6b5ULL, 0x75931e90d5b688cdULL, 0x97974eee0cea70baULL, + 0x3c0e3e31c2286c53ULL, 0x538bc977baa5c994ULL, 0xf384a2908191bd29ULL, + 0x0e28d06838b555d6ULL, 0xe3cf2205411e6d7aULL, 0xedecb325806e77f0ULL, + 0x5b8463e7456b20b8ULL, 0x5569ba971a13cabdULL, 0x97d3d2e344f1e484ULL, + 0x17704ebfa5491f08ULL, 0xd068968795a32b72ULL, 0x7d579c7c04aea72aULL, + 0x056f6c5d6e07d38dULL, 0x8267cc6ec5069efcULL, 0xdf270c1ef21852dfULL, + 0x75f3cfa3ff5b74a8ULL, 0x9453cd41c9093294ULL, 0xad8cc50d02158220ULL, + 0x494a8e68b6811522ULL, 0xfdc2dc1fb526a978ULL, 0xa00d7fb47afa2772ULL, + 0x02a5a6b22b45d376ULL, 0xdb7a320686bd2cbbULL, 0xbb7ec9db8ed84107ULL, + 0xa0419a506cb535efULL, 0x751678b4c82d1e2aULL, 0xd6a0398ca01ef5acULL, + 0xbec9d0e6fd0b27e8ULL, 0x363ed5d997c510eaULL, 0xaa8cfd101861575fULL, + 0xc35f6c57190c3646ULL, 0xaa58edd1230b6282ULL, 0xaee6bb4c99509c3aULL, + 0x6a1e8c62db7b532bULL, 0xd275c05e4924350aULL, 0xdd5c0daa5d4b823eULL, + 0xa9ae10999c1f45daULL, 0xd0778e076a846e20ULL, 0x6f7304aecd9bbf45ULL, + 0x692ab383113c68aeULL, 0x8b0280356f484328ULL, 0x99866efb37b72076ULL, + 0xb5797760c7108ba6ULL, 0x439febc33d5c0ca0ULL, 0xa306a36c73e81d09ULL, + 0xa927b037250bc6b9ULL, 0xdf2bde709a68740bULL, 0xedcd706720f932ccULL, + 0x61a884c301ee6d4eULL, 0x8108084290f3f2efULL, 0x28321ea11485bd62ULL, + 0x969e36e0e6f9b6deULL, 0x3e6b1d5cf28c5483ULL, 0xc72ebc0070076b77ULL, + 0x13d73121a7a448f6ULL, 0x22743fa795feb53aULL, 0x2bd608cca7803150ULL, + 0xcae4b5723d21581cULL, 0x8e70bbb87a85a239ULL, 0xd98023b873b129aeULL, + 0x77b69e4fcfe53920ULL, 0x0508e387973f9b5fULL, 0xbf2966d283c64f11ULL, + 0xaecdf57019e23471ULL, 0x36e7a8e998fe1e04ULL, 0x0780542bb39c8cd9ULL, + 0x4095e66dab7aee65ULL, 0x2086704201a7469eULL, 0x5a5d698442d2e216ULL, + 0xe421106739485e0cULL, 0xea88e48d6eedd5edULL, 0xf8f91dad5142564dULL, + 0x0504199b2e70f466ULL, 0xa0b0e2c6526d6ee5ULL, 0xfb3bef18a0e0c8a9ULL, + 0x197b1a5236d9566bULL, 0xb14e3945730a5bdfULL, 0xb9b7d6906877ea75ULL, + 0xf618a46b8de61fc1ULL, 0x3fb889497a2f1241ULL, 0xb3aeeaf7fefa8bc5ULL, + 0xcbe100a2efd63f9aULL, 0x3556152543cc4204ULL, 0xd9605d470d63ab58ULL, + 0x15545749b38b81b5ULL, 0x22db5baa269e9752ULL, 0x780040e30aa2c9e6ULL, + 0xc180448b0640c9cbULL, 0x6b2a492483c9456eULL, 0xa76cee29e128036cULL, + 0x089f699d6bb0f074ULL, 0x29faf34444846ecaULL, 0xb3c982023f05a58bULL, + 0xe6efc66581e03a5aULL, 0x52939eb64b758485ULL, 0xf9354e3df005a534ULL, + 0xc68b2a012aa99d70ULL, 0xea7d677dc1397e0fULL, 0x1734bd4c86de6e03ULL, + 0x0356a82459388a9fULL, 0xc43aa3ece4266ee2ULL, 0x893bc7d1412eae2dULL, + 0x3aab49744f9b080eULL, 0xed294b9dfc776923ULL, 0xcd6e499b5d4dade2ULL, + 0x9550e1f6c3b36609ULL, 0x2283c0a27f964ef1ULL, 0x3a9760919b276c63ULL, + 0xdec8b25069a70cfbULL, 0x3b5fab4305a819c8ULL, 0x37accf033fb26034ULL, + 0x9c01f1c52e8578ddULL, 0xc810f4676d8701dfULL, 0x6233712c854b1dfcULL, + 0x90fa9224644845d6ULL, 0x9305a3afe347f3d0ULL, 0xd5e66dbd1941872bULL, + 0xe23fa3d2ba84472eULL, +}; + +struct cdc_fingerprint { + uint64_t hash; + uint32_t length; +}; + +struct cdc_fingerprint_set { + size_t nr; + struct cdc_fingerprint entries[FLEX_ARRAY]; +}; + +static int compare_fingerprints(const void *a, const void *b) +{ + const struct cdc_fingerprint *x = a, *y = b; + + if (x->hash != y->hash) + return x->hash < y->hash ? -1 : 1; + return (x->length > y->length) - (x->length < y->length); +} + +struct cdc_fingerprint_set *pack_cdc_fingerprint_blob(const void *buffer, + size_t size) +{ + const unsigned char *data = buffer; + struct cdc_fingerprint_set *set; + size_t alloc, unique = 0; + + if (!data || size < CDC_MIN_BLOB_SIZE || size > CDC_MAX_BLOB_SIZE) + return NULL; + + alloc = sizeof(*set) + + DIV_ROUND_UP(size, CDC_MIN_CHUNK) * sizeof(*set->entries); + set = malloc(alloc); + if (!set) + return NULL; + set->nr = 0; + + for (size_t start = 0; start < size; ) { + size_t end = start + CDC_MIN_CHUNK; + uint64_t cut_hash = 0; + uint64_t fingerprint = 14695981039346656037ULL; + + /* Include bytes skipped by the boundary search. */ + if (end > size) + end = size; + for (size_t i = start; i < end; i++) + fingerprint = (fingerprint ^ data[i]) * CDC_FNV_PRIME; + + /* Use the same byte for the cut hash and the fingerprint. */ + while (end < size && end - start < CDC_MAX_CHUNK) { + unsigned char byte = data[end++]; + uint64_t mask; + + cut_hash = (cut_hash << 1) + gear[byte]; + fingerprint = (fingerprint ^ byte) * CDC_FNV_PRIME; + mask = end - start < CDC_AVG_CHUNK ? + CDC_STRICT_MASK : CDC_LOOSE_MASK; + if (!(cut_hash & mask)) + break; + } + + set->entries[set->nr++] = (struct cdc_fingerprint){ + .hash = fingerprint, + .length = end - start, + }; + start = end; + } + + QSORT(set->entries, set->nr, compare_fingerprints); + /* Repeated chunks should not inflate a candidate's score. */ + for (size_t i = 0; i < set->nr; i++) { + if (unique && + !compare_fingerprints(&set->entries[unique - 1], + &set->entries[i])) + continue; + set->entries[unique++] = set->entries[i]; + } + set->nr = unique; + return set; +} + +size_t pack_cdc_shared_bytes(const struct cdc_fingerprint_set *target, + const struct cdc_fingerprint_set *base) +{ + size_t score = 0, t = 0, b = 0; + + if (!target || !base) + return 0; + + while (t < target->nr && b < base->nr) { + int cmp = compare_fingerprints(&target->entries[t], + &base->entries[b]); + + if (!cmp) + score += target->entries[t].length; + if (cmp <= 0) + t++; + if (cmp >= 0) + b++; + } + return score; +} diff --git a/pack-cdc.h b/pack-cdc.h new file mode 100644 index 00000000000000..fb1c7f5a865c24 --- /dev/null +++ b/pack-cdc.h @@ -0,0 +1,22 @@ +#ifndef PACK_CDC_H +#define PACK_CDC_H + +struct cdc_fingerprint_set; + +/* + * Collect a blob's distinct chunk fingerprints. The result does not + * retain the input buffer and can be released with free(). Return NULL + * for out-of-range input or if allocation fails. + */ +struct cdc_fingerprint_set *pack_cdc_fingerprint_blob(const void *data, + size_t size); + +/* + * Sum the lengths of chunks with distinct matching fingerprints, or return + * zero if either set is missing. This is only an estimate; callers must + * still compare the actual bytes when choosing a delta. + */ +size_t pack_cdc_shared_bytes(const struct cdc_fingerprint_set *target, + const struct cdc_fingerprint_set *base); + +#endif diff --git a/pack-check.c b/pack-check.c index 605811b7b094e3..ab707c47c3c8ed 100644 --- a/pack-check.c +++ b/pack-check.c @@ -144,7 +144,7 @@ static int verify_packfile(struct repository *r, data_valid = 0; } else { data = unpack_entry(r, p, entries[i].offset, &type, - &size); + &size, 0, NULL); data_valid = 1; } diff --git a/packfile.c b/packfile.c index b32b4cd75dad31..05bdde5a3ae384 100644 --- a/packfile.c +++ b/packfile.c @@ -1048,17 +1048,22 @@ static int get_delta_base_oid(struct packed_git *p, static int retry_bad_packed_offset(struct repository *r, struct packed_git *p, - off_t obj_offset) + off_t obj_offset, + enum object_info_flags flags, + struct packed_read_context *read) { - int type; + struct object_info oi = { .packed_read = read }; + enum object_type type; uint32_t pos; struct object_id oid; if (offset_to_pack_pos(p, obj_offset, &pos) < 0) return OBJ_BAD; nth_packed_object_id(&oid, p, pack_pos_to_index(p, pos)); - mark_bad_packed_object(p, &oid); - type = odb_read_object_info(r->objects, &oid, NULL); - if (type <= OBJ_NONE) + record_packed_read_failure(read, p, &oid); + oi.typep = &type; + flags = OBJECT_INFO_LOOKUP_REPLACE | (flags & OBJECT_INFO_SKIP_FETCH_OBJECT); + if (odb_read_object_info_extended(r->objects, &oid, &oi, flags) < 0 || + type <= OBJ_NONE) return OBJ_BAD; return type; } @@ -1070,7 +1075,9 @@ static enum object_type packed_to_object_type(struct repository *r, off_t obj_offset, enum object_type type, struct pack_window **w_curs, - off_t curpos) + off_t curpos, + enum object_info_flags flags, + struct packed_read_context *read) { off_t small_poi_stack[POI_STACK_PREALLOC]; off_t *poi_stack = small_poi_stack; @@ -1097,7 +1104,7 @@ static enum object_type packed_to_object_type(struct repository *r, if (type <= OBJ_NONE) { /* If getting the base itself fails, we first * retry the base, otherwise unwind */ - type = retry_bad_packed_offset(r, p, base_offset); + type = retry_bad_packed_offset(r, p, base_offset, flags, read); if (type > OBJ_NONE) goto out; goto unwind; @@ -1125,7 +1132,7 @@ static enum object_type packed_to_object_type(struct repository *r, unwind: while (poi_stack_nr) { obj_offset = poi_stack[--poi_stack_nr]; - type = retry_bad_packed_offset(r, p, obj_offset); + type = retry_bad_packed_offset(r, p, obj_offset, flags, read); if (type > OBJ_NONE) goto out; } @@ -1220,13 +1227,15 @@ static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent) static void *cache_or_unpack_entry(struct repository *r, struct packed_git *p, off_t base_offset, size_t *base_size, - enum object_type *type) + enum object_type *type, + enum object_info_flags flags, + struct packed_read_context *read) { struct delta_base_cache_entry *ent; ent = get_delta_base_cache_entry(p, base_offset); if (!ent) - return unpack_entry(r, p, base_offset, type, base_size); + return unpack_entry(r, p, base_offset, type, base_size, flags, read); if (type) *type = ent->type; @@ -1318,9 +1327,12 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset, int packed_object_info_with_index_pos(struct odb_source_packed *source, struct packed_git *p, off_t obj_offset, - uint32_t *maybe_index_pos, struct object_info *oi) + uint32_t *maybe_index_pos, struct object_info *oi, + enum object_info_flags flags) { struct pack_window *w_curs = NULL; + struct packed_read_context *read = + flags & OBJECT_INFO_SKIP_FETCH_OBJECT ? oi->packed_read : NULL; size_t size; off_t curpos = obj_offset; enum object_type type = OBJ_NONE; @@ -1333,7 +1345,7 @@ int packed_object_info_with_index_pos(struct odb_source_packed *source, */ if (oi->contentp) { *oi->contentp = cache_or_unpack_entry(p->repo, p, obj_offset, - oi->sizep, &type); + oi->sizep, &type, flags, read); if (!*oi->contentp) type = OBJ_BAD; } else if (oi->sizep || oi->typep || oi->delta_base_oid) { @@ -1392,7 +1404,7 @@ int packed_object_info_with_index_pos(struct odb_source_packed *source, if (oi->typep) { enum object_type ptot; ptot = packed_to_object_type(p->repo, p, obj_offset, - type, &w_curs, curpos); + type, &w_curs, curpos, flags, read); if (oi->typep) *oi->typep = ptot; if (ptot < 0) { @@ -1448,7 +1460,7 @@ int packed_object_info(struct odb_source_packed *source, struct packed_git *p, off_t obj_offset, struct object_info *oi) { - return packed_object_info_with_index_pos(source, p, obj_offset, NULL, oi); + return packed_object_info_with_index_pos(source, p, obj_offset, NULL, oi, 0); } static void *unpack_compressed_entry(struct packed_git *p, @@ -1513,7 +1525,8 @@ struct unpack_entry_stack_ent { }; void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset, - enum object_type *final_type, size_t *final_size) + enum object_type *final_type, size_t *final_size, + enum object_info_flags flags, struct packed_read_context *read) { struct pack_window *w_curs = NULL; off_t curpos = obj_offset; @@ -1524,6 +1537,7 @@ void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset, struct unpack_entry_stack_ent *delta_stack = small_delta_stack; int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC; int base_from_cache = 0; + enum object_info_flags fetch_flags = flags & OBJECT_INFO_SKIP_FETCH_OBJECT; prepare_repo_settings(p->repo); @@ -1646,7 +1660,7 @@ void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset, uint32_t pos; struct object_id base_oid; if (!(offset_to_pack_pos(p, obj_offset, &pos))) { - struct object_info oi = OBJECT_INFO_INIT; + struct object_info oi = { .packed_read = read }; nth_packed_object_id(&base_oid, p, pack_pos_to_index(p, pos)); @@ -1654,13 +1668,12 @@ void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset, " at offset %"PRIuMAX" from %s", oid_to_hex(&base_oid), (uintmax_t)obj_offset, p->pack_name); - mark_bad_packed_object(p, &base_oid); - + record_packed_read_failure(read, p, &base_oid); oi.typep = &type; oi.sizep = &base_size; oi.contentp = &base; if (odb_read_object_info_extended(r->objects, &base_oid, - &oi, 0) < 0) + &oi, fetch_flags) < 0) base = NULL; external_base = base; @@ -1849,15 +1862,43 @@ int is_pack_valid(struct packed_git *p) return !open_packed_git(p); } +struct packed_read_failure { + struct packed_git *pack; + struct object_id oid; +}; + +int packed_read_failed(const struct packed_read_context *read, + const struct packed_git *p, const struct object_id *oid) +{ + for (size_t i = 0; i < read->nr; i++) + if (read->failures[i].pack == p && oideq(&read->failures[i].oid, oid)) + return 1; + return 0; +} + +void record_packed_read_failure(struct packed_read_context *read, + struct packed_git *p, const struct object_id *oid) +{ + if (!read) { + mark_bad_packed_object(p, oid); + return; + } + if (packed_read_failed(read, p, oid)) + return; + ALLOC_GROW(read->failures, read->nr + 1, read->alloc); + read->failures[read->nr++] = (struct packed_read_failure) { p, *oid }; +} + int packfile_fill_entry(struct packed_git *p, const struct object_id *oid, struct pack_entry *e, - struct packed_git **bad_pack) + struct packed_git **bad_pack, + const struct packed_read_context *read) { off_t offset; - if (oidset_size(&p->bad_objects) && - oidset_contains(&p->bad_objects, oid)) { + if ((oidset_size(&p->bad_objects) && oidset_contains(&p->bad_objects, oid)) || + (read && packed_read_failed(read, p, oid))) { if (bad_pack && !*bad_pack) *bad_pack = p; return 0; @@ -1955,7 +1996,7 @@ int has_object_kept_pack(struct repository *r, const struct object_id *oid, for (; *cache; cache++) { struct packed_git *p = *cache; - if (packfile_fill_entry(p, oid, &e, NULL)) + if (packfile_fill_entry(p, oid, &e, NULL, NULL)) return 1; } } diff --git a/packfile.h b/packfile.h index ed3378c45761dd..5c51f59704462b 100644 --- a/packfile.h +++ b/packfile.h @@ -293,11 +293,13 @@ off_t find_pack_entry_one(const struct object_id *oid, struct packed_git *); int packfile_fill_entry(struct packed_git *p, const struct object_id *oid, struct pack_entry *e, - struct packed_git **bad_pack); + struct packed_git **bad_pack, + const struct packed_read_context *read); int is_pack_valid(struct packed_git *); void *unpack_entry(struct repository *r, struct packed_git *, off_t, - enum object_type *, size_t *); + enum object_type *, size_t *, enum object_info_flags, + struct packed_read_context *); size_t unpack_object_header_buffer(const unsigned char *buf, size_t len, enum object_type *type, size_t *sizep); size_t get_size_from_delta(struct packed_git *, struct pack_window **, off_t); @@ -325,9 +327,14 @@ int packed_object_info(struct odb_source_packed *source, off_t offset, struct object_info *); int packed_object_info_with_index_pos(struct odb_source_packed *source, struct packed_git *p, off_t obj_offset, - uint32_t *maybe_index_pos, struct object_info *oi); + uint32_t *maybe_index_pos, struct object_info *oi, + enum object_info_flags flags); void mark_bad_packed_object(struct packed_git *, const struct object_id *); +int packed_read_failed(const struct packed_read_context *, const struct packed_git *, + const struct object_id *); +void record_packed_read_failure(struct packed_read_context *, struct packed_git *, + const struct object_id *); int has_object_pack(struct repository *r, const struct object_id *oid); int has_object_kept_pack(struct repository *r, const struct object_id *oid, diff --git a/t/helper/test-partial-clone.c b/t/helper/test-partial-clone.c index 87c59108e00ccb..3e09ce9af59491 100644 --- a/t/helper/test-partial-clone.c +++ b/t/helper/test-partial-clone.c @@ -32,15 +32,35 @@ static void object_info(const char *gitdir, const char *oid_hex) repo_clear(&r); } +static void retry_object_info(const char *oid_hex) +{ + struct object_id oid; + enum object_type type; + struct object_info oi = { .typep = &type }; + struct object_database *odb = the_repository->objects; + + if (get_oid_hex(oid_hex, &oid)) + die("could not parse oid"); + if (!odb_read_object_info_extended(odb, &oid, &oi, + OBJECT_INFO_LOOKUP_REPLACE | + OBJECT_INFO_SKIP_FETCH_OBJECT)) + die("unexpected success without fetching"); + if (odb_read_object_info_extended(odb, &oid, &oi, + OBJECT_INFO_LOOKUP_REPLACE)) + die("could not retry object with fetching enabled"); + puts(type_name(type)); +} + int cmd__partial_clone(int argc, const char **argv) { setup_git_directory(the_repository); - if (argc < 4) + if (argc < 2) die("too few arguments"); - - if (!strcmp(argv[1], "object-info")) + if (argc == 4 && !strcmp(argv[1], "object-info")) object_info(argv[2], argv[3]); + else if (argc == 3 && !strcmp(argv[1], "retry-object-info")) + retry_object_info(argv[2]); else die("invalid argument '%s'", argv[1]); diff --git a/t/t5300-pack-object.sh b/t/t5300-pack-object.sh index e5ce478484144c..e50c5f8e70e3fa 100755 --- a/t/t5300-pack-object.sh +++ b/t/t5300-pack-object.sh @@ -766,4 +766,109 @@ test_expect_success '--path-walk thin pack' ' git -C server index-pack --fix-thin --stdin raw && + tr "abcd" "efgh" common && + { + cat common && + test-tool genzeros 327680 + } >source/asset && + git -C source add asset && + git -C source commit -m base && + base=$(git -C source rev-parse HEAD:asset) && + extra=262144 && + for fill in a b c d + do + test-tool genzeros $((5 * 1024 * 1024 + extra)) >zeros && + tr "\000" "$fill" source/asset && + git -C source commit -am "unrelated $fill" || return 1 + extra=$((extra - 65536)) + done && + { + printf "version prefix" && + cat common + } >source/asset && + git -C source commit -am target && + target=$(git -C source rev-parse HEAD:asset) && + git -C source rev-parse HEAD >revs && + + git -C source -c pack.pathWalkUseChunkHints=false pack-objects \ + --stdout --revs --path-walk --window=3 --threads=1 stock.pack && + git init --bare stock && + git -C stock index-pack --strict --stdin stock.objects && + test_grep -E "^$target blob +[0-9]+ [0-9]+ [0-9]+$" stock.objects && + + git -C source -c pack.pathWalkUseChunkHints=true pack-objects \ + --stdout --revs --path-walk --window=3 --threads=1 hinted.pack && + git init --bare hinted && + git -C hinted index-pack --strict --stdin hinted.objects && + test_grep -E "^$target blob +[0-9]+ [0-9]+ [0-9]+ [0-9]+ $base$" hinted.objects && + git -C hinted cat-file blob "$target" >actual && + test_cmp_bin source/asset actual && + + git -C source -c pack.pathWalkUseChunkHints=false pack-objects \ + --stdout --revs --path-walk --window=3 --threads=1 \ + --window-memory=1g limited-stock.pack && + git -C source -c pack.pathWalkUseChunkHints=true pack-objects \ + --stdout --revs --path-walk --window=3 --threads=1 \ + --window-memory=1g limited-hinted.pack && + test_cmp_bin limited-stock.pack limited-hinted.pack && + + # Keep a partial match in the regular window. It can produce + # a delta, but not one as small as the older nearly exact base. + { + dd if=common bs=1048576 count=3 && + test-tool genrandom "cdc weak base" 2129920 + } >source/asset && + git -C source commit -am "weak base" && + weak=$(git -C source rev-parse HEAD:asset) && + git -C source rev-parse HEAD >revs && + git -C source -c pack.pathWalkUseChunkHints=false pack-objects \ + --stdout --revs --path-walk --window=3 --threads=1 weak-stock.pack && + git -C stock index-pack --strict --stdin weak-stock.hash && + stock_id=$(sed "s/^pack[ ]*//" weak-stock.hash) && + git verify-pack -v "stock/objects/pack/pack-$stock_id.idx" >weak-stock.objects && + test_grep -E "^$target blob +[0-9]+ [0-9]+ [0-9]+ [0-9]+ $weak$" weak-stock.objects && + git -C source -c pack.pathWalkUseChunkHints=true pack-objects \ + --stdout --revs --path-walk --window=3 --threads=1 weak-hinted.pack && + git -C hinted index-pack --strict --stdin weak-hinted.hash && + hinted_id=$(sed "s/^pack[ ]*//" weak-hinted.hash) && + git verify-pack -v "hinted/objects/pack/pack-$hinted_id.idx" >weak-hinted.objects && + test_grep -E "^$target blob +[0-9]+ [0-9]+ [0-9]+ [0-9]+ $base$" weak-hinted.objects && + + # Put the archived base and target in one pack, leaving the + # ordinary window able to read and try loose candidates. + git -C source rev-parse HEAD^ >revs && + printf "%s\n" "$base" "$target" >objects && + (cd source && git pack-objects --window=0 .git/objects/pack/pack <../objects) && + git -C source prune-packed && + GIT_TRACE_BARE=1 GIT_TRACE_PACK_ACCESS="$PWD/packed-stock.trace" \ + git -C source -c pack.pathWalkUseChunkHints=false pack-objects \ + --stdout --revs --path-walk --window=3 --threads=1 packed-stock.pack && + GIT_TRACE_BARE=1 GIT_TRACE_PACK_ACCESS="$PWD/packed-hinted.trace" \ + git -C source -c pack.pathWalkUseChunkHints=true pack-objects \ + --stdout --revs --path-walk --window=3 --threads=1 packed-hinted.pack && + test_cmp packed-stock.trace packed-hinted.trace && + test_cmp_bin packed-stock.pack packed-hinted.pack && + + git -C source -c pack.pathWalkUseChunkHints=true pack-objects \ + --stdout --revs --path-walk --window=3 --threads=1 \ + --no-reuse-delta recomputed.pack && + git -C hinted index-pack --strict --stdin recomputed.hash && + recomputed_id=$(sed "s/^pack[ ]*//" recomputed.hash) && + git verify-pack -v "hinted/objects/pack/pack-$recomputed_id.idx" >recomputed.objects && + test_grep -E "^$target blob +[0-9]+ [0-9]+ [0-9]+ [0-9]+ $base$" recomputed.objects + ) +' + test_done diff --git a/t/t5303-pack-corruption-resilience.sh b/t/t5303-pack-corruption-resilience.sh index ab99c8b6850481..c86806369e729e 100755 --- a/t/t5303-pack-corruption-resilience.sh +++ b/t/t5303-pack-corruption-resilience.sh @@ -319,6 +319,63 @@ test_expect_success 'corrupting header to have too small output buffer fails unp test_must_fail git cat-file blob $blob_3 > /dev/null ' +test_expect_success 'mktree does not fetch a replacement for a corrupt delta base' ' + test_when_finished "rm -rf promisor" && + create_new_pack && + git prune-packed && + git init --bare promisor && + test_config -C promisor uploadpack.allowFilter true && + echo replacement >replacement && + replacement=$(git hash-object -w replacement) && + git -C promisor hash-object -w --stdin entry && + + test_must_fail git mktree err && + test_grep "object $blob_2 is unavailable" err && + test_must_fail git --no-lazy-fetch cat-file -e "$replacement" && + + # A no-fetch failure must not prevent a retry in the same process. + echo blob >expect && + test-tool partial-clone retry-object-info "$blob_2" >actual && + test_cmp expect actual && + git --no-lazy-fetch cat-file blob "$replacement" >actual && + test_cmp replacement actual && + + # Once the replacement is local, mktree can use it too. + tree=$(git mktree actual && + test_cmp entry actual +' + +test_expect_success 'mktree uses a good packed copy after a corrupt one' ' + create_new_pack && + git prune-packed && + printf "%s\n" "$blob_1" >objects && + git pack-objects .git/objects/pack/pack copy_name && + copy=".git/objects/pack/pack-$(cat copy_name)" && + do_corrupt_object "$blob_1" 0 entry && + + test_config core.multiPackIndex false && + mv "$copy.idx" saved.idx && + test_must_fail git mktree err && + test_grep "object $blob_1 is unavailable" err && + mv saved.idx "$copy.idx" && + + tree=$(git mktree actual && + test_cmp entry actual +' + # \0 - empty base # \1 - one byte in result # \1 - one literal byte (X) diff --git a/t/t5316-pack-delta-depth.sh b/t/t5316-pack-delta-depth.sh index 8a067a45cb4bae..5e17d1c5546336 100755 --- a/t/t5316-pack-delta-depth.sh +++ b/t/t5316-pack-delta-depth.sh @@ -121,4 +121,41 @@ test_expect_success 'negative depth disables deltas' ' test_cmp expect actual ' +test_expect_success '--path-walk accounts for deltas from both searches' ' + git init path-depth && + ( + cd path-depth && + mkdir a z && + # Use a common suffix so the global search groups both paths. + file=longsamefilename1234567.z && + test-tool genrandom depth 262144 >content && + { + cat content && + printf "first" + } >"a/$file" && + { + cat content && + printf "larger base" + } >"z/$file" && + git add a z && + git commit -m base && + base=$(git rev-parse "HEAD:a/$file") && + { + printf "shifted" && + test-tool genrandom depth 258048 + } >"a/$file" && + git commit -am update && + updated=$(git rev-parse "HEAD:a/$file") && + + pack=$(git pack-objects --all --path-walk --depth=1 \ + --name-hash-version=1 --threads=1 --no-reuse-delta \ + --no-reuse-object pack actual && + echo 1 >expect && + test_cmp expect actual && + git verify-pack -v "pack-$pack.idx" >entries && + test_grep "^$updated .* 1 $base$" entries + ) +' + test_done