Re: [PATCH 8/4] match-trees: drop "x = x" initializations
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Mar 24, 2013, 04:55 UTC
- Message-ID
- <7vli9d4crq.fsf@alter.siamese.dyndns.org>
- In-Reply-To
- <514DFB1A.8040102@lsrfire.ath.cx>
René Scharfe <rene.scharfe@lsrfire.ath.cx> writes:
> Hmm, let's see if we can help the compiler follow the code without > making it harder for people to understand. The patch looks a bit > jumbled, but the resulting code is OK in my biased opinion.
I actually think the result is much better than a mere "OK"; the duplicated "at this point we know path1 (or path2) is missing from the other side" has been bothering me and I was about to suggest a similar rewrite before I read your message ;-)
However, the same compiler still thinks {elem,path,mode}1 can be used uninitialized (but not {elem,path,mode}2). The craziness I reported in the previous message is also the same. With this patch on top to swap the side we inspect first, the compiler thinks {elem,path,mode}2 can be used uninitialized but not the other three variables X-<.
So I like your change for readability, but for GCC 4.4.5 we still need the unnecessary initialization.
match-trees.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/match-trees.c b/match-trees.c index c0c66bb..9ea2c80 100644 --- a/match-trees.c +++ b/match-trees.c @@ -77,16 +77,16 @@ static int score_trees(const unsigned char *hash1, const unsigned char *hash2) unsigned mode1, mode2; int cmp = 0; - if (one.size) - elem1 = tree_entry_extract(&one, &path1, &mode1); - else - /* two has more entries */ - cmp = 1; if (two.size) elem2 = tree_entry_extract(&two, &path2, &mode2); else /* two lacks this entry */ cmp = -1; + if (one.size) + elem1 = tree_entry_extract(&one, &path1, &mode1); + else + /* two has more entries */ + cmp = 1; if (!cmp) cmp = base_name_compare(path1, strlen(path1), mode1,