473,756 Members | 3,051 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what can be optimized?

Hello.

What can be optimised in C++ code and how i can garantee stable
behaviour below

1.
Are expression "auto volatile" can deny removing as "unused temporary"
like this:

auto volatile const class_name tmp;

If not, how can i do it else?

2.
Can C++ garantee, that unnamed temporary object will not be removed as
"unused temporary" here:

class A{ A(){cout<<"star t..."; } }
class B{ B(){cout<<"ok"< <endl; } }

void inp();
void test(){ A(); inp(); B(); }

Jan 24 '07
17 2114
Grizlyk wrote:
Erik Wikström wrote:
Does concrete sequence exist between functions calls?
Do you mean if there is a defined sequence in which the ctors of
member-objects will be called?

No. In the case ctor A() used (logically) as ordinary function call.
Does concrete sequence (fixed order) exist between ordinary functions
calls?

Can the example below

a();
if( !is_ok() )b();

be switched by compiler to

if( !is_ok() ){ a(); b();}

I think that compiler can not switch "a()" and "is_ok()" calls, and
probably if a() is ctor (a()==A::A()) can not also.
You need to forget about the fact that A() involves a constructor call. You
have a statement sequence:

a();
if( !is_ok() )b();

The first statement is evaluated first. At the ";" all side-effects of its
evaluation a guaranteed to have taken place since there is a sequence
point. Then the if-statement is executed. This is part of the meaning of
the program. Any subsequence reordering of machine code instructions that
the compiler may perform for optimization must preserve the meaning of the
program (i.e., its observable behavior).

I wonder what made you think it could possibly be otherwise. It's not like
the basic principles of C++ all of a sudden cease to apply just because an
expression involves creating a temporary object.
Best

Kai-Uwe Bux
Jan 24 '07 #11
Kai-Uwe Bux wrote:
You need to forget about the fact that A() involves a constructor call. You
have a statement sequence:

a();
if( !is_ok() )b();

The first statement is evaluated first. At the ";" all side-effects of its
evaluation a guaranteed to have taken place since there is a sequence
point. Then the if-statement is executed. This is part of the meaning of
the program. Any subsequence reordering of machine code instructions that
the compiler may perform for optimization must preserve the meaning of the
program (i.e., its observable behavior).
"there is a sequence point."
The definition "sequence point" is not known to me (i do not see any
C++ book speaking about "sequence point"), and i do not use "sequence
point" befor.
I wonder what made you think it could possibly be otherwise. It's not like
the basic principles of C++ all of a sudden cease to apply just because an
expression involves creating a temporary object.
"what made you think it could possibly be otherwise"
I think like this:
1.
in the following code

{
int i=0;
if( !is_ok() ){b(i);}
return;
}

The variable "int i=0" defined befor "b(i)". The usage of "i" is well
defined so compiler can move the definition "int i=0" into place of
usage in order to increase speed&size for false condition

{
if( !is_ok() ){int i=0; b(i);}
return;
}

(at least if i write compiler i could do it)

2.
the following code

{
int(0);
if( !is_ok() ){b();}
return;
}

The usage of "int(0);" is well defined also, and compiler will not move
"int(0);" into other place of code, but it can eliminate the expression
as "never used".

3.
If I will use "A();" instead of "int(0);" (assuming A() has at least
user defined ctor) I am sure that A() can not be moved to other place
of code, but I want to be shure, that "A();" will never be eliminated
as "int(0);", as "never used".

outcome
I just do not use ctor as ordinary function call befor, so do not
shure, that optimization rules will neber be applyed to my expression
"A();" as for "unused variable".

Jan 25 '07 #12
On Jan 25, 1:05 am, "Grizlyk" <grizl...@yande x.ruwrote:
2. the following code

{
int(0);
if( !is_ok() ){b();}
return;

}

The usage of "int(0);" is well defined also, and compiler will not move
"int(0);" into other place of code, but it can eliminate the expression
as "never used".

3. If I will use "A();" instead of "int(0);" (assuming A() has at least
user defined ctor) I am sure that A() can not be moved to other place
of code, but I want to be shure, that "A();" will never be eliminated
as "int(0);", as "never used".
The compiler is free to rearrange, rewrite or eliminate any of the
above lines of code - however it sees fit. The only restriction is that
the "observable behavior" of the program must remain the same as it
would have been had the compiler produced a program that ran exactly as
it was written.

Now, the "observable behavior" of a C++ program consists of 1) the
program's reads and writes to volatile objects and 2) the program's
calls to library I/O routines. That's it. So unless the lines of code
above have some bearing on either program's I/O or its accessing of
volatile objects (and there seems to be little sign of either in the
above examples) then the compiler is likely to eliminate all of the
code above. So to answer your question - "what can be optimized?" So
far, the answer is - just about everthing.

After all, if no one can tell whether this code executes or not when
the program is run, then why would anyone care one way or the other?

Greg

Jan 25 '07 #13
On Jan 25, 10:05 am, "Grizlyk" <grizl...@yande x.ruwrote:
Kai-Uwe Bux wrote:
You need to forget about the fact that A() involves a constructor call.You
have a statement sequence:
a();
if( !is_ok() )b();
The first statement is evaluated first. At the ";" all side-effects of its
evaluation a guaranteed to have taken place since there is a sequence
point. Then the if-statement is executed. This is part of the meaning of
the program. Any subsequence reordering of machine code instructions that
the compiler may perform for optimization must preserve the meaning of the
program (i.e., its observable behavior).
"there is a sequence point."

The definition "sequence point" is not known to me (i do not see any
C++ book speaking about "sequence point"), and i do not use "sequence
point" befor.
>From the standard: "At certain specific points in the execution
sequence called sequence points, all side effects of the previous
evaluations shall be complete and no side effects of subsequent
evaluations shall have taken place."

If I will use "A();" instead of "int(0);" (assuming A() has at least
user defined ctor) I am sure that A() can not be moved to other place
of code, but I want to be shure, that "A();" will never be eliminated
as "int(0);", as "never used".
I don't think that there are any limitations on what the compiler can
do, just that the executable generated must for all intents and
purposes behave as if it was your code executing. Meaning that it might
very well remove your A(), but if it does it have to add something else
to give the same behaviour as if the A() had been there.

--
Erik Wikström

Jan 25 '07 #14
Grizlyk wrote:
Kai-Uwe Bux wrote:
>You need to forget about the fact that A() involves a constructor call.
You have a statement sequence:

a();
if( !is_ok() )b();

The first statement is evaluated first. At the ";" all side-effects of
its evaluation a guaranteed to have taken place since there is a sequence
point. Then the if-statement is executed. This is part of the meaning of
the program. Any subsequence reordering of machine code instructions that
the compiler may perform for optimization must preserve the meaning of
the program (i.e., its observable behavior).
>"there is a sequence point."
The definition "sequence point" is not known to me (i do not see any
C++ book speaking about "sequence point"), and i do not use "sequence
point" befor.
The C++ standard defines the terms observable behavior and sequence point as
follows:

[1.9/6] The observable behavior of the abstract machine is its sequence of
reads and writes to volatile data and calls to library I/O functions

[1.9/7] Accessing an object designated by a volatile lvalue (3.10),
modifying an object, calling a library I/O function, or calling a function
that does any of those operations are all side effects, which are changes
in the state of the execution environment. Evaluation of an expression
might produce side effects. At certain specified points in the execution
sequence called sequence points, all side effects of previous evaluations
shall be complete and no side effects of subsequent evaluations shall have
taken place.

Also, it says:

[1.9/16] There is a sequence point at the completion of evaluation of each
full-expression.

And of course there is a definition of a full-expression. Suffice it to say
that in

a();

there is a sequence point at the ";". That is, all side-effects from
evaluating a() have taken place and the computation has not progressed into
things that come after the ";".

I don't know why your C++ books do not mention this. The standard specifies
the order of execution by saying where sequence points are. Of course, book
authors are free to explain the rules any way they like (as long as what
they say is effectively equivalent to the standard).

>I wonder what made you think it could possibly be otherwise. It's not
like the basic principles of C++ all of a sudden cease to apply just
because an expression involves creating a temporary object.
>"what made you think it could possibly be otherwise"
I think like this:
1.
in the following code

{
int i=0;
if( !is_ok() ){b(i);}
return;
}

The variable "int i=0" defined befor "b(i)". The usage of "i" is well
defined so compiler can move the definition "int i=0" into place of
usage in order to increase speed&size for false condition

{
if( !is_ok() ){int i=0; b(i);}
return;
}

(at least if i write compiler i could do it)

2.
the following code

{
int(0);
if( !is_ok() ){b();}
return;
}

The usage of "int(0);" is well defined also, and compiler will not move
"int(0);" into other place of code, but it can eliminate the expression
as "never used".
But just because it has no side-effects with observable behavior.
>
3.
If I will use "A();" instead of "int(0);" (assuming A() has at least
user defined ctor) I am sure that A() can not be moved to other place
of code, but I want to be shure, that "A();" will never be eliminated
as "int(0);", as "never used".

outcome
I just do not use ctor as ordinary function call befor, so do not
shure, that optimization rules will neber be applyed to my expression
"A();" as for "unused variable".
I see. Anyway, A() is an expression, evaluation of which can not just be
optimized away if the statement has observable effects.

There is only one case where the compiler is allowed to optimize away
constructor calls with side-effects: under certain circumstances,
copy-constructor calls may be elided [see 12.8/15 for details]. Thus, you
should never write copy-constructors that do more than just copy
constructing objects. You cannot rely on them being called. This, however,
is unrelated to the cases you run into: you did not use copy-constructors.
Best

Kai-Uwe Bux
Jan 25 '07 #15
Grizlyk <gr******@yande x.ruwrote:
>"there is a sequence point."
The definition "sequence point" is not known to me (i do not see any
C++ book speaking about "sequence point"), and i do not use "sequence
point" befor.
The "sequence point" concept was inherited from C:
http://www.c-faq.com/expr/seqpoints.html

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jan 25 '07 #16
On Jan 25, 8:55 am, ricec...@gehenn om.invalid (Marcus Kwok) wrote:
Grizlyk <grizl...@yande x.ruwrote:
"there is a sequence point."
The definition "sequence point" is not known to me (i do not see any
C++ book speaking about "sequence point"), and i do not use "sequence
point" befor.The "sequence point" concept was inherited from C:http://www.c-faq.com/expr/seqpoints.html
The "sequence point" description helps in determining what is well
defined in C (and C++).

For example:

int f()
{
int i = 0;
int j = 0;
return g( ++i, ++j ); // Assume g() is pure function of two ints.
}

is well defined, as i and j are both incremented between the sequence
point at the semicolon after the initialization of j and the sequence
point at the entry to g(), but the order is unspecified (and
unknowable).

Ironically, if i and j were declared as volatile, the code becomes
undefined because the order of the writes to i and j become part of the
(potentially) observable behavior of the program.

The compiler is allowed to perform any rewriting of the code that has
the same observable behavior as the written code. Without any volatile
variables, the observable behavior of, say,

std::cout << f() << std::endl;

can be rewritten by the compiler as:

std::cout << g(1, 1) << std::endl;

even if f() is not defined to be inline. The compiler can keep
rewriting as long as desired, so if g was the extremely recursive
Ackerman function (http://en.wikipedia.org/wiki/Ackermann_function),
the code could be rewritten as:

std::cout << 3 << std::endl;

The compiler can also optimise the resulting machine operations provide
the observable behavior is unchanged. In other words a well-defined
program must act "as if" the code was executed exactly as written.

In the original question, Grislyk asked if volatile was needed to
prevent "unused" objects being removed. The rules of C++ do say that
volatile is needed to ensure that they are actually constructed and
destructed. In practice, this is not an issue, as C++ cannot optimize
away observable behaviour.

In the code:

class A{ A(){cout<<"star t..."; } }
class B{ B(){cout<<"ok"< <endl; } }

void inp();
void test()
{
A();
inp();
B();
}

test() may be implimented directly something like this (don't take too
literally):

void test()
{
{
char tmp[sizeof(A)]; // Allocate memory.
new( tmp ) A; // Initialize memory by calling A::A(),
writes "start..."
(A *)(tmp)->~A(); // Destruct A (no-op)
}
inp();
B(); // Same as for A above.
}

A smart compiler will follow the "as if" rule and produce code
equivalent to this:

void test()
{
cout<<"start... "; // Only observable behavior in A::A()
inp();
cout<<"ok"<<end l;
}

In nearly all real situations, this is exactly what was intended in the
first place, actually allocating and constructing A and B being
incidental to the requirements.

Best Regards,

David O.

Jan 25 '07 #17
"Grizlyk" wrote:
>
auto volatile const class_name tmp;
The "volatile" keyword can not be used, because
class_name::cla ss_name() must be declared as "volatile" function.

I have decided (in order to do not fear possible optimization) to
rewrite class's ctors into set of global functions placed in the
namespace with the same name.

Already have done it. While all looks ok.

Jan 26 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
3291
by: Reinhold Birkenfeld | last post by:
Hello, another Perl/Python question: the subject says it all. Perl is going to change dramatically to become a more powerful and easier to (read|write) language. Is Python taking a similar step (-> Python 3) some time in the near future? Reinhold
3
3143
by: Glen Low | last post by:
I have written a new implemention of the std::valarray library that is optimized to use Altivec (Apple's "Velocity Engine", part of the PowerPC G4's in most Macintoshes and the announced IBM PPC 970). The implementation is mostly standard conforming and is complete. As soon as I get my shingle up on the web (1 or 2 day's time), I'll post the library and its accompanying docs, which I call "MacSTL". Would like comments, tests, discussions...
3
29680
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification says: "The 'visibility' property takes the value 'collapse' for row, row group, column, and column group elements. This value causes the entire row or column to be removed from the display, and the space normally taken up by the row or column to...
133
8573
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
6
3707
by: SSG | last post by:
Hai All! I need the optimized code for finding the string length. i dont want to use strlen function......... can anyone know reply........ By S.S.G
18
3535
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
11
3147
by: ThunderMusic | last post by:
Hi, I have a windows service that only loads a CSV file and import it's data using SqlBulkCopy in a newly created Sql Server 2005 table using 25000 rows batches. If I build the service in debug mode and run it, I get descent performances. If I build it in release mode, I would expect it to at least stay as fast and maybe be faster, but I actually lose performance. I lose about 10% performance.
6
2472
by: \Frank\ | last post by:
I trying to learn what a Bitmap is. Not a Managed Bitmap Object but one that, for example, comes from the clipboard with CF_BITMAP. I'm guessing that a CompatableBitmap is an array of indices that point to the colors in a Palette of the display driver. So if I get a bitmap via CF_BITMAP I need to also get a Palette off the clipboard and realize it then display the bitmap. I believe "Realize it" means to put it into the display driver.
7
2566
by: Zytan | last post by:
I have the simplified build ("show advanced build configurations" turned off), so that pressing F5 runs in DEBUG mode with the debugger. When an assertion fires, I find that I cannot 'watch' some data, it explains that "Cannot evaluate expression because the code of the current method is optimized". I know that I can go into Project- My question: WHY is the DEBUG build optimizing code? Shouldn't that only occur for RELEASE builds? ...
0
10032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9841
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5141
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5303
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.