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

Re: Fix object re-hashing

From
Linus Torvalds <torvalds@osdl.org>
Date
Feb 12, 2006, 18:53 UTC
Message-ID
<Pine.LNX.4.64.0602121037460.3691@g5.osdl.org>
In-Reply-To
<7vaccwbf6v.fsf@assigned-by-dhcp.cox.net>
On Sun, 12 Feb 2006, Junio C Hamano wrote:
> 
> This "fix" makes the symptom that me fire two (maybe three)
> Grrrrr messages earlier this morning disappear.

Goodie. I assume that was the fixed fix, not my original "edit out the useless optimization and then break it totally" fix ;)

Show 5 quoted lines
> I haven't had my caffeine nor nicotine yet after my short sleep, so I 
> need to take some time understanding your explanation first, but I am 
> reasonably sure this must be it (not that I do not trust you, not at all 
> -- it is that I do not trust *me* applying a patch without understanding 
> when I have a bug reproducible).

The basic notion is that this hashing algorithm uses a normal "linear probing" overflow approach, which basically means that overflows in a hash bucket always just probe the next few buckets to find an empty one.

That's a really simple (and fairly cache-friendly) approach, and it makes tons of sense, especially since we always re-size the hash to guarantee that we'll have empty slots. It's a bit more subtle - especially when re-hashing - than the probably more common "collission chain" approach, though.

Now, when we re-hash, the important rule is:
 - the re-hashing has to walk in the same direction as the overflow.

This is important, because when we move a hashed entry, that automatically means that even otherwise _already_correctly_ hashed entries may need to be moved down (ie even if their "inherent hash" does not change, their _effective_ hash address changes because their overflow position needs to be fixed up).

There are two interesting cases:
 - the "overflow of the overflow": when the linear probing itself 
   overflows the size of the hash queue, it will "change direction" by 
   overflowing back to index zero.
   Happily, the re-hashing does not need to care about this case, because 
   the new hash is bigger: the rule we have when doing the re-hashing is 
   that as we re-hash, the "i" entries we have already re-hashed are all 
   valid in the new hash, so even if overflow occurs, it will occur the 
   right way (and if it overflows all the way past the current "i", we'll 
   re-hash the already re-hashed entry anyway).
 - the old/new border case. In particular, the trivial logic says that we 
   only need to re-hash entries that were hashed with the old hash. That's 
   what the broken code did: it only traversed "0..oldcount-1", because 
   any entries that had an index bigger than or equal to "oldcount" were 
   obviously _already_ re-hashed.
   That logic sounds obvious, but it falls down on exactly the fact that 
   we may indeed have to re-hash even entries that already were re-hashed 
   with the new algorithm, exactly because of the overflow changes.

So the boundary for old/new is really: "you need to rehash all entries that were old, but then you _also_ need to rehash the list of entries that you rehashed that might need to be moved down to an empty spot vacated by an old hash".

So the stop condition really ends up being: "stop when you have seen all old hash entries _and_ at least one empty entry after that", since an empty entry means that there was no overflow from earlier positions past that position. But it's just simpler to walk the whole damn new thing and not worry about it.

		Linus
Previous: Junio C HamanoNext: Linus Torvalds
Message 4 of 13 in “Fix object re-hashing”
  1. Linus TorvaldsFeb 12, 2006
  2. Linus TorvaldsFeb 12, 2006
  3. Junio C HamanoFeb 12, 2006
  4. Linus TorvaldsFeb 12, 2006
  5. Linus TorvaldsFeb 12, 2006
  6. Junio C HamanoFeb 12, 2006
  7. Linus TorvaldsFeb 12, 2006
  8. Johannes SchindelinFeb 12, 2006
  9. Linus TorvaldsFeb 13, 2006
  10. Johannes SchindelinFeb 13, 2006
  11. Junio C HamanoFeb 12, 2006
  12. Linus TorvaldsFeb 12, 2006
  13. Linus TorvaldsFeb 12, 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.