public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox•com>
To: Karsten Blees <karsten.blees@gmail•com>
Cc: Git List <git@vger•kernel.org>
Subject: Re: [PATCH v1 1/4] hashmap: factor out getting an int hash code from a, SHA1
Date: Mon, 07 Jul 2014 10:22:56 -0700	[thread overview]
Message-ID: <xmqqoax1dqxb.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <53B485A4.20609@gmail.com> (Karsten Blees's message of "Thu, 03 Jul 2014 00:20:20 +0200")

Karsten Blees <karsten.blees@gmail•com> writes:

> Copying the first bytes of a SHA1 is duplicated in six places, however,
> the implications (wrong byte order on little-endian systems) is documented
> only once.

s/wrong /different /; but other than that I think this is a good
change.

> +`unsigned int sha1hash(const unsigned char *sha1)`::
> +
> +	Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
> +	for use in hash tables. Cryptographic hashes are supposed to have
> +	uniform distribution, so in contrast to `memhash()`, this just copies
> +	the first `sizeof(int)` bytes without shuffling any bits. Note that
> +	the results will be different on big-endian and little-endian
> +	platforms, so they should not be stored or transferred over the net!

Tone down with s/!/./, perhaps?

Another thing we may want to caution against is to use it as a
tie-breaker that affects the final outcome the user can observe, but
that may be something that goes without saying.  I dunno..

> diff --git a/hashmap.h b/hashmap.h
> index a816ad4..ed5425a 100644
> --- a/hashmap.h
> +++ b/hashmap.h
> @@ -13,6 +13,17 @@ extern unsigned int strihash(const char *buf);
>  extern unsigned int memhash(const void *buf, size_t len);
>  extern unsigned int memihash(const void *buf, size_t len);
>  
> +static inline unsigned int sha1hash(const unsigned char *sha1)
> +{
> +	/*
> +	 * Equivalent to 'return *(int *)sha1;', but safe on platforms that
> +	 * don't support unaligned reads.
> +	 */

s/int/unsigned &/; other than that, the explanation is good.

> +	unsigned int hash;
> +	memcpy(&hash, sha1, sizeof(hash));
> +	return hash;
> +}
> +

  reply	other threads:[~2014-07-07 17:23 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-02 22:18 [PATCH v1 0/4] hashmap improvements Karsten Blees
2014-07-02 22:20 ` [PATCH v1 1/4] hashmap: factor out getting an int hash code from a, SHA1 Karsten Blees
2014-07-07 17:22   ` Junio C Hamano [this message]
2014-07-02 22:21 ` [PATCH v1 2/4] hashmap: improve struct hashmap member documentation Karsten Blees
2014-07-02 22:22 ` [PATCH v1 3/4] hashmap: add simplified hashmap_get_from_hash() API Karsten Blees
2014-07-07 17:43   ` Junio C Hamano
2014-07-11 19:11     ` Karsten Blees
2014-07-11 22:21       ` Junio C Hamano
2014-07-02 22:22 ` [PATCH v1 4/4] hashmap: add string interning API Karsten Blees
2014-07-03  7:22   ` Matthieu Moy
2014-07-07 17:44   ` Junio C Hamano
2014-07-03  7:23 ` [PATCH v1 0/4] hashmap improvements Matthieu Moy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqoax1dqxb.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=karsten.blees@gmail$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox