Re: [PATCH v2 2/2] parseopt: check for duplicate long names and numerical options
- From
René Scharfe <l.s.r@web.de>
- Date
- Feb 28, 2026, 11:28 UTC
- Message-ID
- <7c221132-c2ac-4c6f-9d89-72677a74beb5@web.de>
- In-Reply-To
- <20260228105849.GA3626520@coredump.intra.peff.net>
On 2/28/26 11:58 AM, Jeff King wrote:
Show 23 quoted lines
> On Sat, Feb 28, 2026 at 10:19:16AM +0100, René Scharfe wrote:
>
>> +static void parse_options_check_harder(const struct option *opts)
>> +{
>> + struct strset long_names = STRSET_INIT;
>> + for (; opts->type != OPTION_END; opts++) {
>> + if (opts->long_name) {
>> + if (!strset_add(&long_names, opts->long_name))
>> + optbug(opts, "long name already used");
>> + }
>> + }
>> + BUG_if_bug("invalid 'struct option'");
>> + strset_clear(&long_names);
>> +}
>
> I confirmed on my silly pathological case that invoking rev-parse with a
> real option shows no slowdown, and we now pay the same 10ms cost to show
> "-h".
>
> Your other email made me wonder how the sorted-array solution might
> perform (patch below). It shaves off 2ms of those 10. Probably not worth
> caring about for "-h" output (which is already spending another 5-10ms
> to generate the output, versus a normal parse).Curious; sorting performs worse on my machine (Apple M1, 1 is 2cc719175, 2 is patch 2 v2, 3 is your patch on top):
Benchmark 1: ./git_main rev-parse --parseopt -- -h <input Time (mean ± σ): 77.5 ms ± 0.4 ms [User: 73.1 ms, System: 3.5 ms] Range (min … max): 76.8 ms … 78.5 ms 37 runs
Warning: Ignoring non-zero exit code.
Benchmark 2: ./git_strset rev-parse --parseopt -- -h <input Time (mean ± σ): 82.6 ms ± 0.3 ms [User: 77.7 ms, System: 3.9 ms] Range (min … max): 82.1 ms … 83.7 ms 34 runs
Warning: Ignoring non-zero exit code.
Benchmark 3: ./git_qsort rev-parse --parseopt -- -h <input Time (mean ± σ): 85.6 ms ± 0.2 ms [User: 81.2 ms, System: 3.5 ms] Range (min … max): 85.3 ms … 86.5 ms 33 runs
Warning: Ignoring non-zero exit code.
Summary
./git_main rev-parse --parseopt -- -h <input ran
1.07 ± 0.01 times faster than ./git_strset rev-parse --parseopt -- -h <input
1.10 ± 0.01 times faster than ./git_qsort rev-parse --parseopt -- -h <input