From: Junio C Hamano <gitster@pobox.com>
To: shejialuo <shejialuo@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 2/4] string-list: replace negative index encoding with "exact_match" parameter
Date: Mon, 08 Sep 2025 09:56:08 -0700 [thread overview]
Message-ID: <xmqqv7lsx4rb.fsf@gitster.g> (raw)
In-Reply-To: <aL21gazVeyHMeG-s@ArchLinux> (shejialuo@gmail.com's message of "Mon, 8 Sep 2025 00:40:33 +0800")
shejialuo <shejialuo@gmail.com> writes:
> We would return negative index to indicate exact match by converting the
> original positive index to be "-1 - index" in
> "string_list_find_insert_index", which requires callers to decode this
> information.
>
> This is bad due to the following reasons:
>
> 1. The callers need to convert the negative index back to the original
> positive value, which requires the callers to understand the detail
> of the function.
That has pretty much been the convention so far, not convincing that
it is "bad" at all.
> 2. As we have to return negative index, we need to specify the return
> type to be `int` instead of `size_t`, which would cause sign compare
> warnings.
That sounds more like the tail wagging the dog.
Construct your argument the other way around, perhaps?
- We NEED to be able to use the full range of size_t to express the
index in the array string_list holds for SUCH AND SUCH REASONS.
But string_list_find_insert_index() uses "int", which may not be
large enough to cover the range size_t covers.
- In addition, in order to signal that the returned value for a
query is about an existing entry in the array, or a location that
an entry would be inserted at, we use a signed int and use the
bog standard "-1 - index" encoding for this purpose. This
further halves the range of valid array index.
- To allow us to use the full range of size_t, use full size_t for
the index, and have a separate bit to tell if that index is about
an existing entry, or where the queried entry would be stored at
if we inserted it.
Your argument does not justify the first point, your desire to use
size_t in the first place, and that is what makes it sound
backwards, I think..
next prev parent reply other threads:[~2025-09-08 16:56 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-07 16:40 [PATCH 2/4] string-list: replace negative index encoding with "exact_match" parameter shejialuo
2025-09-08 16:56 ` Junio C Hamano [this message]
2025-09-15 12:24 ` shejialuo
-- strict thread matches above, loose matches on Subject: below --
2025-09-07 16:40 [PATCH 0/4] enhance string-list API to fix sign compare warnings shejialuo
2025-09-07 16:42 ` [PATCH 2/4] string-list: replace negative index encoding with "exact_match" parameter shejialuo
2025-09-09 6:22 ` Patrick Steinhardt
2025-09-15 12:11 ` shejialuo
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=xmqqv7lsx4rb.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=shejialuo@gmail.com \
/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).