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

Re: [PATCH v4] cleanup duplicate name_compare() functions

From
Jeremiah Mahler <jmmahler@gmail.com>
Date
Jun 20, 2014, 01:55 UTC
Message-ID
<20140620015511.GB2481@hudson.localdomain>
In-Reply-To
<CAPc5daW5hsKeJsG8+2nxFzFL9gMnLR2gMBXG5__dhSCLKkBWmA@mail.gmail.com>
Junio,
On Thu, Jun 19, 2014 at 11:29:21AM -0700, Junio C Hamano wrote:
Show 40 quoted lines
> On Thu, Jun 19, 2014 at 11:03 AM, Junio C Hamano <gitster@pobox.com> wrote:
> >
> > You chose to use the one that loses the information by unifying
> > these two into the variant that only returns -1/0/+1.  We know that
> > it does not matter for the current callers, but is it expected that
> > no future callers will benefit by having the magnitude information?
> 
> Heh, I was being silly, partly fooled by your reference to
> "magnitude".
> 
> You are not losing information at all, because the caller cannot
> tell if the return value came from an earlier memcmp(), whose only
> guarantee is that the sign of the returned value is all that
> matters, or from the later subtraction between lengths.
> 
> So unifying to the -1/0/+1 variant is entirely justifiable.  It is
> just your rationale was a bit misleading.
> 
>     We often represent our strings as a counted string, i.e. a pair of
>     the pointer to the beginning of the string and its length, and the
>     string may not be NUL terminated to that length.
> 
>     To compare a pair of such counted strings, unpack-trees.c and
>     read-cache.c implement their own name_compare() functions
>     identically.  In addition, cache_name_compare() function in
>     read-cache.c is nearly identical.  The only difference is when one
>     string is the prefix of the other string, in which case the former
>     returns -1/+1 to show which one is longer and the latter returns the
>     difference of the lengths to show the same information.
> 
>     Unify these three functions by using the implementation from
>     cache_name_compare().  This does not make any difference to the
>     existing and future callers, as they must be paying attention only
>     to the sign of the returned value (and not the magnitude) because
>     the original implementations of these two functions return values
>     returned by memcmp(3) when the one string is not a prefix of the
>     other string, and the only thing memcmp(3) guarantees its callers is
>     the sign of the returned value, not the magnitude.
> 
> or something like that, perhaps?

Yes, that looks good. It is a bit clearer than my message. I like how you used "the prefix of the other string" to describe when the two functions behave differently.

-- 
Jeremiah Mahler
jmmahler@gmail.com
http://github.com/jmahler
Previous: Junio C HamanoNext: Jeremiah Mahler
Message 5 of 6 in “cleanup duplicate name_compare() functions”
  1. cleanup duplicate name_compare() functionsJeremiah Mahler, Jun 19, 2014
  2. cleanup duplicate name_compare() functionsJeremiah Mahler, Jun 19, 2014
  3. Junio C HamanoJun 19, 2014
  4. Junio C HamanoJun 19, 2014
  5. Jeremiah MahlerJun 20, 2014
  6. Jeremiah MahlerJun 19, 2014

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.