git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Haggerty <mhagger@alum.mit.edu>
To: Nicolas Pitre <nico@fluxnic.net>
Cc: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>,
	git@vger.kernel.org
Subject: Re: [PATCH 6/6] Document some functions defined in object.c
Date: Mon, 24 Feb 2014 09:47:57 +0100	[thread overview]
Message-ID: <530B073D.5010702@alum.mit.edu> (raw)
In-Reply-To: <alpine.LFD.2.11.1402211222270.17677@knanqh.ubzr>

Nicolas, thanks for the very fast feedback!

On 02/21/2014 06:33 PM, Nicolas Pitre wrote:
> On Fri, 21 Feb 2014, Michael Haggerty wrote:
> 
>> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
> 
> Minor nits below.
> 
> 
>> ---
>>  object.c | 23 ++++++++++++++++++++++-
>>  object.h |  7 +++++++
>>  2 files changed, 29 insertions(+), 1 deletion(-)
>>
>> diff --git a/object.c b/object.c
>> index 584f7ac..c34e225 100644
>> --- a/object.c
>> +++ b/object.c
>> @@ -43,14 +43,26 @@ int type_from_string(const char *str)
>>  	die("invalid object type \"%s\"", str);
>>  }
>>  
>> +/*
>> + * Return a numerical hash value between 0 and n-1 for the object with
>> + * the specified sha1.  n must be a power of 2.
>> + *
>> + * Since the sha1 is essentially random, we just take the required
>> + * bits from the first sizeof(unsigned int) bytes of sha1.
> 
> This might be improved a little.  The only reason for the sizeof() is 
> actually to copy those bits into a properly aligned integer.  Some 
> architectures have alignment restrictions that incure a significant cost 
> when integer operations are performed on unaligned data whereas sha1 
> pointers don't have any particular alignment requirements.  Once upon a 
> time this used to simply be:
> 
> 	return *(unsigned int *)sha1 & (n - 1);
> 
> The memcpy is there only to avoid unaligned accesses.

I understand all that; it's clear that the old code is not correct C,
isn't it?  And ISTM that the use of memcpy() is an implementation detail
that is not relevant to callers and so not needed in the docstring.  So
I suggest that your note be added as comments within the function; what
do you think?

Contrariwise, I thought about it again and believe that it *is*
important for the docstring to mention explicitly that the return value
is architecture-dependent (it depends on endianness and possibly
sizeof(unsigned int)).  Presumably the function is only used within one
Git invocation, so this isn't a problem, but we should warn callers.
Alternatively, we could stick a call to ntohl() in the function to make
the return value consistent, but this would cost a little bit on
little-endian computers.

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/

  reply	other threads:[~2014-02-24  8:48 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-21 16:32 [PATCH 0/6] Add a bunch of docstrings and make a few minor cleanups Michael Haggerty
2014-02-21 16:32 ` [PATCH 1/6] Add docstrings for lookup_replace_object() and do_lookup_replace_object() Michael Haggerty
2014-02-21 18:21   ` Junio C Hamano
2014-02-24  8:25     ` Michael Haggerty
2014-02-24  9:24   ` Christian Couder
2014-02-24 10:17     ` Michael Haggerty
2014-02-24 18:06       ` Junio C Hamano
2014-02-21 16:32 ` [PATCH 2/6] replace_object: use struct members instead of an array Michael Haggerty
2014-02-21 18:23   ` Junio C Hamano
2014-02-21 16:32 ` [PATCH 3/6] find_pack_entry(): document last_found_pack Michael Haggerty
2014-02-21 17:15   ` Nicolas Pitre
2014-02-21 16:32 ` [PATCH 4/6] sha1_file_name(): declare to return a const string Michael Haggerty
2014-02-21 16:32 ` [PATCH 5/6] Document a bunch of functions defined in sha1_file.c Michael Haggerty
2014-02-21 17:17   ` Nicolas Pitre
2014-02-24 18:18   ` Jakub Narębski
2014-02-24 20:01     ` Michael Haggerty
2014-02-24 20:08       ` Jonathan Nieder
2014-02-25 15:23         ` Michael Haggerty
2014-02-21 16:32 ` [PATCH 6/6] Document some functions defined in object.c Michael Haggerty
2014-02-21 17:33   ` Nicolas Pitre
2014-02-24  8:47     ` Michael Haggerty [this message]
2014-02-24 17:12       ` Junio C Hamano
2014-02-24 17:58 ` [PATCH 0/6] Add a bunch of docstrings and make a few minor cleanups Junio C Hamano

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=530B073D.5010702@alum.mit.edu \
    --to=mhagger@alum.mit.edu \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=nico@fluxnic.net \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
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;
as well as URLs for NNTP newsgroup(s).