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

Re: Horrible re-packing?

From
Linus Torvalds <torvalds@osdl.org>
Date
Jun 5, 2006, 19:03 UTC
Message-ID
<Pine.LNX.4.64.0606051155000.5498@g5.osdl.org>
In-Reply-To
<Pine.LNX.4.64.0606051140530.5498@g5.osdl.org>
On this same thread..

This trivial patch not only simplifies the name hashing, it actually improves packing for both git and the kernel.

The git archive pack shrinks from 6824090->6622627 bytes (a 3% improvement), and the kernel pack shrinks from 108756213 to 108219021 (a mere 0.5% improvement, but still, it's an improvement from making the hashing much simpler!)

I think the hash function with its comment is self-explanatory:
        /*
         * This effectively just creates a sortable number from the
         * last sixteen non-whitespace characters. Last characters
         * count "most", so things that end in ".c" sort together.
         */
        while ((c = *name++) != 0) {
                if (isspace(c))
                        continue;
                hash = (hash >> 2) + (c << 24);
        }
        return hash;

ie we just create a 32-bit hash, where we "age" previous characters by two bits, so the last characters in a filename count most. So when we then compare the hashes in the sort routine, filenames that end the same way sort the same way.

It takes the subdirectory into account (unless the filename is > 16 characters), but files with the same name within the same subdirectory will obviously sort closer than files in different subdirectories.

And, incidentally (which is why I tried the hash change in the first place, of course) builtin-rev-list.c will sort fairly close to rev-list.c.

And no, it's not a "good hash" in the sense of being secure or unique, but that's not what we're looking for. The whole "hash" thing is misnamed here. It's not so much a hash as a "sorting number".

Comments?
		Linus
----
 pack-objects.c |   41 +++++++++++------------------------------
 1 files changed, 11 insertions(+), 30 deletions(-)
diff --git a/pack-objects.c b/pack-objects.c
index 3590cd5..ae49fba 100644
--- a/pack-objects.c
+++ b/pack-objects.c
@@ -473,38 +473,19 @@ #define DIRBITS 12
 
 static unsigned name_hash(struct name_path *path, const char *name)
 {
-	struct name_path *p = path;
-	const char *n = name + strlen(name);
-	unsigned hash = 0, name_hash = 0, name_done = 0;
-
-	if (n != name && n[-1] == '\n')
-		n--;
-	while (name <= --n) {
-		unsigned char c = *n;
-		if (c == '/' && !name_done) {
-			name_hash = hash;
-			name_done = 1;
-			hash = 0;
-		}
-		hash = hash * 11 + c;
-	}
-	if (!name_done) {
-		name_hash = hash;
-		hash = 0;
-	}
-	for (p = path; p; p = p->up) {
-		hash = hash * 11 + '/';
-		n = p->elem + p->len;
-		while (p->elem <= --n) {
-			unsigned char c = *n;
-			hash = hash * 11 + c;
-		}
-	}
+	unsigned char c;
+	unsigned hash = 0;
+
 	/*
-	 * Make sure "Makefile" and "t/Makefile" are hashed separately
-	 * but close enough.
+	 * This effectively just creates a sortable number from the
+	 * last sixteen non-whitespace characters. Last characters
+	 * count "most", so things that end in ".c" sort together.
 	 */
-	hash = (name_hash<<DIRBITS) | (hash & ((1U<<DIRBITS )-1));
+	while ((c = *name++) != 0) {
+		if (isspace(c))
+			continue;
+		hash = (hash >> 2) + (c << 24);
+	}
 	return hash;
 }
 
Previous: Linus TorvaldsNext: Junio C Hamano
Message 3 of 15 in “Horrible re-packing?”
  1. Linus TorvaldsJun 5, 2006
  2. Linus TorvaldsJun 5, 2006
  3. Linus TorvaldsJun 5, 2006
  4. Junio C HamanoJun 5, 2006
  5. Linus TorvaldsJun 5, 2006
  6. Junio C HamanoJun 5, 2006
  7. Junio C HamanoJun 6, 2006
  8. Olivier GalibertJun 5, 2006
  9. Nicolas PitreJun 5, 2006
  10. Chris WedgwoodJun 6, 2006
  11. Linus TorvaldsJun 6, 2006
  12. Linus TorvaldsJun 5, 2006
  13. Nicolas PitreJun 5, 2006
  14. Linus TorvaldsJun 5, 2006
  15. Nicolas PitreJun 5, 2006

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.