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

Re: [PATCH v3 11/21] pack-objects: use bitmaps when packing objects

From
Thomas Rast <tr@thomasrast.ch>
Date
Dec 7, 2013, 15:47 UTC
Message-ID
<87zjock6if.fsf@linux-1gf2.Speedport_W723_V_Typ_A_1_00_098>
In-Reply-To
<20131114124510.GK10757@sigill.intra.peff.net>
This week's nits...

I found this harder to read than the previous patch, but I think it's mostly because the existing code is already a bit tangled. I think the second item below is worth fixing, though.

Jeff King <peff@peff.net> writes:
> +static off_t write_reused_pack(struct sha1file *f)
> +{
> +	uint8_t buffer[8192];

We usually just call this 'unsigned char'. I can see why this would be more portable, but git would already fall apart badly on an architecture where char is not 8 bits.

Show 14 quoted lines
> +	off_t to_write;
> +	int fd;
> +
> +	if (!is_pack_valid(reuse_packfile))
> +		return 0;
> +
> +	fd = git_open_noatime(reuse_packfile->pack_name);
> +	if (fd < 0)
> +		return 0;
> +
> +	if (lseek(fd, sizeof(struct pack_header), SEEK_SET) == -1) {
> +		close(fd);
> +		return 0;
> +	}

You do an error return if any of the syscalls in this routine fails, but there is only one caller and it immediately dies:

} + packfile_size = write_reused_pack(f); } + if (!packfile_size) } + die_errno("failed to re-use existing pack");

So if you just died here, when the error happens, you could take the chance to tell the user _which_ syscall failed.

Show 12 quoted lines
> +
> +	if (reuse_packfile_offset < 0)
> +		reuse_packfile_offset = reuse_packfile->pack_size - 20;
> +
> +	to_write = reuse_packfile_offset - sizeof(struct pack_header);
> +
> +	while (to_write) {
> +		int read_pack = xread(fd, buffer, sizeof(buffer));
> +
> +		if (read_pack <= 0) {
> +			close(fd);
> +			return 0;

Similar to the above, but this one may also clobber the 'errno' during close(), which can lead to misleading messages.

Show 6 quoted lines
> +		}
> +
> +		if (read_pack > to_write)
> +			read_pack = to_write;
> +
> +		sha1write(f, buffer, read_pack);
Not your fault, but sha1write() is an odd function -- it purportedly is
  int sha1write(struct sha1file *f, const void *buf, unsigned int count);

but it can only return 0. This goes back all the way to c38138c (git-pack-objects: write the pack files with a SHA1 csum, 2005-06-26).

Show 7 quoted lines
> +		to_write -= read_pack;
> +	}
> +
> +	close(fd);
> +	written += reuse_packfile_objects;
> +	return reuse_packfile_offset - sizeof(struct pack_header);
> +}
[...]
Show 6 quoted lines
> -static int add_object_entry(const unsigned char *sha1, enum object_type type,
> -			    const char *name, int exclude)
> +static int add_object_entry_1(const unsigned char *sha1, enum object_type type,
> +			      int flags, uint32_t name_hash,
> +			      struct packed_git *found_pack, off_t found_offset)
>  {
[...]
Show 40 quoted lines
> -	for (p = packed_git; p; p = p->next) {
> -		off_t offset = find_pack_entry_one(sha1, p);
> -		if (offset) {
> -			if (!found_pack) {
> -				if (!is_pack_valid(p)) {
> -					warning("packfile %s cannot be accessed", p->pack_name);
> -					continue;
> +	if (!found_pack) {
> +		for (p = packed_git; p; p = p->next) {
> +			off_t offset = find_pack_entry_one(sha1, p);
> +			if (offset) {
> +				if (!found_pack) {
> +					if (!is_pack_valid(p)) {
> +						warning("packfile %s cannot be accessed", p->pack_name);
> +						continue;
> +					}
> +					found_offset = offset;
> +					found_pack = p;
>  				}
> -				found_offset = offset;
> -				found_pack = p;
> +				if (exclude)
> +					break;
> +				if (incremental)
> +					return 0;
> +				if (local && !p->pack_local)
> +					return 0;
> +				if (ignore_packed_keep && p->pack_local && p->pack_keep)
> +					return 0;
>  			}
> -			if (exclude)
> -				break;
> -			if (incremental)
> -				return 0;
> -			if (local && !p->pack_local)
> -				return 0;
> -			if (ignore_packed_keep && p->pack_local && p->pack_keep)
> -				return 0;
>  		}
>  	}

This function makes my head spin, and you're indenting it yet another level.

If it's not too much work, can you split it into the three parts that it really is? IIUC it boils down to

  do we have this already?
      possibly apply 'exclude', then return
  are we coming from a call path that doesn't tell us which pack to take
  it from?
      find _all_ instances in packs
      check if any of them are local .keep packs
          if so, return
  construct a packlist entry to taste
Show 22 quoted lines
>  	entry = packlist_alloc(&to_pack, sha1, index_pos);
> -	entry->hash = hash;
> +	entry->hash = name_hash;
>  	if (type)
>  		entry->type = type;
>  	if (exclude)
>  		entry->preferred_base = 1;
>  	else
>  		nr_result++;
> +
> +	if (flags & OBJECT_ENTRY_NO_TRY_DELTA)
> +		entry->no_try_delta = 1;
> +
>  	if (found_pack) {
>  		entry->in_pack = found_pack;
>  		entry->in_pack_offset = found_offset;
> @@ -859,10 +932,21 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,
>  
>  	display_progress(progress_state, to_pack.nr_objects);
>  
> +	return 1;
> +}
-- 
Thomas Rast
tr@thomasrast.ch
Previous: Jeff KingNext: Jeff King
Message 26 of 55 in “pack bitmaps”
  1. 0/21 pack bitmapsJeff King, Nov 14, 2013
  2. 01/21 sha1write: make buffer const-correctJeff King, Nov 14, 2013
  3. 02/21 revindex: Export new APIsJeff King, Nov 14, 2013
  4. 03/21 pack-objects: Refactor the packing listJeff King, Nov 14, 2013
  5. 04/21 pack-objects: factor out name_hashJeff King, Nov 14, 2013
  6. 05/21 revision: allow setting custom limiter functionJeff King, Nov 14, 2013
  7. 06/21 sha1_file: export `git_open_noatime`Jeff King, Nov 14, 2013
  8. 07/21 compat: add endianness helpersJeff King, Nov 14, 2013
  9. 08/21 ewah: compressed bitmap implementationJeff King, Nov 14, 2013
  10. 09/21 documentation: add documentation for the bitmap formatJeff King, Nov 14, 2013
  11. 10/21 pack-bitmap: add support for bitmap indexesJeff King, Nov 14, 2013
  12. Thomas RastNov 24, 2013
  13. Document khashThomas Rast, Nov 25, 2013
  14. Jeff KingNov 28, 2013
  15. Karsten BleesNov 27, 2013
  16. Jeff KingNov 28, 2013
  17. Karsten BleesDec 3, 2013
  18. Jeff KingDec 3, 2013
  19. Karsten BleesDec 7, 2013
  20. Thomas RastNov 29, 2013
  21. Jeff KingDec 2, 2013
  22. Junio C HamanoDec 2, 2013
  23. Jeff KingDec 2, 2013
  24. Junio C HamanoDec 2, 2013
  25. 11/21 pack-objects: use bitmaps when packing objectsJeff King, Nov 14, 2013
  26. Thomas RastDec 7, 2013
  27. Jeff KingDec 21, 2013
  28. 12/21 rev-list: add bitmap mode to speed up object listsJeff King, Nov 14, 2013
  29. Thomas RastDec 7, 2013
  30. 13/21 pack-objects: implement bitmap writingJeff King, Nov 14, 2013
  31. Thomas RastDec 7, 2013
  32. Jeff KingDec 21, 2013
  33. 14/21 repack: stop using magic number for ARRAY_SIZE(exts)Jeff King, Nov 14, 2013
  34. Thomas RastDec 7, 2013
  35. 15/21 repack: turn exts array into array-of-structJeff King, Nov 14, 2013
  36. Thomas RastDec 7, 2013
  37. 16/21 repack: handle optional files created by pack-objectsJeff King, Nov 14, 2013
  38. Thomas RastDec 7, 2013
  39. 17/21 repack: consider bitmaps when performing repacksJeff King, Nov 14, 2013
  40. Thomas RastDec 7, 2013
  41. 18/21 count-objects: recognize .bitmap in garbage-checkingJeff King, Nov 14, 2013
  42. Thomas RastDec 7, 2013
  43. 19/21 t: add basic bitmap functionality testsJeff King, Nov 14, 2013
  44. Thomas RastDec 7, 2013
  45. Jeff KingDec 21, 2013
  46. 20/21 t/perf: add tests for pack bitmapsJeff King, Nov 14, 2013
  47. Thomas RastDec 7, 2013
  48. Jeff KingDec 21, 2013
  49. 21/21 pack-bitmap: implement optional name_hash cacheJeff King, Nov 14, 2013
  50. Thomas RastDec 7, 2013
  51. Ramsay JonesNov 14, 2013
  52. Jeff KingNov 14, 2013
  53. Ramsay JonesNov 14, 2013
  54. Ramsay JonesNov 18, 2013
  55. Thomas RastNov 16, 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.