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

Re: [PATCH] sha1_file: avoid comparison if no packed hash matches the first byte

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Aug 8, 2017, 22:41 UTC
Message-ID
<20170808224114.GD169894@aiede.mtv.corp.google.com>
In-Reply-To
<0c1f898c-46c4-033d-001b-114b17d7d36f@web.de>
René Scharfe wrote:
Show 17 quoted lines
> find_pack_entry_one() uses the fan-out table of pack indexes to find out
> which entries match the first byte of the searched hash and does a
> binary search on this subset of the main index table.
>
> If there are no matching entries then lo and hi will have the same
> value.  The binary search still starts and compares the hash of the
> following entry (which has a non-matching first byte, so won't cause any
> trouble), or whatever comes after the sorted list of entries.
>
> The probability of that stray comparison matching by mistake is low, but
> let's not take any chances and check when entering the binary search
> loop if we're actually done already.
>
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  sha1_file.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
Thanks for a clear explanation.

Sanity checking: is this correct in the sha1[0] == 0 case? In that case, we have lo = 0, hi = the 0th offset from the fanout table. The offsets in the fanout table are defined as "the number of objects in the corresponding pack, the first byte of whose object name is less than or equal to N." So hi == lo would mean there are no objects with id starting with 0, as hoped.

Or in other words, the [lo, hi) interval we're trying to search is indeed a half-open interval.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Previous: René ScharfeNext: Junio C Hamano
Message 2 of 14 in “sha1_file: avoid comparison if no packed hash matches the first byte”
  1. sha1_file: avoid comparison if no packed hash matches the first byteRené Scharfe, Aug 8, 2017
  2. Jonathan NiederAug 8, 2017
  3. Junio C HamanoAug 8, 2017
  4. Jeff KingAug 8, 2017
  5. Jeff KingAug 8, 2017
  6. Junio C HamanoAug 9, 2017
  7. Jeff KingAug 9, 2017
  8. Jeff KingAug 9, 2017
  9. 1/2 sha1_file: drop experimental GIT_USE_LOOKUP searchJeff King, Aug 9, 2017
  10. Junio C HamanoAug 9, 2017
  11. Jeff KingAug 9, 2017
  12. 2/2 hashcmp: use memcmp instead of open-coded loopJeff King, Aug 9, 2017
  13. René ScharfeAug 9, 2017
  14. Jeff KingAug 9, 2017

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.