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

Re: Distribution of longest common hash prefixes

From
Randal L. Schwartz <merlyn@stonehenge.com>
Date
Apr 2, 2007, 17:17 UTC
Message-ID
<86y7laitlz.fsf@blue.stonehenge.com>
In-Reply-To
<Pine.LNX.4.64.0704020938470.6730@woody.linux-foundation.org>
>>>>> "Linus" == Linus Torvalds <torvalds@linux-foundation.org> writes:
Linus> No yay yet.. That counts hex digits, not bits.

I thought the goal was to figure out how long (on the average) you had to give a SHA1 to be "unique".

But even that's wrong, because of the following:

CAFEFEED357 DEADBEEF123 DEADBEEF456 DEADBEEF467 DEADBEEF478

for that sequence, I'd count 0, 8, 9, 9 when in fact, it should be 8, 9, 9, 9. It's not the items in common with the previous value... it's the longer of the items in common with the string on either side. The easiest way for that would be to use a 3-item window:

git-rev-list --objects HEAD | sort | perl -lne '
  substr($_, 40) = "";
  if (defined $p) {
    ($p ^ $_) =~ /^(\0*)/;
    $common = length $1;
    if (defined $pcommon) {
      $count[$pcommon > $common ? $pcommon : $common]++;
    }
  }
  $p = $_;
  $pcommon = $common;
  END { print "$_: $count[$_]" for 0..$#count }
'

this also fixes the bug where I compare the first line to nothing. With this, I get (on git.git):

    0: 
    1: 
    2: 6
    3: 21153
    4: 15008
    5: 1232
    6: 90
    7: 
    8: 2

which now makes sense. There are 2 items that need 9 hex chars to be unique.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Previous: Linus TorvaldsNext: Randal L. Schwartz
Message 5 of 20 in “Distribution of longest common hash prefixes”
  1. Peter EriksenApr 2, 2007
  2. Linus TorvaldsApr 2, 2007
  3. Randal L. SchwartzApr 2, 2007
  4. Linus TorvaldsApr 2, 2007
  5. Randal L. SchwartzApr 2, 2007
  6. Randal L. SchwartzApr 2, 2007
  7. James CloosApr 3, 2007
  8. Randal L. SchwartzApr 3, 2007
  9. Shawn O. PearceApr 3, 2007
  10. Linus TorvaldsApr 3, 2007
  11. Junio C HamanoApr 3, 2007
  12. Linus TorvaldsApr 3, 2007
  13. Nicolas PitreApr 3, 2007
  14. Junio C HamanoApr 3, 2007
  15. Nicolas PitreApr 3, 2007
  16. Olivier GalibertApr 3, 2007
  17. Linus TorvaldsApr 3, 2007
  18. James CloosApr 4, 2007
  19. James CloosApr 2, 2007
  20. Peter EriksenApr 2, 2007

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.