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

Re: [PATCH] string-list: make compare function compatible with qsort(3)

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 21, 2016, 18:10 UTC
Message-ID
<xmqqy3z9i24x.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<c7bac0b7-c555-162f-7880-0355831cee48@web.de>
René Scharfe <l.s.r@web.de> writes:
Show 10 quoted lines
> The cmp member of struct string_list points to a comparison function
> that is used for sorting and searching of list items.  It takes two
> string pointers -- like strcmp(3), which is in fact the default;
> cmp_items() provides a qsort(3) compatible interface by passing the
> string members of two struct string_list_item pointers to cmp.
>
> One shortcoming is that the comparison function is restricted to working
> with the string members of items; util is inaccessible to it.  Another
> one is that the value of cmp is passed in a global variable to
> cmp_items(), making string_list_sort() non-reentrant.

I think it is insane to make util accessible to the comparison function in the first place.

A string-list is primarily a table of strings that can be used to quickly look up a string in it (or detect absense of it), and optionally set and get the data associated to that string. If you allow the comparison function to take anything other than the string itself into account, you can no longer binary search unless you force callers to specify what to put in util when a string is added to the table, and you also remove the ability to modify util once a string is added to the table.

The string-list API exposes the "append without sorting and then sort before starting to look up efficiently with a binary search", and I think that is its biggest misdesign. Such an optimization would have been hidden from callers in a correctly designed API by making sure sorting happens lazily when a function that wants to see a sorted nature of the list for the first time, but somehow we ended up with an API with separate functions _insert() and _append() with an explicit _sort(). It then leads to unsorted_*_lookup() and similar mess, that imply that a string-list can be used not as a look-up table but just an unordered bag of items. In our attempt to make it serve as these two quite different things, it has become good API for neither of its two uses. The caller is forced to know when the list is not sorted and unsorted_* variant must be used, for example. "Perhaps it makes it even more flexible if we made util available to ordering decision" is a line of thinking that makes it even worse.

I do agree that non-reentrancy is an issue that is worth solving, though.

Previous: René ScharfeNext: Junio C Hamano
Message 4 of 5 in “string-list: make compare function compatible with qsort(3)”
  1. string-list: make compare function compatible with qsort(3)René Scharfe, Dec 21, 2016
  2. Jeff KingDec 21, 2016
  3. René ScharfeDec 29, 2016
  4. Junio C HamanoDec 21, 2016
  5. Junio C HamanoDec 21, 2016

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.