473,725 Members | 1,942 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 #1
17 2112


On Jan 24, 11:50 am, "Grizlyk" <grizl...@yande x.ruwrote:
Hello.

What can be optimised in C++ code and how i can garantee stable
behaviour below
Anything might be optimised as long as visible behaviour is not
affected. Visible behaviour is anything output by the program, but not
e.g. how long time something takes.
>
1.
Are expression "auto volatile" can deny removing as "unused temporary"
like this:

auto volatile const class_name tmp;
No.
>
If not, how can i do it else?
What is your problem? Optimisation should not cause problems (unless
the optimised code is wrong, of course).
>
2.
Can C++ garantee, that unnamed temporary object will not be removed as
"unused temporary" here:
Yes. Anything that has a side-effect will normally not be optimised
away. Also, removal of your temporary objects below would change output
which is visible behaviour.
>
class A{ A(){cout<<"star t..."; } }
class B{ B(){cout<<"ok"< <endl; } }

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

Jan 24 '07 #2
"peter koch" wrote:

What can be optimised in C++ code and how i can garantee
stable behaviour below
Anything might be optimised as long as visible behaviour is not
affected.
I said, that compiler and programmer can think about "visible
behaviour" differently. Compiler for example, often eliminate
"unusable" code if compiler think code is not used.
1.
Are expression "auto volatile" can deny removing as "unused temporary"
like this:
auto volatile const class_name tmp;No.
What is your problem?
In the case of temporary object compiler could remove object to the
point, where object will be used, for example:

{
int tmp=0;

if(!is_ready)re turn tmp;
return 1;
}

can be optimized to

{
if(!is_ready){ int tmp=0; return tmp; }
return 1;
}

Can "auto volatile" force object to be placed into stack, not into CPU
registers?
2.
Can C++ garantee, that unnamed temporary object will not be
removed as "unused temporary" here:
In the case of temporary object

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

compiler could eliminate objects, that created but after that never
used:

void test(){ inp(); }

because A(); and B(); are never used unnamed objects.

Jan 24 '07 #3
Grizlyk wrote:
"peter koch" wrote:
>
What can be optimised in C++ code and how i can garantee
stable behaviour below
Anything might be optimised as long as visible behaviour is not
affected.

I said, that compiler and programmer can think about "visible
behaviour" differently. Compiler for example, often eliminate
"unusable" code if compiler think code is not used.
The compiler doesn't just do random guesses about what isn't needed. It
usually removes code that actually isn't used, even if you might think it
is. There are some special circumstances in low-level programming (direct
interaction with hardware, operating system kernels and such) where more
control is needed. That's what volatile is there for.
1.
Are expression "auto volatile" can deny removing as "unused temporary"
like this:
auto volatile const class_name tmp;No.
What is your problem?

In the case of temporary object compiler could remove object to the
point, where object will be used, for example:

{
int tmp=0;

if(!is_ready)re turn tmp;
return 1;
}

can be optimized to

{
if(!is_ready){ int tmp=0; return tmp; }
return 1;
}
It can even optimize it to:

{
if(!is_ready){ return 0; }
return 1;
}
Or it might even do:

{
return is_ready != 0;
}

But why would that be a problem?
Can "auto volatile" force object to be placed into stack, not into CPU
registers?
On some compilers, it does, but I'm not sure if that's required by the
standard.
2.
Can C++ garantee, that unnamed temporary object will not be
removed as "unused temporary" here:

In the case of temporary object

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

compiler could eliminate objects, that created but after that never
used:

void test(){ inp(); }

because A(); and B(); are never used unnamed objects.
The compiler might optimize those objects away only if their construction
and destruction are guaranteed to have no side effects, i.e. if you
wouldn't notice it anyway.
Jan 24 '07 #4
Rolf Magnus wrote:
1.
Are expression "auto volatile" can deny removing as "unused temporary"
like this:
auto volatile const class_name tmp;No.
What is your problem?
In the case of temporary object compiler could remove object to the
point, where object will be used, for example:
{
int tmp=0;
if(!is_ready)re turn tmp;
return 1;
}
can be optimized to
{
if(!is_ready){ int tmp=0; return tmp; }
return 1;
}It can even optimize it to:

But why would that be a problem?
Instead of "int tmp" can be "myclass tmp", so non-trivial ctor exist,
and I want to be shure, that myclass::myclas s() ctor will be
unconditional executed. And I do not want rewite class's ctors into
static function calls.
2.
Can C++ garantee, that unnamed temporary object will not be
removed as "unused temporary" here:
In the case of temporary object
class A;
class B;
void test(){ A(); inp(); B(); }
compiler could eliminate objects, that created but after that never
used:
void test(){ inp(); }
because A(); and B(); are never used unnamed objects.
The compiler might optimize those objects away only if their construction
and destruction are guaranteed to have no side effects, i.e. if you
wouldn't notice it anyway.
What means "to have no side effects"? The fact that A() and B() have
non-trivial ctors is side effect or not? Or it is incorrect to call
obects ctors or I must explicit write A::A() or B::B(). What standard
require?

Jan 24 '07 #5
On Jan 24, 3:46 pm, "Grizlyk" <grizl...@yande x.ruwrote:
Rolf Magnus wrote:
The compiler might optimize those objects away only if their construction
and destruction are guaranteed to have no side effects, i.e. if you
wouldn't notice it anyway.
What means "to have no side effects"? The fact that A() and B() have
non-trivial ctors is side effect or not? Or it is incorrect to call
obects ctors or I must explicit write A::A() or B::B(). What standard
require?
A side effect is when something that happens besides the fact that (in
this instance) an object is created. Consider the following:

class Foo {
Foo(int& i) { ++i; }
}

If we create a temporary with this object and pass an integer as a
parameter the ctor will increment the supplied integer, this is a
side-effect. This means that the compiler can not optimize away the
creation of the object since it would also affect the value of the
integer passed (unless the integer can also be optimized away.

If you instead have the following class:

class Bar {
Bar(int i) { ++i; } // Notice no &
}

The compiler can optimize away the construction of the object since
nothing but the objects existence depends on it.

--
Erik Wikström

Jan 24 '07 #6
Grizlyk wrote:
Rolf Magnus wrote:
1.
Are expression "auto volatile" can deny removing as "unused
temporary" like this:
auto volatile const class_name tmp;No.
What is your problem?
In the case of temporary object compiler could remove object to the
point, where object will be used, for example:
{
int tmp=0;
if(!is_ready)re turn tmp;
return 1;
}
can be optimized to
{
if(!is_ready){ int tmp=0; return tmp; }
return 1;
}It can even optimize it to:

But why would that be a problem?

Instead of "int tmp" can be "myclass tmp", so non-trivial ctor exist,
and I want to be shure, that myclass::myclas s() ctor will be
unconditional executed. And I do not want rewite class's ctors into
static function calls.
Don't worry: in cases like this, the constructors will be called unless they
do not add to the observable behavior. In other words, constructor calls
can be optimized away only if the static function calls that you would use
in their place could be optimized away, too.
2.
Can C++ garantee, that unnamed temporary object will not be
removed as "unused temporary" here:
In the case of temporary object
class A;
class B;
void test(){ A(); inp(); B(); }
compiler could eliminate objects, that created but after that never
used:
void test(){ inp(); }
because A(); and B(); are never used unnamed objects.
The compiler might optimize those objects away only if their construction
and destruction are guaranteed to have no side effects, i.e. if you
wouldn't notice it anyway.

What means "to have no side effects"? The fact that A() and B() have
non-trivial ctors is side effect or not? Or it is incorrect to call
obects ctors or I must explicit write A::A() or B::B().
No, you don't. You can call the constructor just fine by writing A(). More
precisely, upon evaluation of this expression, a temporary is created and
constructed by calling the default constructor.

Now, as far as optimization is concerned, the following might happen:

struct A {

char arr [200];

A () {
std::cout << "hello world!\n";
}

};

// ...

int main () {
A(); //(*)
}

The standard requires that line (*) prints "hello world!". However, the
compiler is allowed to observe that there is no need to allocate 200 bytes
on the stack. Thus, machine instructions to that extend maybe eliminated.
I.e., in fact the compiler may actually not create an object (region of
memory) but just inline the body of the constructor. The reason is that the
observable behavior does not differ either way.
Best

Kai-Uwe Bux
Jan 24 '07 #7
Kai-Uwe Bux wrote:
Or it is incorrect to call objects ctors
or I must explicit write A::A() or B::B()?

No, you don't. You can call the constructor just fine by writing A(). More
precisely, upon evaluation of this expression, a temporary is created and
constructed by calling the default constructor.
Well, and is it a good (known) C++ idiom to do the temporary object, to
use ctor of object as ordinary function call or not good?

The goal to use unnnamed ctor calling:

1. old code use X() as object, and I do not want to rewrite ctors into
static functions or class into namespace. It seems to me, that easyer
to change ctors implementations .

2. class members of the object, used by ctor can be created
unconcditionall y

class X
{
public:
A a; //a created independed from if( !is_ok() ) or not?
X(){ if( !is_ok() )throw a; }
};

If compare 2 with next example

void test()
{
A a;
if( !is_ok() )throw a;
}

If A has ctor, must "A::A()" be called befor "is_ok()" in both cases or
in the case of class member only? Does concrete sequence exist between
functions calls?

Jan 24 '07 #8
On 2007-01-24 17:23, Grizlyk wrote:
2. class members of the object, used by ctor can be created
unconcditionall y

class X
{
public:
A a; //a created independed from if( !is_ok() ) or not?
X(){ if( !is_ok() )throw a; }
};
Yes, A's ctor will be called before X's ctor. These kinds of things are
easy to examine by creating small classes that prints some text in the
ctor (and perhaps dtor).
If compare 2 with next example

void test()
{
A a;
if( !is_ok() )throw a;
}

If A has ctor, must "A::A()" be called befor "is_ok()" in both cases or
in the case of class member only?
I assume you meant function-call only, since it's obvious that A's ctor
have to be called in test(). But to answer your question, yes A's ctor
will be called in both cases.
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? If you have a class like this:

class X {
A a;
B b;
C c;
// ...
}

Then the ctors of the members will be declared in the order they were
declared, that's why you have to take some care when using initialization.

X::X(int a_, int b_)
: b(b_), a(b + a_) // OOPS!
{ }
In the above constructor to X the initialization of the members (ctor-
calls) will be in the order they were declared, so the initialization of
'a' witch 'b + a_' is no good since 'b' has not been initialized yet.

--
Erik Wikström
Jan 24 '07 #9
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.

Jan 24 '07 #10

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
3139
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
29679
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
8558
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
6
3705
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
3530
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
3146
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
2470
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
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
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...
1
9174
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
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
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
6011
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
4517
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
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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

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.