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

Re: Hash Tables

From
Thomas Rast <trast@student.ethz.ch>
Date
Aug 6, 2009, 08:53 UTC
Message-ID
<200908061053.38739.trast@student.ethz.ch>
In-Reply-To
<4A7A6756.4010305@googlemail.com>
Philip Herron wrote:
> 
> Question still stands is the hashing function [in hash.c], which one and why?
In the spirit of teaching you to fish...

First you'll want to find out where the original users of this code were. So you run

  git blame -- hash.c

and see that most of the lines come from 9027f53 (Do linear-time/space rename logic for exact renames, 2007-10-25). So you can then look at this commit:

  git show 9027f53c
Aha, it says
    In the expectation that we will indeed do the same hashing trick for the
    general rename case, this code uses a generic hash-table implementation
    that can be used for other things too.  In fact, we might be able to
    consolidate some of our existing hash tables with the new generic code
    in hash.[ch]
and further down in the patch
+       hash = hash_filespec(filespec);
+       pos = insert_hash(hash, entry, table);
and right above that
+static unsigned int hash_filespec(struct diff_filespec *filespec)
+{
+       unsigned int hash;
+       if (!filespec->sha1_valid) {
+               if (diff_populate_filespec(filespec, 0))
+                       return 0;
+               hash_sha1_file(filespec->data, filespec->size, "blob", filespec-
+       }
+       memcpy(&hash, filespec->sha1, sizeof(hash));
+       return hash;
+}
See?

As for the *why*, presumably because all of git assumes two objects with the same SHA1 are indeed the same file; so we can later make the same optimisation again:

+                       if (hashcmp(one->sha1, two->sha1))
+                               continue;

And then, as we've already computed the SHA1, any subset of it is as good a hash as anything else; it'll be uniformly distributed.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch
Previous: Philip Herron
Message 3 of 3 in “Hash Tables”
  1. Philip HerronAug 6, 2009
  2. Philip HerronAug 6, 2009
  3. Thomas RastAug 6, 2009

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.