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

Re: [PATCH] Handle deltified object correctly in git-*-pull family.

From
Nicolas Pitre <nico@cam.org>
Date
Jun 2, 2005, 21:31 UTC
Message-ID
<Pine.LNX.4.63.0506021713330.17354@localhost.localdomain>
In-Reply-To
<7v3bs07fmu.fsf@assigned-by-dhcp.cox.net>
On Thu, 2 Jun 2005, Junio C Hamano wrote:
Show 5 quoted lines
> The initial parts of each retrieved SHA1 file is inflated and
> inspected to see if it is deltified, and its base object is
> asked from the remote side when it is.  Since this partial
> inflation and inspection has a small performance hit, it can
> optionally be skipped by giving -d flag to git-*-pull commands.
It is still way more expensive than it could.
Show 18 quoted lines
> diff --git a/sha1_file.c b/sha1_file.c
> --- a/sha1_file.c
> +++ b/sha1_file.c
> @@ -325,7 +325,13 @@ void *unpack_sha1_rest(z_stream *stream,
>  	int bytes = strlen(buffer) + 1;
>  	char *buf = xmalloc(1+size);
>  
> -	memcpy(buf, buffer + bytes, stream->total_out - bytes);
> +	/* (stream->total_out - bytes) is what we already have.  The
> +	 * caller could be asking for something smaller than that.
> +	 */
> +	if (size < stream->total_out - bytes)
> +		memcpy(buf, buffer + bytes, size);
> +	else
> +		memcpy(buf, buffer + bytes, stream->total_out - bytes);
>  	bytes = stream->total_out - bytes;
>  	if (bytes < size) {
>  		stream->next_out = buf + bytes;
This hunk is completely unneeded.
Show 11 quoted lines
> @@ -401,6 +407,41 @@ void * unpack_sha1_file(void *map, unsig
>  	return unpack_sha1_rest(&stream, hdr, *size);
>  }
>  
> +int sha1_delta_base(const unsigned char *sha1, unsigned char *base_sha1)
> +{
> +	int ret;
> +	unsigned long mapsize, size;
> +	void *map;
> +	z_stream stream;
> +	char hdr[1024], type[20];

Don't make hdr 1024 bytes long. If you do so unpack_sha1_header() will uncompress up to 1024 bytes which is way above required. Instead, consider a value of say 64 which is plenty sufficient (10 for the type string, another 10 for the size, 20 for the reference sha1 and another 10 for the beginning of the delta data that must include the size, and the rest for good measure).

Show 15 quoted lines
> +	void *delta_data_head;
> +
> +	map = map_sha1_file(sha1, &mapsize);
> +	if (!map)
> +		return -1;
> +	ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
> +	if (ret < Z_OK || parse_sha1_header(hdr, type, &size) < 0) {
> +		ret = -1;
> +		goto out;
> +	}
> +	if (strcmp(type, "delta")) {
> +		ret = 0;
> +		goto out;
> +	}
> +	delta_data_head = unpack_sha1_rest(&stream, hdr, 20);

Here you don't need to call unpack_sha1_rest() at all which would call xmalloc and another memcpy needlessly. Instead, just use:

	memcpy(base_sha1, hdr + strlen(hdr) + 1, 20);
and you're done.  No need to call an extra free() either.
And maybe this function should live in delta.c instead?
Nicolas
Previous: Junio C HamanoNext: Nicolas Pitre
Message 5 of 20 in “diff: mode bits fixes”
  1. diff: mode bits fixesJunio C Hamano, Jun 1, 2005
  2. Handle deltified object correctly in git-*-pull family.Junio C Hamano, Jun 2, 2005
  3. Linus TorvaldsJun 2, 2005
  4. Handle deltified object correctly in git-*-pull family.Junio C Hamano, Jun 2, 2005
  5. Nicolas PitreJun 2, 2005
  6. Nicolas PitreJun 2, 2005
  7. 1/2 Handle deltified object correctly in git-*-pull family.Junio C Hamano, Jun 2, 2005
  8. Linus TorvaldsJun 2, 2005
  9. 2/2 Find size of SHA1 object without inflating everything.Junio C Hamano, Jun 2, 2005
  10. Find size of SHA1 object without inflating everything.Junio C Hamano, Jun 2, 2005
  11. Linus TorvaldsJun 2, 2005
  12. Junio C HamanoJun 2, 2005
  13. McMullan, JasonJun 2, 2005
  14. Junio C HamanoJun 2, 2005
  15. Use correct U*MAX.Junio C Hamano, Jun 2, 2005
  16. Petr BaudisJun 3, 2005
  17. Junio C HamanoJun 3, 2005
  18. Petr BaudisJun 4, 2005
  19. Junio C HamanoJun 4, 2005
  20. Find size of SHA1 object without inflating everything.Junio C Hamano, Jun 2, 2005

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.