Re: [PATCH] merge: break out of all_strategy loop when strategy is found

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Rose via GitGitGadget" <gitgitgadget@xxxxxxxxx> writes:

> From: Seija Kijin <doremylover123@xxxxxxxxx>
>
> strncmp does not modify any of the memory,
> so looping through all elements is a waste of resources.

Modifying or not probably has little to do with why we may want to
do this change.

Here, we are trying to see which commands that appear in the
main_cmds table do not appear in the all_strategy[] table.  The way
we do so is by iterating over the main_cmds table, and for each of
the command, if it is found in the all_strategy[] table.  If there
is one, then found bit is set.  If there isn't, then found bit is
left clear.  After looping over all_strategy[] table, we act upon
the value of the found bit.

So, as soon as we find one match in all_strategy[] table and flip
the found bit on, in the loop we never clear the bit.  So it does
make sense to break out of that inner loop once we find a single
match.

    Once we find a match, there is no point to try finding the
    second match in the inner loop.  Break out of the loop once we
    find the first match.

would be a more appropriate explanation.

>  builtin/merge.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/merge.c b/builtin/merge.c
> index 0f093f2a4f2..5ab0feb47b6 100644
> --- a/builtin/merge.c
> +++ b/builtin/merge.c
> @@ -189,9 +189,12 @@ static struct strategy *get_strategy(const char *name)
>  			int j, found = 0;
>  			struct cmdname *ent = main_cmds.names[i];
>  			for (j = 0; j < ARRAY_SIZE(all_strategy); j++)
> -				if (!strncmp(ent->name, all_strategy[j].name, ent->len)
> -						&& !all_strategy[j].name[ent->len])
> +				if (!strncmp(ent->name, all_strategy[j].name,
> +					     ent->len) &&
> +				    !all_strategy[j].name[ent->len]) {
>  					found = 1;
> +					break;
> +				}

The above is not wrong per-se, but we can do the same with less
damage to the code, e.g.

 builtin/merge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git c/builtin/merge.c w/builtin/merge.c
index dd474371a2..2437aae6bc 100644
--- c/builtin/merge.c
+++ w/builtin/merge.c
@@ -188,7 +188,7 @@ static struct strategy *get_strategy(const char *name)
 		for (i = 0; i < main_cmds.cnt; i++) {
 			int j, found = 0;
 			struct cmdname *ent = main_cmds.names[i];
-			for (j = 0; j < ARRAY_SIZE(all_strategy); j++)
+			for (j = 0; !found && j < ARRAY_SIZE(all_strategy); j++)
 				if (!strncmp(ent->name, all_strategy[j].name, ent->len)
 						&& !all_strategy[j].name[ent->len])
 					found = 1;


I've mentioned it before, but could you please drop the

    Rose <83477269+AtariDreams@xxxxxxxxxxxxxxxxxxxxxxxx>

address from the Cc: list?  It is rude to force those who want to
respond to you to remove the non-working address that is meant not
to receive any responses.

Thanks.



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux