Git development
 help / color / mirror / Atom feed
* [PATCH] delta-islands: revisit trees with new delta marks
@ 2026-11-30 19:25 Derrick Stolee via GitGitGadget
  0 siblings, 0 replies; only message in thread
From: Derrick Stolee via GitGitGadget @ 2026-11-30 19:25 UTC (permalink / raw)
  To: git; +Cc: gitster, peff, ttaylorr, newren, Derrick Stolee, Derrick Stolee

From: Derrick Stolee <stolee@gmail.com>

When exploring the object graph to group objects into delta-islands,
some trees and blobs can be visited from one path and then revisited
later from another. If those two visits come from objects with different
delta marks, then the final mark of the visited object should be the
union of those marks. Instead, some cases only have the first marks as
we don't reprocess objects marked with SEEN. This gets worse for objects
recursively reachable from that object.

This change includes some test cases that demonstrate this problem, and
several of the modes fail without the accompanying code change. These
examples were discovered while working with the --path-walk option as
integrated with delta islands in 7e6de2ac628 (pack-objects: support
`--delta-islands` with `--path-walk`, 2026-06-21), but that option only
exposed an existing failure within the marking algorithm. At least
one of the new tests exposes a failure without --path-walk.

The issue is not just about different path names, but about appearing at
paths of different depth. For example, a tree appearing at a/b/ can be
visited with one set of marks as supplied from the trees at a/ while
later being seen at x/y/z/ with different marks supplied by objects at
x/y/.

To fix this issue, we need to potentially reprocess a tree and
everything it can reach when we discover an update to the delta island
marks. This can lead to massive recomputation with certain data shapes
that revisit objects from different paths. This seems unlikely to happen
often in realistic scenarios, but is unavoidable without topologically
sorting the entire object graph before distributing marks.

Concretely, update set_island_marks() to report whether the object
gained any bits in the process. Use a depth-priority queue to revisit
trees that have new marks. We need to be careful to avoid adding
duplicates to the queue without replicating the SEEN bit. This depth
heuristic is only to prioritize a breadth-first-search, handling root
trees before the next set of trees, and so on. This is intended to
maximize the possibility of seeing all links to an object before
visiting that object.

The first-seen-depth problem was identified during the original
delta-island review [1]: SEEN prevents revisiting shared trees, so their
recorded depths need not give a topological ordering. That discussion
illustrated missed delta opportunities. Incomplete marks can also allow
unwanted cross-island deltas, as acknowledged for a related root-depth
bug in 3949053617 (pack-objects: fix off-by-one in delta-island
tree-depth computation, 2018-11-20) [2]. Later discussion emphasized
real fork-network results and the difficulty of constructing focused
corner-case tests [3].

[1] https://lore.kernel.org/git/20180727130229.GB18599@sigill.intra.peff.net/
[2] https://lore.kernel.org/git/20181120095053.GC22742@sigill.intra.peff.net/
[3] https://lore.kernel.org/git/20181120113423.GA29319@sigill.intra.peff.net/

Using p5305's historical-fork island selection, with --no-path-walk,
bitmap reads disabled, --window=0, --compression=0, and stored deltas
reusable, gives these old-to-fixed command times and tree visits:

                           Time (s)           Tree visits
    git/git              8.08 ->   8.24      162,791 ->   162,795
    nodejs/node          9.85 ->  10.14      238,723 ->   239,837
    microsoft/fluentui   4.13 ->   4.24      287,056 ->   287,136
    torvalds/linux     176.28 -> 170.73    6,888,020 -> 6,888,085

It's possible that a true integration of delta islands with path-walk
would help here, having the path-walk API compute the delta island marks
as we traverse objects in path-based groups. Such an implementation
would need to allow for objects to be readded to groups when their delta
marks are updated while avoiding multiple emissions of the object from
the API. Such a large rework of the delta island computation is avoided
for now, as we can reproduce the error with the existing implementation.

Assisted-by: GPT-6.1 Sol
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
    delta-islands: revisit trees with new delta marks
    
    Discovered while working on a real application of delta islands and the
    --path-walk feature.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2343%2Fderrickstolee%2Fdelta-islands-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2343/derrickstolee/delta-islands-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2343

 delta-islands.c          | 63 +++++++++++++++++++---------
 t/t5320-delta-islands.sh | 91 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+), 20 deletions(-)

diff --git a/delta-islands.c b/delta-islands.c
index c1c0f992f5..840484a4bc 100644
--- a/delta-islands.c
+++ b/delta-islands.c
@@ -10,6 +10,7 @@
 #include "pack.h"
 #include "tree-walk.h"
 #include "diff.h"
+#include "prio-queue.h"
 #include "progress.h"
 #include "refs.h"
 #include "khash.h"
@@ -158,7 +159,7 @@ static struct island_bitmap *create_or_get_island_marks(struct object *obj)
 	return kh_value(island_marks, pos);
 }
 
-static void set_island_marks(struct object *obj, struct island_bitmap *marks)
+static int set_island_marks(struct object *obj, struct island_bitmap *marks)
 {
 	struct island_bitmap *b;
 	khiter_t pos;
@@ -172,19 +173,23 @@ static void set_island_marks(struct object *obj, struct island_bitmap *marks)
 		 */
 		marks->refcount++;
 		kh_value(island_marks, pos) = marks;
-		return;
+		return 1;
 	}
 
+	b = kh_value(island_marks, pos);
+	if (island_bitmap_is_subset(marks, b))
+		return 0;
+
 	/*
 	 * We do have it. Make sure we split any copy-on-write before
 	 * updating.
 	 */
-	b = kh_value(island_marks, pos);
 	if (b->refcount > 1) {
 		b->refcount--;
 		b = kh_value(island_marks, pos) = island_bitmap_new(b);
 	}
 	island_bitmap_or(b, marks);
+	return 1;
 }
 
 static void mark_remote_island_1(struct repository *r,
@@ -226,9 +231,10 @@ static void mark_remote_island_1(struct repository *r,
 struct tree_islands_todo {
 	struct object_entry *entry;
 	unsigned int depth;
+	unsigned queued:1;
 };
 
-static int tree_depth_compare(const void *a, const void *b)
+static int tree_depth_compare(const void *a, const void *b, void *data UNUSED)
 {
 	const struct tree_islands_todo *todo_a = a;
 	const struct tree_islands_todo *todo_b = b;
@@ -242,40 +248,44 @@ void resolve_tree_islands(struct repository *r,
 {
 	struct progress *progress_state = NULL;
 	struct tree_islands_todo *todo;
-	int nr = 0;
-	int i;
+	struct tree_islands_todo *current;
+	struct prio_queue queue = { .compare = tree_depth_compare };
+	uint64_t processed = 0;
+	uint32_t i;
 
 	if (!island_marks)
 		return;
 
 	/*
 	 * We process only trees, as commits and tags have already been handled
-	 * (and passed their marks on to root trees, as well. We must make sure
-	 * to process them in descending tree-depth order so that marks
-	 * propagate down the tree properly, even if a sub-tree is found in
-	 * multiple parent trees.
+	 * (and passed their marks on to root trees, as well). Prefer shallower
+	 * trees, but the first-seen depth can place a shared tree before
+	 * another parent. Requeue trees whose marks change after processing.
 	 */
 	ALLOC_ARRAY(todo, to_pack->nr_objects);
 	for (i = 0; i < to_pack->nr_objects; i++) {
 		if (oe_type(&to_pack->objects[i]) == OBJ_TREE) {
-			todo[nr].entry = &to_pack->objects[i];
-			todo[nr].depth = oe_tree_depth(to_pack, &to_pack->objects[i]);
-			nr++;
+			todo[i].entry = &to_pack->objects[i];
+			todo[i].depth = oe_tree_depth(to_pack, &to_pack->objects[i]);
+			todo[i].queued = 1;
+			prio_queue_put(&queue, &todo[i]);
 		}
 	}
-	QSORT(todo, nr, tree_depth_compare);
 
 	if (progress)
-		progress_state = start_progress(r, _("Propagating island marks"), nr);
+		progress_state = start_progress(r, _("Propagating island marks"), 0);
 
-	for (i = 0; i < nr; i++) {
-		struct object_entry *ent = todo[i].entry;
+	while ((current = prio_queue_get(&queue))) {
+		struct object_entry *ent = current->entry;
 		struct island_bitmap *root_marks;
 		struct tree *tree;
 		struct tree_desc desc;
 		struct name_entry entry;
 		khiter_t pos;
 
+		current->queued = 0;
+		display_progress(progress_state, ++processed);
+
 		pos = kh_get_oid_map(island_marks, ent->idx.oid);
 		if (pos >= kh_end(island_marks))
 			continue;
@@ -289,6 +299,8 @@ void resolve_tree_islands(struct repository *r,
 		init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
 		while (tree_entry(&desc, &entry)) {
 			struct object *obj;
+			struct object_entry *child;
+			struct tree_islands_todo *child_todo;
 
 			if (S_ISGITLINK(entry.mode))
 				continue;
@@ -297,15 +309,26 @@ void resolve_tree_islands(struct repository *r,
 			if (!obj)
 				continue;
 
-			set_island_marks(obj, root_marks);
+			if (!set_island_marks(obj, root_marks) ||
+			    !S_ISDIR(entry.mode))
+				continue;
+
+			child = packlist_find(to_pack, &entry.oid);
+			if (!child || oe_type(child) != OBJ_TREE)
+				continue;
+
+			child_todo = &todo[child - to_pack->objects];
+			if (!child_todo->queued) {
+				child_todo->queued = 1;
+				prio_queue_put(&queue, child_todo);
+			}
 		}
 
 		free_tree_buffer(tree);
-
-		display_progress(progress_state, i+1);
 	}
 
 	stop_progress(&progress_state);
+	clear_prio_queue(&queue);
 	free(todo);
 }
 
diff --git a/t/t5320-delta-islands.sh b/t/t5320-delta-islands.sh
index 9b28344a0a..e8544cdfe3 100755
--- a/t/t5320-delta-islands.sh
+++ b/t/t5320-delta-islands.sh
@@ -170,4 +170,95 @@ test_expect_success 'unmatched island core is not fatal' '
 	git -c "pack.islandcore=one" repack -adfi
 '
 
+for order in deep-newer shallow-newer
+do
+	test_expect_success "setup shared subtree ($order)" '
+		git init "shared-subtree-$order" &&
+		(
+			cd "shared-subtree-$order" &&
+			shared=$({ test-tool genrandom seed 10240 && echo 1; } |
+				git hash-object -w --stdin) &&
+			private=$({ test-tool genrandom seed 10240 && echo 12; } |
+				git hash-object -w --stdin) &&
+			nested=$({ test-tool genrandom nested 10240 && echo 1; } |
+				git hash-object -w --stdin) &&
+			private_nested=$({ test-tool genrandom nested 10240 && echo 12; } |
+				git hash-object -w --stdin) &&
+			leaf=$(printf "100644 blob %s\tfile\n" "$nested" |
+				git mktree) &&
+			subtree=$(printf "040000 tree %s\tchild\n100644 blob %s\tfile\n" \
+				"$leaf" "$shared" | git mktree) &&
+			root_one=$({
+				printf "040000 tree %s\ta\n" "$subtree" &&
+				printf "100644 blob %s\tprivate\n" "$private" &&
+				printf "100644 blob %s\tprivate-nested\n" "$private_nested"
+			} | git mktree) &&
+			inner=$(printf "040000 tree %s\tc\n" "$subtree" |
+				git mktree) &&
+			middle=$(printf "040000 tree %s\tb\n" "$inner" |
+				git mktree) &&
+			root_two=$(printf "040000 tree %s\tz\n" "$middle" |
+				git mktree) &&
+			test_tick &&
+			one=$(echo one | git commit-tree "$root_one") &&
+			test_tick &&
+			two=$(echo two | git commit-tree "$root_two") &&
+			if test "$order" = shallow-newer
+			then
+				test_tick &&
+				one=$(echo one | git commit-tree "$root_one")
+			fi &&
+			git update-ref refs/heads/one "$one" &&
+			git update-ref refs/heads/two "$two"
+		)
+	'
+
+	for traversal in --no-path-walk --path-walk
+	do
+		for bitmap in --no-write-bitmap-index --write-bitmap-index
+		do
+			test_expect_success "islands respect shared subtree ($order, $traversal, $bitmap)" '
+				(
+					cd "shared-subtree-$order" &&
+					shared=$(git rev-parse one:a/file) &&
+					nested=$(git rev-parse one:a/child/file) &&
+					: >trace &&
+					GIT_TRACE2_EVENT="$(pwd)/trace" \
+					git -c "pack.island=refs/heads/(.*)" \
+						repack -adfi "$bitmap" "$traversal" 2>err &&
+					test_grep ! "failed to pack objects via path-walk" err &&
+					if test "$traversal" = --path-walk
+					then
+						test_region pack-objects path-walk trace
+					fi &&
+					is_delta_base "$shared" "$ZERO_OID" &&
+					is_delta_base "$nested" "$ZERO_OID"
+				)
+			'
+		done
+
+		test_expect_success "island core orders shared subtree ($order, $traversal)" '
+			(
+				cd "shared-subtree-$order" &&
+				shared=$(git rev-parse one:a/file) &&
+				private=$(git rev-parse one:private) &&
+				: >trace &&
+				GIT_TRACE2_EVENT="$(pwd)/trace" \
+				git -c "pack.island=refs/heads/(.*)" \
+					-c pack.islandCore=two repack -adfi "$traversal" 2>err &&
+				test_grep ! "failed to pack objects via path-walk" err &&
+				if test "$traversal" = --path-walk
+				then
+					test_region pack-objects path-walk trace
+				fi &&
+				printf "%s\n" "$shared" "$private" >expect &&
+				git verify-pack -v .git/objects/pack/*.pack >pack &&
+				cut -d" " -f1 pack |
+				grep -E "^($shared|$private)$" >actual &&
+				test_cmp expect actual
+			)
+		'
+	done
+done
+
 test_done

base-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-30 19:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 19:25 [PATCH] delta-islands: revisit trees with new delta marks Derrick Stolee via GitGitGadget

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox