git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Kaartic Sivaraam <kaarticsivaraam91196@gmail.com>
To: Jonathan Nieder <jrnieder@gmail.com>
Cc: Michael Haggerty <mhagger@alum.mit.edu>,
	Junio C Hamano <gitster@pobox.com>,
	Alex Riesen <raa.lkml@gmail.com>,
	git@vger.kernel.org
Subject: Re: [PATCH] for_each_string_list_item(): behave correctly for empty list
Date: Wed, 20 Sep 2017 13:05:01 +0530	[thread overview]
Message-ID: <3467b198-8c8c-2bfa-b139-d3ed5ab6b8bc@gmail.com> (raw)
In-Reply-To: <20170920023008.GB126984@aiede.mtv.corp.google.com>

Hi,

Though this thread seems to have reached a conclusion, I just wanted to
know what I was missing about the optimisation.

On Wednesday 20 September 2017 08:00 AM, Jonathan Nieder wrote:
> From that link:
>      for ( ;valid_int && *valid_int < 10; (*valid_int)++) {
>          printf("Valid instance");
>      }
>
> Both gcc and clang are able to optimize out the 'valid_int &&' because
> it is dereferenced on the RHS of the &&.
>
> For comparison, 'item < (list)->items + (list)->nr' does not
> dereference (list)->items.  So that optimization doesn't apply here.
>
> A smart compiler could be able to take advantage of there being no
> object pointed to by a null pointer, which means
>
> 	item < (list)->items + (list)->nr
>
> is always false when (list)->items is NULL, which in turn makes a
> '(list)->items &&' test redundant.  But a quick test with gcc 4.8.4
> -O2 finds that at least this compiler does not contain such an
> optimization.  The overhead Michael Haggerty mentioned is real.
>

I thought the compiler optimized that check out of the loop because the
check was "invariant" across loop runs. IOW, the values used in the check
didn't change across loop runs so the compiler thought it's better to do
the check once outside the loop rather than doing it each time inside
the loop. I guess this is some kind of "loop unswitching"[1]. I don't 
see how
dereferencing influences the optimization here.

Just to be sure, I tried once more to see whether the compiler optimizes 
this
or not. This time with a more similar example and even using the macro 
of concern.
Surprisingly, the compiler did optimize the check out of the loop. This 
time both
'gcc' and 'clang' with an -O1 !

https://godbolt.org/g/Y6rHc1
https://godbolt.org/g/EMrftw

So, is the overhead still real or am I missing something?

[1] : https://en.wikipedia.org/wiki/Loop_unswitching

---
Kaartic

  parent reply	other threads:[~2017-09-20  7:35 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-15 16:00 [PATCH] for_each_string_list_item(): behave correctly for empty list Michael Haggerty
2017-09-15 18:43 ` Jonathan Nieder
2017-09-16  4:06   ` Michael Haggerty
2017-09-16 11:51     ` SZEDER Gábor
2017-09-17 10:19       ` Michael Haggerty
2017-09-19 14:38     ` Kaartic Sivaraam
2017-09-20  1:38       ` Junio C Hamano
2017-09-20  1:43         ` Jonathan Nieder
2017-09-20  5:14           ` Junio C Hamano
2017-09-20  2:30       ` Jonathan Nieder
2017-09-20  3:54         ` Junio C Hamano
2017-09-20  5:27           ` [PATCH v2] for_each_string_list_item: avoid undefined behavior " Jonathan Nieder
2017-09-20  5:40             ` Junio C Hamano
2017-09-20  7:00             ` Michael Haggerty
2017-09-20  7:40             ` Kaartic Sivaraam
2017-09-20 12:22             ` [PATCH v2] doc: camelCase the config variables to improve readability Kaartic Sivaraam
2017-09-20 16:28             ` [PATCH v2] for_each_string_list_item: avoid undefined behavior for empty list Andreas Schwab
2017-09-20 17:31               ` Jonathan Nieder
2017-09-20 21:51                 ` Andreas Schwab
2017-09-21  1:12                   ` Junio C Hamano
2017-09-21 15:39                     ` Andreas Schwab
2017-09-20  7:35         ` Kaartic Sivaraam [this message]
2017-09-17  0:59 ` [PATCH] for_each_string_list_item(): behave correctly " Junio C Hamano
2017-09-17 10:24   ` Michael Haggerty
2017-09-18  0:37     ` Junio C Hamano
2017-09-19  0:08       ` Stefan Beller
2017-09-19  6:51         ` Michael Haggerty
2017-09-19 13:38           ` SZEDER Gábor
2017-09-19 13:45             ` SZEDER Gábor

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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3467b198-8c8c-2bfa-b139-d3ed5ab6b8bc@gmail.com \
    --to=kaarticsivaraam91196@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jrnieder@gmail.com \
    --cc=mhagger@alum.mit.edu \
    --cc=raa.lkml@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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).