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

[PATCH v10 6/6] pack-redundant: consistent sort method

From
Jiang Xin <worldhello.net@gmail.com>
Date
Feb 2, 2019, 13:30 UTC
Message-ID
<20190202133017.1039-7-worldhello.net@gmail.com>
In-Reply-To
<CAPig+cQh5TDKVaDi0gg9LZTo1Og_Qw6S2sH9cPABR9q05gEUfg@mail.gmail.com>
From: Jiang Xin <zhiyou.jx@alibaba-inc.com>

SZEDER reported that test case t5323 has different test result on MacOS. This is because `cmp_pack_list_reverse` cannot give identical result when two pack being sorted has the same size of remaining_objects.

Changes to the sorting function will make consistent test result for t5323.

The new algorithm to find redundant packs is a trade-off to save memory resources, and the result of it may be different with old one, and may be not the best result sometimes. Update t5323 for the new algorithm.

Reported-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/pack-redundant.c  | 24 ++++++++++++++++--------
 t/t5323-pack-redundant.sh | 18 +++++++++---------
 2 files changed, 25 insertions(+), 17 deletions(-)
diff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c
index 15cdf233c4..29ff5e99cb 100644
--- a/builtin/pack-redundant.c
+++ b/builtin/pack-redundant.c
@@ -33,6 +33,7 @@ static struct pack_list {
 	struct packed_git *pack;
 	struct llist *unique_objects;
 	struct llist *remaining_objects;
+	size_t all_objects_size;
 } *local_packs = NULL, *altodb_packs = NULL;
 
 static struct llist_item *free_nodes;
@@ -340,19 +341,25 @@ static inline off_t pack_set_bytecount(struct pack_list *pl)
 	return ret;
 }
 
-static int cmp_pack_list_reverse(const void *a, const void *b)
+static int cmp_remaining_objects(const void *a, const void *b)
 {
 	struct pack_list *pl_a = *((struct pack_list **)a);
 	struct pack_list *pl_b = *((struct pack_list **)b);
-	size_t sz_a = pl_a->remaining_objects->size;
-	size_t sz_b = pl_b->remaining_objects->size;
 
-	if (sz_a == sz_b)
-		return 0;
-	else if (sz_a < sz_b)
+	if (pl_a->remaining_objects->size == pl_b->remaining_objects->size) {
+		/* have the same remaining_objects, big pack first */
+		if (pl_a->all_objects_size == pl_b->all_objects_size)
+			return 0;
+		else if (pl_a->all_objects_size < pl_b->all_objects_size)
+			return 1;
+		else
+			return -1;
+	} else if (pl_a->remaining_objects->size < pl_b->remaining_objects->size) {
+		/* sort by remaining objects, more objects first */
 		return 1;
-	else
+	} else {
 		return -1;
+	}
 }
 
 /* Sort pack_list, greater size of remaining_objects first */
@@ -370,7 +377,7 @@ static void sort_pack_list(struct pack_list **pl)
 	for (n = 0, p = *pl; p; p = p->next)
 		ary[n++] = p;
 
-	QSORT(ary, n, cmp_pack_list_reverse);
+	QSORT(ary, n, cmp_remaining_objects);
 
 	/* link them back again */
 	for (i = 0; i < n - 1; i++)
@@ -511,6 +518,7 @@ static struct pack_list * add_pack(struct packed_git *p)
 		llist_insert_back(l.remaining_objects, (const struct object_id *)(base + off));
 		off += step;
 	}
+	l.all_objects_size = l.remaining_objects->size;
 	l.unique_objects = NULL;
 	if (p->pack_local)
 		return pack_list_insert(&local_packs, &l);
diff --git a/t/t5323-pack-redundant.sh b/t/t5323-pack-redundant.sh
index 3e62e8663f..384b244314 100755
--- a/t/t5323-pack-redundant.sh
+++ b/t/t5323-pack-redundant.sh
@@ -165,15 +165,15 @@ test_expect_success 'master: no redundant for pack 1, 2, 3' '
 #         | T A B C D E F G H I J K L M N O P Q R
 #     ----+--------------------------------------
 #     P1  | x x x x x x x                       x
-#     P2* |     ! ! ! !   ! ! !
-#     P3  |             x     x x x x x
+#     P2  |     x x x x   x x x
+#     P3* |             !     ! ! ! ! !
 #     P4  |                     x x x x     x
 #     P5  |               x x           x x
 #     ----+--------------------------------------
 #     ALL | x x x x x x x x x x x x x x x x x   x
 #
 #############################################################################
-test_expect_failure 'master: one of pack-2/pack-3 is redundant (failed on Mac)' '
+test_expect_success 'master: one of pack-2/pack-3 is redundant' '
 	create_pack_in "$master_repo" P4 <<-EOF &&
 		$J
 		$K
@@ -190,7 +190,7 @@ test_expect_failure 'master: one of pack-2/pack-3 is redundant (failed on Mac)'
 	(
 		cd "$master_repo" &&
 		cat >expect <<-EOF &&
-			P2:$P2
+			P3:$P3
 			EOF
 		git pack-redundant --all >out &&
 		format_packfiles <out >actual &&
@@ -214,7 +214,7 @@ test_expect_failure 'master: one of pack-2/pack-3 is redundant (failed on Mac)'
 #     ALL | x x x x x x x x x x x x x x x x x x x
 #
 #############################################################################
-test_expect_failure 'master: pack 2, 4, and 6 are redundant (failed on Mac)' '
+test_expect_success 'master: pack 2, 4, and 6 are redundant' '
 	create_pack_in "$master_repo" P6 <<-EOF &&
 		$N
 		$O
@@ -254,7 +254,7 @@ test_expect_failure 'master: pack 2, 4, and 6 are redundant (failed on Mac)' '
 #     ALL | x x x x x x x x x x x x x x x x x x x
 #
 #############################################################################
-test_expect_failure 'master: pack-8 (subset of pack-1) is also redundant (failed on Mac)' '
+test_expect_success 'master: pack-8 (subset of pack-1) is also redundant' '
 	create_pack_in "$master_repo" P8 <<-EOF &&
 		$A
 		EOF
@@ -281,7 +281,7 @@ test_expect_success 'master: clean loose objects' '
 	)
 '
 
-test_expect_failure 'master: remove redundant packs and pass fsck (failed on Mac)' '
+test_expect_success 'master: remove redundant packs and pass fsck' '
 	(
 		cd "$master_repo" &&
 		git pack-redundant --all | xargs rm &&
@@ -301,7 +301,7 @@ test_expect_success 'setup shared.git' '
 	)
 '
 
-test_expect_failure 'shared: all packs are redundant, but no output without --alt-odb (failed on Mac)' '
+test_expect_success 'shared: all packs are redundant, but no output without --alt-odb' '
 	(
 		cd "$shared_repo" &&
 		git pack-redundant --all >out &&
@@ -334,7 +334,7 @@ test_expect_failure 'shared: all packs are redundant, but no output without --al
 #     ALL | x x x x x x x x x x x x x x x x x x x
 #
 #############################################################################
-test_expect_failure 'shared: show redundant packs in stderr for verbose mode (failed on Mac)' '
+test_expect_success 'shared: show redundant packs in stderr for verbose mode' '
 	(
 		cd "$shared_repo" &&
 		cat >expect <<-EOF &&
-- 
2.20.1.103.ged0fc2ca7b
Previous: Jiang XinNext: Jiang Xin
Message 22 of 83 in “pack-redundant: new algorithm to find min packs”
  1. 1/2 pack-redundant: new algorithm to find min packsJiang Xin, Dec 18, 2018
  2. 2/2 pack-redundant: remove unused functionsJiang Xin, Dec 18, 2018
  3. 0/3 pack-redundant: new algorithm to find min packsJiang Xin, Dec 19, 2018
  4. 0/3 pack-redundant: new algorithm to find min packsJiang Xin, Jan 2, 2019
  5. 1/3 t5323: test cases for git-pack-redundantJiang Xin, Jan 2, 2019
  6. SZEDER GáborJan 9, 2019
  7. SZEDER GáborJan 9, 2019
  8. 0/5 pack-redundant: new algorithm to find min packsJiang Xin, Jan 10, 2019
  9. 0/5 pack-redundant: new algorithm to find min packsJiang Xin, Jan 12, 2019
  10. 0/6 pack-redundant: new algorithm to find min packsJiang Xin, Jan 30, 2019
  11. 0/6 pack-redundant: new algorithm to find min packsJiang Xin, Feb 1, 2019
  12. 1/6 t5323: test cases for git-pack-redundantJiang Xin, Feb 1, 2019
  13. Eric SunshineFeb 1, 2019
  14. Junio C HamanoFeb 1, 2019
  15. Eric SunshineFeb 1, 2019
  16. 0/6 pack-redundant: new algorithm to find min packsJiang Xin, Feb 2, 2019
  17. 1/6 t5323: test cases for git-pack-redundantJiang Xin, Feb 2, 2019
  18. 2/6 pack-redundant: delay creation of unique_objectsJiang Xin, Feb 2, 2019
  19. 3/6 pack-redundant: delete redundant codeJiang Xin, Feb 2, 2019
  20. 4/6 pack-redundant: new algorithm to find min packsJiang Xin, Feb 2, 2019
  21. 5/6 pack-redundant: rename pack_list.all_objectsJiang Xin, Feb 2, 2019
  22. 6/6 pack-redundant: consistent sort methodJiang Xin, Feb 2, 2019
  23. 2/6 pack-redundant: delay creation of unique_objectsJiang Xin, Feb 1, 2019
  24. 3/6 pack-redundant: delete redundant codeJiang Xin, Feb 1, 2019
  25. 4/6 pack-redundant: new algorithm to find min packsJiang Xin, Feb 1, 2019
  26. 5/6 pack-redundant: rename pack_list.all_objectsJiang Xin, Feb 1, 2019
  27. 6/6 pack-redundant: consistent sort methodJiang Xin, Feb 1, 2019
  28. 1/6 t5323: test cases for git-pack-redundantJiang Xin, Jan 30, 2019
  29. Junio C HamanoJan 31, 2019
  30. Jiang XinFeb 1, 2019
  31. Eric SunshineFeb 1, 2019
  32. Jiang XinFeb 1, 2019
  33. Jiang XinFeb 1, 2019
  34. Jiang XinFeb 1, 2019
  35. 2/6 pack-redundant: delay creation of unique_objectsJiang Xin, Jan 30, 2019
  36. 3/6 pack-redundant: new algorithm to find min packsJiang Xin, Jan 30, 2019
  37. Junio C HamanoJan 31, 2019
  38. Jiang XinFeb 1, 2019
  39. 4/6 pack-redundant: remove unused functionsJiang Xin, Jan 30, 2019
  40. 1/1 pack-redundant: delete redundant code16657101987@163.com, Jan 30, 2019
  41. 5/6 pack-redundant: rename pack_list.all_objectsJiang Xin, Jan 30, 2019
  42. 6/6 pack-redundant: consistent sort methodJiang Xin, Jan 30, 2019
  43. 1/5 t5323: test cases for git-pack-redundantJiang Xin, Jan 12, 2019
  44. 2/5 pack-redundant: new algorithm to find min packsJiang Xin, Jan 12, 2019
  45. 3/5 pack-redundant: remove unused functionsJiang Xin, Jan 12, 2019
  46. 4/5 pack-redundant: rename pack_list.all_objectsJiang Xin, Jan 12, 2019
  47. 5/5 pack-redundant: consistent sort methodJiang Xin, Jan 12, 2019
  48. 1/5 t5323: test cases for git-pack-redundantJiang Xin, Jan 10, 2019
  49. Junio C HamanoJan 10, 2019
  50. Jiang XinJan 11, 2019
  51. Junio C HamanoJan 11, 2019
  52. 2/5 pack-redundant: new algorithm to find min packsJiang Xin, Jan 10, 2019
  53. SZEDER GáborJan 11, 2019
  54. 3/5 pack-redundant: rename pack_list.all_objectsJiang Xin, Jan 10, 2019
  55. 4/5 pack-redundant: consistent sort methodJiang Xin, Jan 10, 2019
  56. SZEDER GáborJan 10, 2019
  57. 5/5 pack-redundant: remove unused functionsJiang Xin, Jan 10, 2019
  58. Jiang XinJan 10, 2019
  59. Johannes SixtJan 10, 2019
  60. SZEDER GáborJan 10, 2019
  61. Torsten BögershausenJan 10, 2019
  62. Junio C HamanoJan 10, 2019
  63. 1/1 test-lint: sed -E (or -a, -l) are not portabletboegi@web.de, Jan 15, 2019
  64. Eric SunshineJan 15, 2019
  65. Ævar Arnfjörð BjarmasonJan 16, 2019
  66. 1/1 test-lint: Only use only sed [-n] [-e command] [-f command_file]tboegi@web.de, Jan 20, 2019
  67. Junio C HamanoJan 22, 2019
  68. Torsten BögershausenJan 22, 2019
  69. Eric SunshineJan 22, 2019
  70. Torsten BögershausenJan 23, 2019
  71. Junio C HamanoJan 23, 2019
  72. Torsten BögershausenJan 25, 2019
  73. Junio C HamanoJan 27, 2019
  74. 2/3 pack-redundant: new algorithm to find min packsJiang Xin, Jan 2, 2019
  75. 3/3 pack-redundant: remove unused functionsJiang Xin, Jan 2, 2019
  76. 1/1 pack-redundant: remove unused functions16657101987@163.com, Jan 8, 2019
  77. 0/1 pack-redundant: remove unused functions16657101987@163.com, Jan 8, 2019
  78. Junio C HamanoJan 8, 2019
  79. 16657101987@163.comJan 9, 2019
  80. 0/1 pack-redundant: remove unused functions16657101987@163.com, Jan 8, 2019
  81. 1/3 t5322: test cases for git-pack-redundantJiang Xin, Dec 19, 2018
  82. 2/3 pack-redundant: new algorithm to find min packsJiang Xin, Dec 19, 2018
  83. 3/3 pack-redundant: remove unused functionsJiang Xin, Dec 19, 2018

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.