optimizing costly calculation

View: New views
2 Messages — Rating Filter:   Alert me  

optimizing costly calculation

by Mathieu Lacage-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hi,

I have a piece of code which does roughly this (after inlining):

Foo *foo1 = bar (ctx);
foo1->something ();
Foo *foo2 = bar (ctx);
foo2->something ();

bar is a function which will always return the same value if its input
is the same value (and ctx is constant over the function body) so, I
would like to instruct gcc that he can optimize away the second call
to bar but I could not find any obvious way to do this. Is this a
really stupid idea or did I miss something obvious ?

Mathieu
--
Mathieu Lacage <mathieu.lacage@...>

Re: optimizing costly calculation

by Kevin P. Fleming-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Mathieu Lacage wrote:

> hi,
>
> I have a piece of code which does roughly this (after inlining):
>
> Foo *foo1 = bar (ctx);
> foo1->something ();
> Foo *foo2 = bar (ctx);
> foo2->something ();
>
> bar is a function which will always return the same value if its input
> is the same value (and ctx is constant over the function body) so, I
> would like to instruct gcc that he can optimize away the second call
> to bar but I could not find any obvious way to do this. Is this a
> really stupid idea or did I miss something obvious ?

Look at the 'pure' and 'const' attributes that can be applied to
functions. These are GCC extensions, but one of them will be appropriate
for your example.

--
Kevin P. Fleming
Digium, Inc. | Director of Software Technologies
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
skype: kpfleming | jabber: kpfleming@...
Check us out at www.digium.com & www.asterisk.org