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

[PATCH 16/23] pack v4: tree object delta encoding

From
Nicolas Pitre <nico@fluxnic.net>
Date
Aug 27, 2013, 04:26 UTC
Message-ID
<1377577567-27655-17-git-send-email-nico@fluxnic.net>
In-Reply-To
<1377577567-27655-1-git-send-email-nico@fluxnic.net>

In order to be able to quickly walk tree objects, let's encode their "delta" as a range of entries into another tree object.

The encoding allows for the base object to change so multiple base objects can be borrowed from. The code doesn't try to exploit this possibility at the moment though.

The code isn't optimal at the moment as it doesn't consider the case where a borrow sequence could be larger than the local sequence it means to replace.

Signed-off-by: Nicolas Pitre <nico@fluxnic.net>
---
 packv4-create.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 104 insertions(+), 11 deletions(-)
diff --git a/packv4-create.c b/packv4-create.c
index a9e4fb3..744514c 100644
--- a/packv4-create.c
+++ b/packv4-create.c
@@ -409,24 +409,53 @@ bad:
 	return NULL;
 }
 
+static int compare_tree_entries(struct name_entry *e1, struct name_entry *e2)
+{
+	int len1 = tree_entry_len(e1);
+	int len2 = tree_entry_len(e2);
+	int len = len1 < len2 ? len1 : len2;
+	unsigned char c1, c2;
+	int cmp;
+
+	cmp = memcmp(e1->path, e2->path, len);
+	if (cmp)
+		return cmp;
+	c1 = e1->path[len];
+	c2 = e2->path[len];
+	if (!c1 && S_ISDIR(e1->mode))
+		c1 = '/';
+	if (!c2 && S_ISDIR(e2->mode))
+		c2 = '/';
+	return c1 - c2;
+}
+
 /*
  * This converts a canonical tree object buffer into its
  * tightly packed representation using the already populated
  * and sorted tree_path_table dictionary.  The parsing is
  * strict so to ensure the canonical version may always be
  * regenerated and produce the same hash.
+ *
+ * If a delta buffer is provided, we may encode multiple ranges of tree
+ * entries against that buffer.
  */
-void * conv_to_dict_tree(void *buffer, unsigned long *psize)
+void * conv_to_dict_tree(void *buffer, unsigned long *psize,
+			 void *delta, unsigned long delta_size,
+			 const unsigned char *delta_sha1)
 {
 	unsigned long size = *psize;
 	unsigned char *in, *out, *end;
-	struct tree_desc desc;
-	struct name_entry name_entry;
+	struct tree_desc desc, delta_desc;
+	struct name_entry name_entry, delta_entry;
 	int nb_entries;
+	unsigned int delta_start, delta_pos = 0, delta_match = 0, first_delta = 1;
 
 	if (!size)
 		return NULL;
 
+	if (!delta_size)
+		delta = NULL;
+
 	/*
 	 * We can't make sure the result will always be smaller.
 	 * The smallest possible entry is "0 x\0<40 byte SHA1>"
@@ -449,21 +478,85 @@ void * conv_to_dict_tree(void *buffer, unsigned long *psize)
 	out = add_number(out, nb_entries);
 
 	init_tree_desc(&desc, in, size);
+	if (delta) {
+		init_tree_desc(&delta_desc, delta, delta_size);
+		if (!tree_entry(&delta_desc, &delta_entry))
+			delta = NULL;
+	}
+
 	while (tree_entry(&desc, &name_entry)) {
-		int pathlen = tree_entry_len(&name_entry);
-		int index = dict_add_entry(tree_path_table, name_entry.mode,
-					   name_entry.path, pathlen);
-		if (index < 0) {
-			error("missing tree dict entry");
-			free(buffer);
-			return NULL;
+		int pathlen, index;
+
+		/*
+		 * Try to match entries against our delta object.
+		 */
+		if (delta) {
+			int ret;
+
+			do {
+				ret = compare_tree_entries(&name_entry, &delta_entry);
+				if (ret < 1)
+					break;
+				if (tree_entry(&delta_desc, &delta_entry))
+					delta_pos++;
+				else
+					delta = NULL;
+			} while (delta);
+
+			if (ret == 0) {
+				if (!delta_match)
+					delta_start = delta_pos;
+				delta_match++;
+				continue;
+			}
+		}
+
+		if (delta_match) {
+			/*
+			 * Let's write a sequence indicating we're copying
+			 * entries from another object:
+			 *
+			 * 0 + start_entry + nr_entries + object_ref
+			 *
+			 * However, if object_ref is the same as the
+			 * preceeding one, we can omit it and save some
+			 * more space, especially if that ends up being a
+			 * full sha1 reference.  Let's steal the LSB
+			 * of delta_start for that purpose.
+			 */
+			if (end - out < (first_delta ? 48 : 7)) {
+				unsigned long sofar = out - (unsigned char *)buffer;
+				buffer = xrealloc(buffer, sofar + 48);
+				out = (unsigned char *)buffer + sofar;
+				end = out + 48;
+			}
+
+			delta_start <<= 1;
+			delta_start |= first_delta;
+			out = add_number(out, 0);
+			out = add_number(out, delta_start);
+			out = add_number(out, delta_match);
+			if (first_delta)
+				out = add_sha1_ref(out, delta_sha1);
+			delta_match = 0;
+			first_delta = 0;
 		}
+
 		if (end - out < 45) {
 			unsigned long sofar = out - (unsigned char *)buffer;
 			buffer = xrealloc(buffer, sofar + 45);
 			out = (unsigned char *)buffer + sofar;
 			end = out + 45;
 		}
+
+		pathlen = tree_entry_len(&name_entry);
+		index = dict_add_entry(tree_path_table, name_entry.mode,
+				       name_entry.path, pathlen);
+		if (index < 0) {
+			error("missing tree dict entry");
+			free(buffer);
+			return NULL;
+		}
 		out = add_number(out, index);
 		out = add_sha1_ref(out, name_entry.sha1);
 	}
@@ -777,7 +870,7 @@ static off_t packv4_write_object(struct sha1file *f, struct packed_git *p,
 		result = conv_to_dict_commit(src, &size);
 		break;
 	case OBJ_TREE:
-		result = conv_to_dict_tree(src, &size);
+		result = conv_to_dict_tree(src, &size, NULL, 0, NULL);
 		break;
 	default:
 		die("unexpected object type %d", type);
-- 
1.8.4.22.g54757b7
Previous: Nicolas PitreNext: Nicolas Pitre
Message 39 of 83 in “Preliminary pack v4 support”
  1. 00/23 Preliminary pack v4 supportNicolas Pitre, Aug 27, 2013
  2. 01/23 pack v4: initial pack dictionary structure and codeNicolas Pitre, Aug 27, 2013
  3. Junio C HamanoAug 27, 2013
  4. Nicolas PitreAug 27, 2013
  5. 02/23 export packed_object_info()Nicolas Pitre, Aug 27, 2013
  6. 03/23 pack v4: scan tree objectsNicolas Pitre, Aug 27, 2013
  7. 04/23 pack v4: add tree entry mode support to dictionary entriesNicolas Pitre, Aug 27, 2013
  8. 05/23 pack v4: add commit object parsingNicolas Pitre, Aug 27, 2013
  9. Junio C HamanoAug 27, 2013
  10. Nicolas PitreAug 27, 2013
  11. Junio C HamanoAug 27, 2013
  12. 06/23 pack v4: split the object list and dictionary creationNicolas Pitre, Aug 27, 2013
  13. 07/23 pack v4: move to struct pack_idx_entry and get rid of our own struct idx_entryNicolas Pitre, Aug 27, 2013
  14. 08/23 pack v4: basic references encodingNicolas Pitre, Aug 27, 2013
  15. Junio C HamanoAug 27, 2013
  16. Nicolas PitreAug 27, 2013
  17. 09/23 pack v4: commit object encodingNicolas Pitre, Aug 27, 2013
  18. Junio C HamanoAug 27, 2013
  19. Nicolas PitreAug 27, 2013
  20. Nicolas PitreAug 27, 2013
  21. Junio C HamanoAug 27, 2013
  22. Nicolas PitreAug 27, 2013
  23. Duy NguyenSep 2, 2013
  24. Nicolas PitreSep 3, 2013
  25. Duy NguyenSep 3, 2013
  26. Nicolas PitreSep 5, 2013
  27. 10/23 pack v4: tree object encodingNicolas Pitre, Aug 27, 2013
  28. Junio C HamanoAug 27, 2013
  29. Nicolas PitreAug 27, 2013
  30. 11/23 pack v4: dictionary table outputNicolas Pitre, Aug 27, 2013
  31. 12/23 pack v4: creation codeNicolas Pitre, Aug 27, 2013
  32. Junio C HamanoAug 27, 2013
  33. Nicolas PitreAug 27, 2013
  34. 13/23 pack v4: object headersNicolas Pitre, Aug 27, 2013
  35. 14/23 pack v4: object data copyNicolas Pitre, Aug 27, 2013
  36. Junio C HamanoAug 27, 2013
  37. Nicolas PitreAug 27, 2013
  38. 15/23 pack v4: object writingNicolas Pitre, Aug 27, 2013
  39. 16/23 pack v4: tree object delta encodingNicolas Pitre, Aug 27, 2013
  40. 17/23 pack v4: load delta candidate for encoding tree objectsNicolas Pitre, Aug 27, 2013
  41. 18/23 pack v4: honor pack.compression config optionNicolas Pitre, Aug 27, 2013
  42. 19/23 pack v4: relax commit parsing a bitNicolas Pitre, Aug 27, 2013
  43. 20/23 pack index v3Nicolas Pitre, Aug 27, 2013
  44. 21/23 pack v4: normalize pack name to properly generate the pack index file nameNicolas Pitre, Aug 27, 2013
  45. 22/23 pack v4: add progress displayNicolas Pitre, Aug 27, 2013
  46. 23/23 initial pack index v3 support on the read sideNicolas Pitre, Aug 27, 2013
  47. Duy NguyenAug 31, 2013
  48. Nicolas PitreSep 3, 2013
  49. Duy NguyenSep 3, 2013
  50. Document pack v4 formatNguyễn Thái Ngọc Duy, Aug 27, 2013
  51. Junio C HamanoAug 27, 2013
  52. Nicolas PitreAug 27, 2013
  53. Document pack v4 formatNguyễn Thái Ngọc Duy, Aug 31, 2013
  54. Nicolas PitreSep 3, 2013
  55. Nicolas PitreSep 3, 2013
  56. Duy NguyenSep 3, 2013
  57. Duy NguyenSep 3, 2013
  58. Nicolas PitreSep 5, 2013
  59. Duy NguyenSep 5, 2013
  60. Nicolas PitreSep 5, 2013
  61. Duy NguyenSep 5, 2013
  62. Nicolas PitreSep 5, 2013
  63. Duy NguyenSep 5, 2013
  64. Nicolas PitreSep 5, 2013
  65. Junio C HamanoSep 5, 2013
  66. Nicolas PitreSep 5, 2013
  67. Duy NguyenSep 6, 2013
  68. Nicolas PitreSep 6, 2013
  69. Document pack v4 formatNguyễn Thái Ngọc Duy, Sep 6, 2013
  70. Nicolas PitreSep 6, 2013
  71. Duy NguyenSep 6, 2013
  72. Nicolas PitreSep 6, 2013
  73. Duy NguyenSep 6, 2013
  74. Nicolas PitreSep 6, 2013
  75. Nicolas PitreSep 7, 2013
  76. Nicolas PitreSep 7, 2013
  77. Duy NguyenSep 7, 2013
  78. Junio C HamanoAug 27, 2013
  79. Nicolas PitreAug 27, 2013
  80. Junio C HamanoAug 27, 2013
  81. Duy NguyenAug 28, 2013
  82. Nicolas PitreAug 28, 2013
  83. Duy NguyenAug 28, 2013

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.