git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 09/12] builtin/gc: introduce `odb_optimize_required()`

From
Patrick Steinhardt <ps@pks.im>
Date
Jul 13, 2026, 05:52 UTC
Message-ID
<20260713-b4-pks-odb-optimize-v2-9-9c2c3ee94b38@pks.im>
In-Reply-To
<20260713-b4-pks-odb-optimize-v2-0-9c2c3ee94b38@pks.im>

When invoking either git-gc(1) or git-maintenance(1) with the "--auto" flag then we only perform those maintenance tasks that are actually required. This logic is inherently an implementation detail of the object database backend that's in use. But the logic is scattered around multiple different functions, which makes it hard to make the logic pluggable.

Introduce a new `odb_optimize_required()` function that allows us to check these conditions in a generic way.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/gc.c | 160 ++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 92 insertions(+), 68 deletions(-)
diff --git a/builtin/gc.c b/builtin/gc.c
index c8504f4456..e119930adc 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -676,25 +676,84 @@ static void add_repack_incremental_option(struct strvec *args)
 	strvec_push(args, "--no-write-bitmap-index");
 }
 
-static int need_to_gc(struct repository *repo)
+static bool odb_optimize_required(struct object_database *odb,
+				  const struct odb_optimize_options *opts)
 {
-	int gc_auto_threshold = 6700;
-	int gc_auto_pack_limit = 50;
+	switch (opts->strategy) {
+	case ODB_OPTIMIZE_INCREMENTAL: {
+		int gc_auto_threshold = 6700;
+		int gc_auto_pack_limit = 50;
 
-	repo_config_get_int(repo, "gc.auto", &gc_auto_threshold);
-	repo_config_get_int(repo, "gc.autopacklimit", &gc_auto_pack_limit);
+		repo_config_get_int(odb->repo, "gc.auto", &gc_auto_threshold);
+		repo_config_get_int(odb->repo, "gc.autopacklimit", &gc_auto_pack_limit);
 
-	/*
-	 * Setting gc.auto to 0 or negative can disable the
-	 * automatic gc.
-	 */
-	if (gc_auto_threshold <= 0)
-		return 0;
-	if (!too_many_packs(gc_auto_pack_limit) &&
-	    !too_many_loose_objects(gc_auto_threshold))
-		return 0;
+		/*
+		 * Setting gc.auto to 0 or negative can disable the
+		 * automatic gc.
+		 */
+		if (gc_auto_threshold <= 0)
+			return false;
+		if (!too_many_packs(gc_auto_pack_limit) &&
+		    !too_many_loose_objects(gc_auto_threshold))
+			return false;
 
-	return 1;
+		return true;
+	}
+	case ODB_OPTIMIZE_GEOMETRIC: {
+		struct pack_geometry geometry = {
+			.split_factor = 2,
+		};
+		struct pack_objects_args po_args = {
+			.local = 1,
+		};
+		struct existing_packs existing_packs = EXISTING_PACKS_INIT;
+		struct string_list kept_packs = STRING_LIST_INIT_DUP;
+		int auto_value = 100;
+		bool ret;
+
+		repo_config_get_int(odb->repo, "maintenance.geometric-repack.auto",
+				    &auto_value);
+		if (!auto_value)
+			return false;
+		if (auto_value < 0)
+			return true;
+
+		repo_config_get_int(odb->repo, "maintenance.geometric-repack.splitFactor",
+				    &geometry.split_factor);
+
+		existing_packs.repo = odb->repo;
+		existing_packs_collect(&existing_packs, &kept_packs);
+		pack_geometry_init(&geometry, &existing_packs, &po_args);
+		pack_geometry_split(&geometry);
+
+		/*
+		 * When we'd merge at least two packs with one another we always
+		 * perform the repack.
+		 */
+		if (geometry.split) {
+			ret = true;
+			goto out;
+		}
+
+		/*
+		 * Otherwise, we estimate the number of loose objects to determine
+		 * whether we want to create a new packfile or not.
+		 */
+		if (too_many_loose_objects(auto_value)) {
+			ret = true;
+			goto out;
+		}
+
+		ret = false;
+
+	out:
+		existing_packs_release(&existing_packs);
+		pack_geometry_release(&geometry);
+		return ret;
+	}
+	default:
+		BUG("unknown maintenance strategy '%d'", opts->strategy);
+	}
 }
 
 /* return NULL on success, else hostname running the gc */
@@ -1076,13 +1135,19 @@ int cmd_gc(int argc,
 		die(_("failed to parse prune expiry value %s"), cfg.prune_expire);
 
 	if (opts.auto_flag) {
+		struct odb_optimize_options optimize_opts = {
+			.strategy = ODB_OPTIMIZE_INCREMENTAL,
+			OPTIMIZE_FIELDS_FROM_GC_CONFIG(&cfg, 0),
+		};
+
 		if (cfg.detach_auto && opts.detach < 0)
 			opts.detach = 1;
 
 		/*
 		 * Auto-gc should be least intrusive as possible.
 		 */
-		if (!need_to_gc(the_repository) || run_hooks(the_repository, "pre-auto-gc")) {
+		if (!odb_optimize_required(the_repository->objects, &optimize_opts) ||
+		    run_hooks(the_repository, "pre-auto-gc")) {
 			ret = 0;
 			goto out;
 		}
@@ -1379,9 +1444,13 @@ static int maintenance_task_gc_background(struct maintenance_run_opts *opts,
 	return run_command(&child);
 }
 
-static int gc_condition(struct gc_config *cfg UNUSED)
+static int gc_condition(struct gc_config *cfg)
 {
-	return need_to_gc(the_repository);
+	struct odb_optimize_options opts = {
+		.strategy = ODB_OPTIMIZE_INCREMENTAL,
+		OPTIMIZE_FIELDS_FROM_GC_CONFIG(cfg, 0),
+	};
+	return odb_optimize_required(the_repository->objects, &opts);
 }
 
 static int prune_packed(struct maintenance_run_opts *opts)
@@ -1681,58 +1750,13 @@ static int maintenance_task_geometric_repack(struct maintenance_run_opts *opts,
 	return odb_optimize(the_repository->objects, &odb_opts);
 }
 
-static int geometric_repack_auto_condition(struct gc_config *cfg UNUSED)
+static int geometric_repack_auto_condition(struct gc_config *cfg)
 {
-	struct pack_geometry geometry = {
-		.split_factor = 2,
-	};
-	struct pack_objects_args po_args = {
-		.local = 1,
+	struct odb_optimize_options opts = {
+		.strategy = ODB_OPTIMIZE_GEOMETRIC,
+		OPTIMIZE_FIELDS_FROM_GC_CONFIG(cfg, 0),
 	};
-	struct existing_packs existing_packs = EXISTING_PACKS_INIT;
-	struct string_list kept_packs = STRING_LIST_INIT_DUP;
-	int auto_value = 100;
-	int ret;
-
-	repo_config_get_int(the_repository, "maintenance.geometric-repack.auto",
-			    &auto_value);
-	if (!auto_value)
-		return 0;
-	if (auto_value < 0)
-		return 1;
-
-	repo_config_get_int(the_repository, "maintenance.geometric-repack.splitFactor",
-			    &geometry.split_factor);
-
-	existing_packs.repo = the_repository;
-	existing_packs_collect(&existing_packs, &kept_packs);
-	pack_geometry_init(&geometry, &existing_packs, &po_args);
-	pack_geometry_split(&geometry);
-
-	/*
-	 * When we'd merge at least two packs with one another we always
-	 * perform the repack.
-	 */
-	if (geometry.split) {
-		ret = 1;
-		goto out;
-	}
-
-	/*
-	 * Otherwise, we estimate the number of loose objects to determine
-	 * whether we want to create a new packfile or not.
-	 */
-	if (too_many_loose_objects(auto_value)) {
-		ret = 1;
-		goto out;
-	}
-
-	ret = 0;
-
-out:
-	existing_packs_release(&existing_packs);
-	pack_geometry_release(&geometry);
-	return ret;
+	return odb_optimize_required(the_repository->objects, &opts);
 }
 
 typedef int (*maintenance_task_fn)(struct maintenance_run_opts *opts,
-- 
2.55.0.313.g8d093f411d.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 26 of 33 in “odb: make optimizations pluggable”
  1. 00/11 odb: make optimizations pluggablePatrick Steinhardt, Jul 7, 2026
  2. 01/11 odb: run "pre-auto-gc" hook for all maintenance tasksPatrick Steinhardt, Jul 7, 2026
  3. Junio C HamanoJul 7, 2026
  4. Patrick SteinhardtJul 8, 2026
  5. 02/11 builtin/gc: move worktree and rerere tasks before object optimizationsPatrick Steinhardt, Jul 7, 2026
  6. Junio C HamanoJul 7, 2026
  7. 03/11 builtin/gc: extract object database optimizations into separate functionPatrick Steinhardt, Jul 7, 2026
  8. Junio C HamanoJul 7, 2026
  9. 04/11 builtin/gc: make repack arguments self-containedPatrick Steinhardt, Jul 7, 2026
  10. 05/11 builtin/gc: inline config values specific to the "files" backendPatrick Steinhardt, Jul 7, 2026
  11. 06/11 builtin/gc: introduce object database optimization optionsPatrick Steinhardt, Jul 7, 2026
  12. 07/11 builtin/gc: move geometric repacking into `odb_optimize()`Patrick Steinhardt, Jul 7, 2026
  13. 08/11 builtin/gc: introduce `odb_optimize_required()`Patrick Steinhardt, Jul 7, 2026
  14. 09/11 builtin/gc: refactor ODB optimizations to operate on "files" sourcePatrick Steinhardt, Jul 7, 2026
  15. 10/11 builtin/gc: fix signedness issues in ODB-related functionalityPatrick Steinhardt, Jul 7, 2026
  16. 11/11 odb: make optimizations pluggablePatrick Steinhardt, Jul 7, 2026
  17. 00/12 odb: make optimizations pluggablePatrick Steinhardt, Jul 13, 2026
  18. 01/12 t7900: simplify how we check for maintenance tasksPatrick Steinhardt, Jul 13, 2026
  19. 02/12 odb: run "pre-auto-gc" hook for all maintenance tasksPatrick Steinhardt, Jul 13, 2026
  20. 03/12 builtin/gc: move worktree and rerere tasks before object optimizationsPatrick Steinhardt, Jul 13, 2026
  21. 04/12 builtin/gc: extract object database optimizations into separate functionPatrick Steinhardt, Jul 13, 2026
  22. 05/12 builtin/gc: make repack arguments self-containedPatrick Steinhardt, Jul 13, 2026
  23. 06/12 builtin/gc: inline config values specific to the "files" backendPatrick Steinhardt, Jul 13, 2026
  24. 07/12 builtin/gc: introduce object database optimization optionsPatrick Steinhardt, Jul 13, 2026
  25. 08/12 builtin/gc: move geometric repacking into `odb_optimize()`Patrick Steinhardt, Jul 13, 2026
  26. 09/12 builtin/gc: introduce `odb_optimize_required()`Patrick Steinhardt, Jul 13, 2026
  27. 10/12 builtin/gc: refactor ODB optimizations to operate on "files" sourcePatrick Steinhardt, Jul 13, 2026
  28. 11/12 builtin/gc: fix signedness issues in ODB-related functionalityPatrick Steinhardt, Jul 13, 2026
  29. Junio C HamanoJul 13, 2026
  30. Patrick SteinhardtJul 22, 2026
  31. Junio C HamanoJul 26, 2026
  32. 12/12 odb: make optimizations pluggablePatrick Steinhardt, Jul 13, 2026
  33. Junio C HamanoJul 13, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.