Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #107461
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Appropriate optimization |
| Date | 2017-03-30 10:20 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <obievd$d1v$1@dont-email.me> (permalink) |
| References | <117d90fa-6b8d-4a4f-a663-4686945fdc2d@googlegroups.com> |
On 29/03/17 22:44, Rick C. Hodgin wrote:
> This question is for anyone, but I'm specifically curious what Supercat
> thinks...
>
> Would it be an appropriate optimization to move the code on lines 101:
> and 102: to somewhere in the middle of the if..expression on line 104:
>
> // Note: This code is in the middle of a local function
> 101: int count = sizeof(thing) / my_sizing_determination(thing);
> 102: int size = count * MY_COUNT_REQUIREMENT;
> 103:
> 104: if (!p && (p = allocate_something(count, size)))
> 105: {
> ... only the code in this block uses count and size
> Nnn: }
>
> The values not used before that point in the function, and they're
> not used outside of the loop, and they're not flagged as volatile.
> It would delay or prevent the call to my_sizing_determination(),
> and is that a constraint?
>
In this particular case, line 104 uses both "count" and "size", so they
need to be evaluated before that.
But in general, all the compiler has to preserve is the ordering of
"observable behaviour" - file I/O, program start/stop, and volatile
accesses. Everything else can be moved around, repeated, eliminated,
split up, combined, and otherwise massaged as long as all the observable
behaviour is as it should be. Function calls can also be moved around
if the compiler knows about the observable behaviour in the call - but
if it does not know the definition of the function, then it must assume
the worst.
So a call to an external function "foo" is assumed to access volatiles
and any globally visible data. But if "foo" is defined in the same
translation unit, or the compiler has other information about it from
somewhere (for example, the compiler knows how the standard library
functions work), then it might know that it is safe to move it around.
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
Appropriate optimization "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-03-29 13:44 -0700
Re: Appropriate optimization Ian Collins <ian-news@hotmail.com> - 2017-03-30 10:41 +1300
Re: Appropriate optimization supercat@casperkitty.com - 2017-03-29 15:22 -0700
Re: Appropriate optimization supercat@casperkitty.com - 2017-03-29 15:10 -0700
Re: Appropriate optimization Robert Wessel <robertwessel2@yahoo.com> - 2017-03-30 00:47 -0500
Re: Appropriate optimization David Brown <david.brown@hesbynett.no> - 2017-03-30 10:15 +0200
Re: Appropriate optimization "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-03-30 10:11 -0700
Re: Appropriate optimization David Brown <david.brown@hesbynett.no> - 2017-03-30 19:55 +0200
Re: Appropriate optimization supercat@casperkitty.com - 2017-03-30 11:47 -0700
Re: Appropriate optimization Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-30 12:03 -0700
Re: Appropriate optimization Keith Thompson <kst-u@mib.org> - 2017-03-30 12:24 -0700
Re: Appropriate optimization supercat@casperkitty.com - 2017-03-30 13:21 -0700
Re: Appropriate optimization Ike Naar <ike@iceland.freeshell.org> - 2017-03-30 20:39 +0000
Re: Appropriate optimization supercat@casperkitty.com - 2017-03-30 13:58 -0700
Re: Appropriate optimization David Brown <david.brown@hesbynett.no> - 2017-03-30 10:20 +0200
csiph-web