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

Re: [PATCH 3/4] xdiff: use stronger hash function internally

From
René Scharfe <l.s.r@web.de>
Date
Oct 24, 2017, 20:23 UTC
Message-ID
<16718615-c5df-24dd-fa62-2b42f5d83a02@web.de>
In-Reply-To
<20171024185917.20515-4-sbeller@google.com>
Am 24.10.2017 um 20:59 schrieb Stefan Beller:
> Instead of using the hash seeded with 5381, and updated via
> `(hash << 5) ^ new_byte`, use the FNV-1 primitives as offered by
> hashmap.h, which is seeded with 0x811c9dc5 and computed as
> `(hash * 0x01000193) ^ new_byte`.

The hash function you're replacing is called DJB2; I think that's worth mentioning.

Performance test results would be nice. No idea how to find edge cases, though, or better: demonstrate a lack thereof.

Show 17 quoted lines
> 
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>   xdiff/xutils.c | 19 ++++++++-----------
>   1 file changed, 8 insertions(+), 11 deletions(-)
> 
> diff --git a/xdiff/xutils.c b/xdiff/xutils.c
> index 04d7b32e4e..a58a28c687 100644
> --- a/xdiff/xutils.c
> +++ b/xdiff/xutils.c
> @@ -24,7 +24,8 @@
>   #include <assert.h>
>   #include "xinclude.h"
>   
> -
> +#include "cache.h"
> +#include "hashmap.h"

Ouch. Defining FNV32_BASE and FNV32_PRIME here would be much easier overall. And if that's too much duplication then those definitions could be extracted into a new header file (fnv32.h?) included by both hashmap.h and xutils.c.

Show 19 quoted lines
>   
>   
>   long xdl_bogosqrt(long n) {
> @@ -228,7 +229,7 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
>   
>   static unsigned long xdl_hash_record_with_whitespace(char const **data,
>   		char const *top, long flags) {
> -	unsigned long ha = 5381;
> +	unsigned long ha = memhash(NULL, 0);
>   	char const *ptr = *data;
>   
>   	for (; ptr < top && *ptr != '\n'; ptr++) {
> @@ -243,21 +244,18 @@ static unsigned long xdl_hash_record_with_whitespace(char const **data,
>   				; /* already handled */
>   			else if (flags & XDF_IGNORE_WHITESPACE_CHANGE
>   				 && !at_eol) {
> -				ha += (ha << 5);
> -				ha ^= (unsigned long) ' ';
> +				ha = memhash_feed(ha, (unsigned char) ' ');

All the memhash_feed() callers in this file cast to unsigned char. A macro or a function (possibly inline) defined at the top could do that for them.

Show 37 quoted lines
>   			}
>   			else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
>   				 && !at_eol) {
>   				while (ptr2 != ptr + 1) {
> -					ha += (ha << 5);
> -					ha ^= (unsigned long) *ptr2;
> +					ha = memhash_feed(ha, (unsigned char) *ptr2);
>   					ptr2++;
>   				}
>   			}
>   			continue;
>   		}
> -		ha += (ha << 5);
> -		ha ^= (unsigned long) *ptr;
> +		ha = memhash_feed(ha, (unsigned char) *ptr);
>   	}
>   	*data = ptr < top ? ptr + 1: ptr;
>   
> @@ -265,15 +263,14 @@ static unsigned long xdl_hash_record_with_whitespace(char const **data,
>   }
>   
>   unsigned long xdl_hash_record(char const **data, char const *top, long flags) {
> -	unsigned long ha = 5381;
> +	unsigned long ha = memhash(NULL, 0);
>   	char const *ptr = *data;
>   
>   	if (flags & XDF_WHITESPACE_FLAGS)
>   		return xdl_hash_record_with_whitespace(data, top, flags);
>   
>   	for (; ptr < top && *ptr != '\n'; ptr++) {
> -		ha += (ha << 5);
> -		ha ^= (unsigned long) *ptr;
> +		ha = memhash_feed(ha, (unsigned char) *ptr);
>   	}
>   	*data = ptr < top ? ptr + 1: ptr;
>   
> 
Previous: Stefan BellerNext: Stefan Beller
Message 10 of 31 in “(x)diff cleanup: remove duplicate code”
  1. 0/4 (x)diff cleanup: remove duplicate codeStefan Beller, Oct 24, 2017
  2. 1/4 hashmap: introduce memhash_feed to access the internals of FNV-1 hashStefan Beller, Oct 24, 2017
  3. René ScharfeOct 24, 2017
  4. Stefan BellerOct 24, 2017
  5. 2/4 xdiff-interface: export comparing and hashing stringsStefan Beller, Oct 24, 2017
  6. René ScharfeOct 24, 2017
  7. Stefan BellerOct 24, 2017
  8. René ScharfeOct 26, 2017
  9. 3/4 xdiff: use stronger hash function internallyStefan Beller, Oct 24, 2017
  10. René ScharfeOct 24, 2017
  11. Stefan BellerOct 24, 2017
  12. Jonathan NiederOct 24, 2017
  13. 4/4 diff.c: get rid of duplicate implementationStefan Beller, Oct 24, 2017
  14. Stefan BellerOct 24, 2017
  15. 0/2 (x)diff cleanup: remove duplicate codeStefan Beller, Oct 24, 2017
  16. 1/2 xdiff-interface: export comparing and hashing stringsStefan Beller, Oct 24, 2017
  17. Eric SunshineOct 25, 2017
  18. 2/2 diff.c: get rid of duplicate implementationStefan Beller, Oct 24, 2017
  19. Junio C HamanoOct 25, 2017
  20. Stefan BellerOct 25, 2017
  21. 0/2 (x)diff cleanup: remove duplicate codeStefan Beller, Oct 25, 2017
  22. 2/2 diff.c: get rid of duplicate implementationStefan Beller, Oct 25, 2017
  23. Junio C HamanoOct 26, 2017
  24. René ScharfeOct 26, 2017
  25. Jeff KingOct 30, 2017
  26. Jeff KingOct 30, 2017
  27. 1/2 xdiff-interface: export comparing and hashing stringsStefan Beller, Oct 25, 2017
  28. René ScharfeOct 26, 2017
  29. Junio C HamanoOct 27, 2017
  30. Stefan BellerOct 27, 2017
  31. 1/5 fnv: migrate code from hashmap to fnvStefan Beller, Oct 24, 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.